Netease 2017 campus recruitment algorithm question C language implementation source code, 2017 campus recruitment
Question:
Given an array, except for a number that appears once, the remaining number appears three times. Find the number of occurrences. For example: {1, 2, 1, 2, 1, 2, 7}, find 7.
Format:
Input n in the first line indicates the length of the array, and input A [n] In the next line (the input array must meet the problem description requirements ), the number of output records that appear only once.
Requirements:
Your algorithm can only be a linear time complexity and cannot use extra space ~
Sample Input
4
0 0 0 5
Sample output
5
Source code:
# Include <stdio. h> # include <math. h> int main () {int a [] = {,}; int n = 10; int res, I, j, m; res = 0; for (I = 0; I <32; I ++) // 32 is the binary digit of the int {m = 0; for (j = 0; j <n; j ++) {if (a [j] % 2 = 1) m ++; a [j] = a [j]> 1 ;} res + = (m % 3) * pow (2, I); // 3 indicates that the remaining number appears three times} printf ("% d", res );}