content
Question 1: The question of age
Question 2: Pirates and Gold Coins
Question 5: Number of exchanges
Question 6: Self-Descriptive Sequences
Question 1: The question of age
Mrs S has always been mysterious. When someone asked her age, she thought about it and said,
"Twenty years ago, my husband was exactly 2 times my age, and now he is exactly 1.5 times my age".
Can you figure out how old Mrs S is now?
Note that what needs to be submitted is an integer, do not fill in any redundant content.
Question 2: Pirates and Gold Coins
Twelve pirates discovered a large amount of gold coins on a small island, with a total of nearly 50,000 coins in total.
Boarding the island is at night, the weather is not good. For various reasons, some pirates stole a lot and some took very little.
Later, in order to "equalize the rich and the poor", the leader proposed a very strange plan:
each pirate put the gold coins he got on the table. Then start a game.
The pirate with the most gold coins has to take out his gold coins to compensate others.
The amount of compensation is exactly double the amount of gold coins of the compensated person (that is, double the original amount).
The game has to keep going until it can't be completed.
(When there is more than one person with the most gold coins or the person with the most gold coins does not hold enough gold coins to compensate others)
The game went on nervously like this, and it went on for 12 rounds, and everyone happened to "bleed" once. What
's even more bizarre is that just after the 12th round, everyone's gold coins were actually equal! ! Is this God's will?
Please calculate, before the game starts, the initial gold coins of all pirates are arranged from small to large with a space in the middle.
The answer looks like this:
8 15 29 58 110 ...
Of course, this is not the correct answer.
Note:
What needs to be submitted is a line of integers separated by spaces, do not submit any redundant content.
The delimiter should use a space in Western language, do not use other symbols (such as commas, Chinese symbols, etc.)
Question 3: All Arrangements
For a certain string, such as: "1234", find all its permutations.
And it is required that these full arrangements must be arranged in ascending alphabetical order. For " 1234
", it should output (total 4!=24 lines) :
1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
31412
3421
4123
4131 24
4213
The following is the implementation program, please analyze the program logic carefully and fill in the missing code in the underlined part.
// Rotate the first k, and then recurse
import java.util.*;
public class A
{
static void permu(char[] data, int cur){
if(cur==data.length-1){
System.out.println(new String(data));
return;
}
for(int i=cur; i<data.length; i++){
char tmp = data[i];
for(int j=i-1; j>=cur; j--) data[j+1] = data[j];
data[cur] = tmp;
permu(data, cur+1);
tmp = data[cur];
__________________________________________ ;
data[i] = tmp;
}
}
static void permu(String x){
permu(x.toCharArray(),0);
}
public static void main(String[] args){
permu("1234");
}
}
Please note: Only fill in the missing content in the underlined part, do not copy existing codes or symbols.
Question 4: Joseph Ring
The numbers of n persons are 1~n. If they are arranged in a circle clockwise according to the number, the number is counted clockwise from the person with the number 1.
(The number of reports starts from 1) When k is reported, the person quits the game circle. The next person starts counting from 1 again.
Find the number of the last remaining person. This is the famous Joseph ring problem.
This problem is to find the number of the last remaining person given n and k.
The input of the question is one line, and the integers n and k separated by 2 spaces are
required to output an integer, indicating the number of the last remaining person.
Convention: 0 < n, k < 1 million
For example enter:
10 3
The program should output:
4
Resource convention:
peak memory consumption (including virtual machine) < 256M
CPU consumption < 1000ms
Please output strictly according to the requirements, and do not superficially print superfluous content like: "Please enter...".
All code is placed in the same source file, after debugging, copy and submit the source code.
Don't use the package statement. Do not use features of jdk1.7 and above.
The name of the main class must be: Main, otherwise it will be processed as invalid code.
Question 5: Number of exchanges
The demand for talents in the IT industry is rising steadily. Industry giants Baidu, Alibaba and Tencent (BAT for short) were recruiting at a beach.
The recruiting department is lined up. Due to the free preemption of seats, the seats of the three major companies are randomly staggered, such as:
ABABTATT, which makes the candidates very uncomfortable.
As a result, management asked recruiters to make the necessary swaps so that each group's seats were next to each other. That is, the final shape is:
BBAAATTT, of course, it may also be:
AAABBTTT and so on.
Now, assuming only 2 seats can be swapped at a time, and knowing the current seat distribution,
your task is to calculate how many swaps are required to get each group's recruiting seats next to each other.
The input is a line of n characters (containing only the letters B, A, or T) representing the current seat distribution.
The output is an integer representing at least the number of swaps.
For example, enter:
TABTABBTTTT
The program should output:
3
For another example, enter:
TTAAABB
The program should output:
0
We agree that the length n of the input string is not greater than 100,000
Resource convention:
peak memory consumption (including virtual machine) < 256M
CPU consumption < 1000ms
Please output strictly according to the requirements, and do not superficially print superfluous content like: "Please enter...".
All code is placed in the same source file, after debugging, copy and submit the source code.
Don't use the package statement. Do not use features of jdk1.7 and above.
The name of the main class must be: Main, otherwise it will be processed as invalid code.
Question 6: Self-Descriptive Sequences
Xiao Ming is studying a sequence, called the Golomb self-describing sequence, which may be denoted as {G(n)}. This sequence has 2 interesting properties:
1. For any positive integer n, n occurs exactly G(n) times in the entire sequence.
2. This sequence is not descending.
Here are the first few terms of {G(n)}:
n 1 2 3 4 5 6 7 8 9 10 11 12 13
G(n) 1 2 2 3 3 4 4 4 5 5 5 6 6
Given an integer n, can you help Xiao Ming calculate the value of G(n)?
Input
----
an integer n.
For 30% of the data, 1 <= n <= 1000000
For 70% of the data, 1 <= n <= 1000000000
For 100% of the data, 1 <= n <= 2000000000000000
output
----
an integer G(n)
【Sample input】
13
[Sample output]
6
Resource convention:
peak memory consumption (including virtual machine) < 256M
CPU consumption < 1000ms
Please output strictly according to the requirements, and do not superficially print superfluous content like: "Please enter...".
All code is placed in the same source file, after debugging, copy and submit the source code.
Don't use the package statement. Do not use features of jdk1.7 and above.
The name of the main class must be: Main, otherwise it will be processed as invalid code.