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

Re:网上找的代码,路过的看看吧,服了...

Posted by angeldust at 2012-01-11 14:37:42 on Problem 2453
In Reply To:网上找的代码,路过的看看吧,服了... Posted by:vortex101 at 2009-02-26 13:51:45
> #include <stdio.h>
> int main()
> {
>     int n,x;
>     while(scanf("%d",&n),n)
>     {
>         x=n&-n;
>         printf("%d\n",n+x+(n^n+x)/x/4);
>     }
> }

int n,x,y;
while (scanf("%d",&n)&&n)
{
	x=n&-n;
	n+=x;
	y=n&-n;
	n+=y/x/2-1;
	printf("%d\n",n);
}
其实原理都是把n的最低位的1左移一位,如果它前面有a个连续的1,就会产生a次连续进位,相当于最后的结果少了a个1,那么把这a个1加到最后就行了

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