51Nod 1596 搬货物

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1596

思路:

模拟二进制的进位。

这题很坑啊...用c++会超时,用c就行了...

 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 const int maxn=1e6+1000;
 5 
 6 int w[maxn];
 7 
 8 int main()
 9 {
10     //freopen("D:\input.txt","r",stdin);
11     int n;
12     while(scanf("%d",&n)!=EOF)
13     {
14         memset(w,0,sizeof(w));
15         int x;
16         while(n--)
17         {
18             scanf("%d",&x);
19             w[x]++;        }
20         int sum=0;
21         for(int i=0;i<maxn;i++)
22         {
23             if(w[i]>1)
24             {
25                 w[i+1]+=w[i]/2;
26                 w[i]%=2;
27             }
28             if(w[i]==1)  sum++;
29         }
30         printf("%d
",sum);
31     }
32     return 0;
33 }
原文地址:https://www.cnblogs.com/zyb993963526/p/6764088.html