zoukankan      html  css  js  c++  java
  • 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
    ",sum);
    29     }
    30     return 0;
    31 }
  • 相关阅读:
    Javascript事件处理程序的3种方式
    JS原生AJAX
    所谓的渐进增强,优雅降级?
    1059 老师的苦恼
    HTML5 参数传递
    HDU 5289 Assignment(二分+RMQ-ST)
    HDU 3333 Turing Tree(离线树状数组)
    mac下 mysql 插入中文乱码解决
    校园商铺-7商品类别模块-5商品类别删除后端开发
    校园商铺-7商品类别模块-3商品类别添加后端开发
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4841256.html
Copyright © 2011-2022 走看看