yisyis's blog

By yisyis, 12 years ago, In English

Today my friend ask me a question and i need help

This is the question:

he randomly wrote numbers 1 to 100 on a paper. But somehow he missed a one number. How many ways you find the missing number.

But please don't write very similar answers like

let define tzv=sum all of the numbers he wrote

and missing number is (100*101)/2-tzv

that is a solution but you do it multiply all numbers it will be count same because this two solution is almost the same.

So how many ways i find the missing number?

  • Vote: I like it
  • -4
  • Vote: I do not like it

| Write comment?
»
12 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

for(int i=1;i<=100;i++) { fl=0; for(int j=1;j<=100;j++) { if(ar[j]==i) { fl=1; break; } } if(fl==0) printf("Got it ! %d\n",i);

}

»
12 years ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

char a[101]; // all set to 0 initially for(i=1;i<=99;i++){ scanf("%d",number); a[number] = 1; } for(i=1;i<=100;i++){ if(a[i]==0){ printf("Got it %d\n",i); break; } }

»
12 years ago, # |
  Vote: I like it 0 Vote: I do not like it

int bf,N = 50*101 // (1 + 100, 2 + 99, ... , 50 + 51) while (scanf("%d",&bf) == 1) N -= bf;
printf("%d",N); I hope it works)

  • »
    »
    12 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    it is the same i wrote in the question

    `let define tzv=sum all of the numbers he wrote

    and missing number is (100*101)/2-tzv`

    • »
      »
      »
      12 years ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      tzv=sum all of the numbers. There are no "tzv" in my solution. Just N — x1 — x2 — x3 — ... — x99. That was general idea. But I really don't care, forget about it)

»
12 years ago, # |
Rev. 3   Vote: I like it +12 Vote: I do not like it

you can use xor

int res = 0; for(int i = 1; i<=100; i++) res ^= i; for(int i = 0; i<99; i++) res ^= a[i];

res will be equal to the missing number.

ps: I didn't see the previous comment, sorry for the duplicate record.

  • »
    »
    12 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Instead of first loop use int res = 100;

    // cumulative xor (0..N) depends on at most three numbers ;)