Sum up Time limit: 2 Seconds Memory Limit: 65536 KB
Vivid have stored a piece of private information, which consisted of a serial of integers in a secret number format. All the stored numbers is in the range [-63, 63]. So every number contains exactly 7 bits-the leftmost bit was the sign bit (0 for positive and 1 for negative), and all OT Her bits represent the absolute value of the number (e.g. 000000 stands for 0, 000001 stands for 1 and 111111 stands for 6 3). With the sign bit, 1000000 and 0000000 is considered to is equal, both of them stand for 0.
All the numbers has been pushed into 16-bits integers, which is, one 16-bits integer are enough to hold 2 numbers plus 2 bi TS of another number.
In this problem, you is given a serial of 16-bits integers, and you need to output the sum of these 7-bits integers.
Input:
There is multiple test cases. Each test case begins with an integer n (the number of 16-bits numbers, 0 <= n <= 7000, n was always a multiple of 7) . Then N 16-bits numbers follow, all of which is in the range [0, 65535]. A case with N =-1 denotes the end of input, which should is not being proceeded.
Output:
For each test case, the output an integer indicating the sum of these 7bits-integers.
Sample Input:
71 0 0 0 0 0 0765535 65535 65535 65535 65535 65535 65535-1
Sample Output:
32-1008
The key: is to give you some 16-bit number, these numbers in accordance with the word binary line, now let into 7 bits, every 7 bits read a number, so that the number of these numbers;
Code:
1#include <stdio.h>2 Const intmaxn=7010;3 intm[maxn],b[maxn* -];4 intMain () {5 intN;6 while(SCANF ("%d", &n), n!=-1){7 for(intI=0; i<n;i++) scanf ("%d", m+i);8 intsum=0;9 inttop=1;Ten for(intj=m-1; i>=0; i--){ One intt=0; A while(t< -){ -b[top]=m[i]&1; -top++;m[i]>>=1; t++; the } - } - intA=0; - for(intI=1; i<top;i++){ + if(i%7){ -a+=b[i]<< (i%7-1); + } A Else{ at if(B[i]) sum-=A; - Elsesum+=A; -A=0; - } - } -printf"%d\n", sum); in } - return 0; to}
zoj2729 Sum Up (analog)