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?
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);
}
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; } }
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)
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`
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)
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.
Instead of first loop use
int res = 100;
// cumulative xor (0..N) depends on at most three numbers ;)