IOS interview FAQ
1. jesus has 13 disciples, and one of them is the traitor who sold Jesus. Please use exclusion to find the traitor: 13 people sat around and started reporting cyclically from the first person and counted to three, the last person left is the traitor.
Int people [13] = {1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
Int count = 0; // used to record the number of reports
Int number = 13; // record the number of living persons
Int I = 0; // records the number of persons reporting
While (number> 1 ){
If (people [I]! = 0 ){
Count ++; // If a [I] is not removed, the report count is valid.
} If (count = 3 ){
People [I] = 0; // remove,
Count = 0; // the return value is cleared.
Number --; // The number of active users minus one
}
I ++; // the next person in the record
If (I = 13 ){
I = 0; // set the number of applicants to the first when the value exceeds the specified range.
}
} For (int I = 0; I <13; I ++ ){
If (people [I]> 0 ){
Printf ("the traitor is % d", people [I]);
}
}
2. There are 1000000 numbers. The value range of each number is 0 ~ 999999. Find the number of duplicates.
# Derefined COUNT 1000000
Int main (void)
{
Inta [COUNT] = {0 };
For (int I = 0; I <COUNT; I ++ ){
Int number = arc4random () % COUNT; // use the random number as the subscript and the value as the number of occurrences
A [number-1] ++;
}
// Output repeated numbers and number of repetitions
For (int I = 0; I <COUNT; I ++ ){
If (a [I]> 1 ){
Printf ("% d repeats % d times \ n", I + 1, a [I]);
}
}
Return0;
}