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

有两步没看懂,看懂的解释一下

Posted by 771385494 at 2012-08-01 14:18:33 on Problem 2453 and last updated at 2012-08-01 14:21:30
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:
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