Netease 2017 campus recruitment algorithm question C language implementation source code, 2017 campus recruitment

Source: Internet
Author: User

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 );}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.