一段十进制转二进制的代码,方便各种乱搞的题目用:
struct twonum { ll len,content[100]; }; twonum ten2two(ll n) { //最高位是len-1 ,最低位0 twonum re; ll i,ans[100]; for(i=0; i<100; i++) re.content[i] = 0; if(n==0) { re.len = 1; re.content[0] = 0; return re; } for(i=0; n; i++) { ans[i]=n%2; n=n/2; } re.len = 0; ll k = i; for(i=0; i<k; i++) { re.content[re.len++] = ans[i]; } return re; }
二进制转十进制:
ll two2ten(twonum ee) { ll r = 0; int t; int m = ee.len; for(t=0; t < m; t++) { ll v=pow(2,t); ll p=v*ee.content[t]; r=r+p; } return r; }