Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

很简单的一道题嘛,直接暴力算就过了(440K 0MS),好像用位运算的效果会更好,不管了,反正是过了,贴上自己的代码喽

Posted by bupt_chen at 2008-07-09 15:21:22 on Problem 2453
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator