ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [2010] Problem A. Odd Man Out
    Programming/Google Code Jam 2018. 2. 6. 16:18

    URL : https://code.google.com/codejam/contest/438101/dashboard

    Problem 

    You are hosting a party with G guests and notice that there is an odd number of guests! When planning the party you deliberately invited only couples and gave each couple a unique number C on their invitation. You would like to single out whoever came alone by asking all of the guests for their invitation numbers.


    Input

    The first line of input gives the number of cases, N.
    N test cases follow. For each test case there will be:

    • One line containing the value G the number of guests.
    • One line containing a space-separated list of G integers. Each integer C indicates the invitation code of a guest.

    Output

    For each test case, output one line containing "Case #x: " followed by the number C of the guest who is alone.

    Limits

    1 ≤ N ≤ 50
    0 < C ≤ 2147483647

    Small dataset

    3 ≤ G < 100

    Large dataset

    3 ≤ G < 1000


    [Solution]


    #include <stdio.h>


    int main()

    {

    int input,i,j,input2,k,count;

    scanf("%d",&input);

    for(i = 0; i < input; i++)

    {

    scanf("%d",&input2);

    int arr[input2];

    for(j = 0; j < input2; j++)

    {

    scanf("%d",&arr[j]);

    }

    for(j = 0; j < input2; j++)

    {

    for(k = j+1; k < input2; k++)

    {

    if(arr[j] == arr[k])

    {

    arr[j] = 0;

    arr[k] = 0;

    }

    }

    }

    for(j = 0; j < input2; j++)

    {

    if(arr[j] != 0)

    goto print;

    }

    print:

    printf("Case #%d: %d\n",(i+1),arr[j]);

    }

    return 0;

    }


    arr배열을 내부적으로 검사해서 같은 값이 있을때 빼주면 되는데


    더 쉽게 풀이하는 방법은 xor 연산을 하면된다.


    같은 값을 xor 연산하면 0이 되기 때문.

    반응형
Designed by Tistory.