Topic Link http://acm.hust.edu.cn/vjudge/problem/19593
Thinking of solving problems
Write the number 2 in the system.
In the case of 2, the relationship between the two is compared, and the conclusion can be drawn.
Code
#include <stdio.h>#include<string.h>#include<stdlib.h>Const intMaxLen =205;intBase2[maxlen];voidGetBase2 (intNumint&W) { while(num! =0) {base2[++W] = num%2; Num/=2; }}voidChangeBASE (intNumint&W) { if(Num >0) { for(intI=0; i<=w; i++)ifI2==1) { intJin =0, j = i +1; if(Base2[i]) base2[j]++; Jin= Base2[j]/2; BASE2[J]%=2; while(Jin) {j++; BASE2[J]+=Jin; Jin= Base2[j]/2; BASE2[J]%=2; } W= w > J?w:j; } } Else if(Num <0) { for(intI=0; i<=w; i++)ifI2==0) { intJin, j = i +1; if(Base2[i]) base2[j]++; Jin= Base2[j]/2; BASE2[J]%=2; while(Jin) {j++; BASE2[J]+=Jin; Jin= Base2[j]/2; BASE2[J]%=2; } W= w > J?w:j; } } ElseW =0;}intMain () {intCases, T =1; intWei; scanf ("%d", &cases); while(cases--) { intNumber ; Wei= -1; memset (Base2,0,sizeof(BASE2)); scanf ("%d", &Number ); GETBASE2 (ABS (number), Wei); ChangeBASE (number, Wei); printf ("Case #%d:", T); for(intI=wei; i>=0; i--) printf ("%d", Base2[i]); printf ("\ n"); T++; } return 0;}
uva11121--2 Binary