1. Jesus had 13 disciples, one of whom was the traitor who betrayed Jesus, please use the rule of exclusion to find the traitor: 13 people sitting around a circle, from the first person began to cycle off, count to three exclude, the last remaining person is a traitor
int people[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
int count = 0;//is used to record a datagram
int number = 13;//Records alive
int i = 0;//record number of people are on the count.
while (number > 1) {
if (people[i]! = 0) {
count++; Assuming A[i] is not removed, the count is valid
} if (count = = 3) {
People[i] = 0;//removed,
Count = 0;//to clear Zero
number--;//number of survivors reduced by one
}
i++;//the next person to record the count.
if (i = = 13) {
i = 0;//when out of range, immediately set the number of numbers as the first person
}
} for (int i = 0; i < i++) {
if (People[i] > 0) {
printf ("Traitor is the first%d", People[i]);
}
}
2. There are 1 million numbers, each value range is 0~999999, to find out the number of repeated
#define COUNT 1000000
int main (void)
{
Inta[count] = {0};
for
(int i = 0; i < COUNT; i++) {
int number = Arc4random ()%count;//random number as subscript, value as occurrence
A[number-1] + +;
}
//output repeated numbers and repeated times
for
(int i = 0; i < count;i++) {
if (A[i] > 1) {
printf ("%d repeats%d times\n", I+1,a[i]);
}
}
Return0;
}
iOS Interview Common questions