Блог пользователя yisyis

Автор yisyis, 12 лет назад, По-английски

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?

  • Проголосовать: нравится
  • -4
  • Проголосовать: не нравится

»
12 лет назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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 лет назад, # |
Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится

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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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 лет назад, # ^ |
        Проголосовать: нравится +1 Проголосовать: не нравится

      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 лет назад, # |
Rev. 3   Проголосовать: нравится +12 Проголосовать: не нравится

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 лет назад, # ^ |
      Проголосовать: нравится +3 Проголосовать: не нравится

    Instead of first loop use int res = 100;

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