Sum Up 2729 (进制模拟) 好题

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
//题意:
给你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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值