2012年四月份ACM月赛 problem A

Problem A

flip

时间限制:1000 ms  |  内存限制:65535 KB
 
描述
Give you a non-negative integer x and an operation. The only operation you can do is to reverse one bit in binary form of x 
 
once(i.e 1->0, 0->1).
your goal is to turn x into x+1.
Calculate the minimum times of operations you need to do. 
 
输入
The first line of the input is an integer T indicates the test cases.
Then follow T lines. Each line is a non-negative integer x as described above, note that 0<=x<10^9.
输出
Output the minimum times of operations you need to do to reach the goal.
样例输入
3
1
2
3
样例输出
2
1
3

#include"stdio.h"
int main(void)
{
    int a[10000],b[10000];
    int n,m,i,j,p,q,k,t;
    scanf("%d",&t);
    while(t--)
    {
      p = q = i = j = k = 0;
 scanf("%d",&n);
 m = n+1;
   while(n)
 {
  a[i] = n%2;
  n = n/2;
  i++;
 }   p = i;
   while(m)
 {
  b[j] = m%2;
  m = m/2;
  j++;
 }   q = j;

   if(p < q)
   {
        k++;
         for(i = 0;i < p; i++)
             {
                   if(a[i] != b[i])
                   {k++;}
             }
        }
   if(p == q){
             for(i = 0;i < q; i++)
             {
                   if(a[i] != b[i])
                   {k++;}
             }
       }
   printf("%d\n",k);
}
 return 0;
}

posted @ 2012-04-22 18:26  牧童不吃蛋炒饭  阅读(199)  评论(0)    收藏  举报