2014 questions from group B in Xi'an, 2014 Huawei
2014 questions of group B in Xi'an
Question 1: Number of lights on
A corridor contains n (1 ≤ n ≤ 65535) lamps, numbers 1, 2, 3 ,... N-1 and n. Each lamp is controlled by a wire pulling switch. Start, all the lights are turned off.
N students passed through the corridor. The first student pulls the switch of the electric lamp whose number is a multiple of 1, and the second student pulls the switch of the electric lamp whose number is a multiple of 2; then the third student pulls the switch of the electric lamp whose number is a multiple of 3. Then, the nth student pulls the switch of the electric lamp whose number is a multiple of n. After n students finished according to this rule, there were several lights in the corridor.
Note: The number of electric lights is the same as that of students. You cannot write a double loop, and the operation times out.
Enter 65535
255 output
Question Analysis:
For any lamp, because it is not on, when its switch is pressed for an odd number of times, the lamp is on; when its switch is pressed for an even number of times, the light is closed. The number of times a lamp is switched on or off is equal to the number of the lamp numbers, this is to ask which lights have an odd number factor. obviously, the number of distinct has an odd number of factors. Each number divided by one number A equals to another number B, so A and B are its factors, so the factors appear in pairs, but the factor must be odd, it must be A = B, so this number must be A square.
To sum up, this question is very simple, that is, to find the number of full counters in 1-65535. We use the enumeration method
========================================================== ======================================
Reference code:
// Number of lights on. cpp // 2014.7.9 hepanhui # include <iostream> using namespace std; int main () {int n; int cnt = 0; cin> n; for (int x = 1 ;; x ++) {int y = x * x; if (y> n) break; cnt ++;} cout <cnt <endl ;}
The debugging process is prone to errors. We need to put cnt ++ behind the jump out of the loop, otherwise the number of times will be more than the correct answer.
Question 2: Same characters
Enter a string to determine whether the string contains the same substring (the string length is greater than 1), yes, output 1, no, and output 0.
For example, 12312 contains two 12 s, so output 1; 23456 does not have the same subsequence, and output 0.
Input: 12312
Output: 1
Question Analysis:We first use a double loop for processing, then use a while statement, and finally use the intermediate variable for a conversion.
========================================================== ====================================
Reference code:
// Same character. cpp // 2014.7.10 hepanhui # include <iostream> # include <string> const int maxn = 1000; using namespace std; int character char (char * str) {// illegal input if (str = NULL) return 0; int cnt = 0; int p, q; int len = strlen (str); for (int I = 0; I <len; I ++) {for (int j = I + 1; j <len; j ++) {p = I; q = j; while (str [p] = str [q]) {p ++; q ++; cnt ++;} if (cnt> 1) return 1; cnt = 0 ;}}return 0 ;}int main () {char s [1000]; gets (s); cout <symbol char (s) <endl; return 0 ;}
Errors During debugging:
① The input string may contain spaces. Therefore, cin cannot be used.
② When using gets (s), be sure to add the header file # include <string>
③ Remember to use the intermediate variable, because it ensures that ij does not change during judgment, so the intermediate variable is used.
④ Remember that cnt always needs to be set to 0 after each while
Question 3: Integer Division
Returns the result by dividing two integers with strings. If it is a repeating decimal point, enclose the bits in parentheses.
Function prototype: void div (const int a, const int B, char * str)
Input: 1 3
Output: 0. (3)
Question Analysis:
This question is a little bit more than a circular decimal question, that is, to judge the positive and negative of AB, and all others are the same.
The difficulty lies in the cycle and cycle length of the cyclic decimal places. Therefore, we define two arrays here.
Int reminder_exist [max_INT]; int reminder_pos [max_INT];
========================================================== ==============================================
Reference code:
// Integer division. cpp // 2014.7.9 hepanhui # include <iostream> # include <string> using namespace std; const int maxn = 100; // set the maximum number of digits of the string const int max_INT = 10000; int reminder_exist [max_INT]; int reminder_pos [max_INT]; void div (const int a, const int B, char * str) {int numerator, denominator, quotient, reminder, outcnt = 0; int flag = 0; // negative number indicates int original_numerator = a; // evaluate the memset (reminder_exist, 0, sizeof (rem Inder_exist); memset (reminder_pos, 0, sizeof (reminder_pos); numerator = a; // when we need to change the denominator of a molecule because the const int is defined, so we convert denominator = B through the intermediate variable; if (a * B <0) flag = 1; // convert the numerator and denominator to an integer numerator = numerator <0? -Numerator: numerator; denominator = denominator <0? -Denominator: denominator; quotient = numerator/denominator; reminder = numerator % denominator; int integer = quotient; // find the loop // int found_cycle = 0; int cycle_pos = maxn; // The Position of the loop int cycle_len = 0; // The initialization cycle length int I = 0; for (I = 0; I <= maxn; I ++) {// find the case where the remainder is equal and find the cycle if (reminder_exist [reminder]) {cycle_pos = reminder_pos [reminder]; cycle_len = I-cycle_pos; break ;} else {reminder_exist [reminder] = 1; reminder_pos [reminder] = I;} numerator = reminder * 10; quotient = numerator/denominator; reminder = numerator % denominator; str [outcnt ++] = quotient + '0'; // store the updated operator in the string} str [outcnt ++] = '\ 0'; if (! Flag) {cout <integer <". ";} else cout <"-"<integer <". "; for (int j = 0; j <cycle_pos; j ++) cout <str [j]; cout <" ("; for (int j = cycle_pos; j <I; j ++) cout <str [j]; cout <")" <endl ;}int main () {int a, B, flag = 0; char s [maxn]; cin> a> B; if (! A &&! B) return 0; div (a, B, s); return 0 ;}
Errors that may occur during debugging:
① The settings of the main function and sub-function variables are messy with the arrangement of illegal input;
② When we discuss negative numbers, we should convert them into Integers to cycle and learn to use them? : Three-object Operator
③ Define the output as a string type. We cannot directly str [outcnt ++] = quotient; Because quotient is a number, we must add + '0'
④ You still need to note the questions stored in the string array.
1) initialize outcnt to 0
2) In str [outcnt ++] ++, you cannot forget
3) str [outcnt ++] = '\ 0'
4) char str [maxn] When the main function is defined;
5) the output format of this question must be carefully written.
Error Message
Xi'an: Tomorrow (919), I will go to the Huawei machine for a trial. What is the question of the machine?
It is difficult for you to search questions over the years on the Internet. Today, Huawei's software in Wuhan is developed for 30 minutes. It is not difficult to answer a question. For reference only. I wish you success.