zoj2729 Sum Up(模拟)

Sum Up

Time Limit: 2 Seconds       Memory Limit: 65536 KB

Vivid has stored a piece of private information, which consisted of a serial of integers in a secret number format. All the stored numbers are in the range [-63, 63]. So every number contains exactly 7 bits - the leftmost bit is the sign bit (0 for positive and 1 for negative), and all other bits represent the absolute value of the number (e.g. 000000 stands for 0, 000001 stands for 1 and 111111 stands for 63). With the sign bit, 1000000 and 0000000 are considered to be equal, both of them stand for 0.

All the numbers have been pushed into 16-bits integers, that is, one 16-bits integer is enough to hold 2 numbers plus 2 bits of another number.

In this problem, you are given a serial of 16-bits integers, and you need to output the sum of these 7-bits integers.

Input:

There are multiple test cases. Each test case begins with an integer N (the number of 16-bits numbers, 0 <= N <= 7000, N is always a multiple of 7). Then N 16-bits numbers follow, all of which are in the range [0, 65535]. A case with N = -1 denotes the end of input, which should not be proceeded.

Output:

For each test case, output an integer indicating the sum of these 7bits-integers in a single line.

Sample Input:

7
1 0 0 0 0 0 0
7
65535 65535 65535 65535 65535 65535 65535
-1

Sample Output:

32
-1008
题解:就是给你一些16位的数,这些数字按照二进制一字排开,现在让变成7位,每7位读取一个数,让求这些数字的和;
代码:
 1 #include<stdio.h>
 2 const int MAXN=7010;
 3 int m[MAXN],b[MAXN*20];
 4 int main(){
 5     int N;
 6     while(scanf("%d",&N),N!=-1){
 7         for(int i=0;i<N;i++)scanf("%d",m+i);
 8         int sum=0;
 9         int top=1;
10         for(int i=N-1;i>=0;i--){
11             int t=0;
12             while(t<16){
13                 b[top]=m[i]&1;
14                 top++;m[i]>>=1;t++;
15             }
16         }
17         int a=0;
18         for(int i=1;i<top;i++){
19             if(i%7){
20                 a+=b[i]<<(i%7-1);
21             }
22             else{
23                 if(b[i])sum-=a;
24                 else sum+=a;
25                 a=0;
26             }
27         }
28         printf("%d\n",sum);
29     }
30     return 0;
31 }

 

转载于:https://www.cnblogs.com/handsomecui/p/4841256.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值