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 |
很简单的一道题嘛,直接暴力算就过了(440K 0MS),好像用位运算的效果会更好,不管了,反正是过了,贴上自己的代码喽#include <iostream> using namespace std; int main() { int n; 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; 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(int j=0;j!=count;j++) bit[j]=1; for(int j=count;i!=j;j++) bit[j]=0; int result=0; for(int i=digit-1;i>=0;i--) { result*=2; result += bit[i]; } cout << result << endl; } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator