Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
有两步没看懂,看懂的解释一下In Reply To:Re:很简单的一道题嘛,直接暴力算就过了(440K 0MS),好像用位运算的效果会更好,不管了,反正是过了,贴上自己的代码喽 Posted by:conanhjj at 2008-12-27 16:40:14 count 这个变量是用来干嘛的??看不懂。。。 while(cin >> n && n != 0) { int bit[50]; memset(bit,50,sizeof(int)); int digit = 0; int count = 0; int temp = n; while(temp > 0)//求出二进制数(逆序) { bit[digit] = temp % 2; digit++; temp /= 2; } bit[digit] = 0; int i,j; for(i = 0;i < digit;i++) { if(bit[i] == 1 && bit[i + 1] == 0) { bit[i] = 0; bit[i + 1] = 1; break; } if(bit[i] == 1) count++;//??? } if(bit[digit - 1] == 0 && bit[digit] == 1) digit++;//这一步什么意思??? for(j = 0;j != count;j++)//?? bit[j] = 1; for(j = count;i != j;j++)//?? bit[j] = 0; int result = 0; for(i = digit - 1;i >= 0;i--)//计算结果 { result *= 2; result += bit[i]; } cout << result << endl; Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator