zoukankan      html  css  js  c++  java
  • Gym

    Decoding of Varints

    题意&思路: 首先根据红色边框部分的公式算出x,再有绿色部分得知,如果x是偶数则直接除以2,x是奇数则(x+1)/-2。

    PS:这题有数据会爆掉unsigned long long,就是在最后奇数转换的时候。所以转换的时候可以变公式为-((x-1)/2+1)。

    代码:

     1 #include <iostream>
     2 #include <queue>
     3 #include <cstdio>
     4 #include <algorithm>
     5 #include <cmath>
     6 #include <cstring>
     7 #define INF 0x3f3f3f3f
     8 
     9 using namespace std;
    10 typedef unsigned long long ll;
    11 const int maxn = 11000;
    12 ll buf[maxn];
    13 
    14 
    15 int main(){
    16     int n;
    17     scanf("%d",&n);
    18     for(int i=0; i<n; i++){
    19         scanf("%llu",&buf[i]);
    20     }
    21     for(int i = 0; i<n; i++){
    22         int j = i;
    23         ll sum = 0,base = 1;
    24         while(j<n){
    25             if(buf[j]>=128){
    26                 sum += (buf[j]-128)*base;
    27                 base*=128;
    28                 j++;
    29             }
    30             else{
    31                 sum = sum + buf[j]*base;
    32                 i = j;
    33                 break;
    34             }
    35         }
    36         if(sum&1)
    37             printf("%lld
    ",(long long)-((sum-1)/2+1));
    38         else
    39             printf("%llu
    ",sum/2);
    40     }
    41     return 0;
    42 }
    View Code
  • 相关阅读:
    Python解释器【转载】
    Python第一行代码
    Hive安装部署
    Python 3.6安装教程
    Spark安装部署
    Code:Blocks中文输出乱码解决方法
    HBase集群安装部署
    Hadoop集群时间同步
    ZooKeeper安装部署
    Linux重置mysql密码
  • 原文地址:https://www.cnblogs.com/sykline/p/9737887.html
Copyright © 2011-2022 走看看