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

精简算法测试OK为何错呢?

Posted by sunnvya at 2006-09-18 20:54:39 on Problem 2453
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
int main()
{
    long n;int a[64];
    while(cin>>n&&n)
    {
                    memset(a,0,64);
                    int i=0,k=0;
                    while(n)
                    {
                            a[i++]=n%2;
                            n/=2;
                    }
                    for(int j=0;j<i;j++)
                    {
                            if(a[j]==1&&a[j+1]==0)
                            {a[j]=0;a[j+1]=1;break;}
                            if(a[j]==1&&a[j+1]==1)
                            {k++;a[j]=0;}
                    }
                    for(int j=0;j<k;j++)
                    a[j]=1;
                    long s=0;
                    for(int j=0;j<=i;j++)
                    {
                       s+=a[j]*pow(2,j);
                    }
                    cout<<s<<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