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 -1Sample Output:
32 -1008
//题意:
给你7*n个数,每个数都要用16位的二进制数表示,并将这些数的所有位数都存放到一块,然后每7位取一个数,这个
7位的二进制数的首位是符号位,若为1则其表示负号“-”,若为0则表示正号“+”,算出所有这些7位数的和。
#include<stdio.h> #include<string.h> int a[7100],b[7100*20]; int main() { int n,m,i,j; while(scanf("%d",&n),n!=-1) { for(i=1;i<=n;i++) scanf("%d",&a[i]); int top=1; int sum=0; for(i=n;i>=1;i--) { int t=0; while(t<16) { b[top]=a[i]&1; top++; a[i]>>=1; t++; } } m=0; for(i=1;i<top;i++) { if(i%7) m+=b[i]<<(i%7-1); else { if(b[i]) sum-=m; else sum+=m; m=0; } } printf("%d\n",sum); } return 0; }