C

C - Johnny and Another Rating Drop

这道题的题意看下面的注释应该清楚了,这里也不描述了(绝对不是我不知道这么描述QAQ)

题解:这道题一开始不知道怎么做,然后列了几个数据就明了了

000000 
000001 1                          1
000010 1+2                        3
000011 1+2+1                      4
000100 1+2+1+3                    7
000101 1+2+1+3+1                  8
000110 1+2+1+3+1+2                10
000111 1+2+1+3+1+2+1              11
001000 1+2+1+3+1+2+1+4            15
001001 1+2+1+3+1+2+1+4+1          16
001010 1+2+1+3+1+2+1+4+1+2       18
001011 1+2+1+3+1+2+1+4+1+2+1
001100 1+2+1+3+1+2+1+4+1+2+1+3
001101 1+2+1+3+1+2+1+4+1+2+1+3+1
001110 1+2+1+3+1+2+1+4+1+2+1+3+1+2
001111 1+2+1+3+1+2+1+4+1+2+1+3+1+2+1
010000 1+2+1+3+1+2+1+4+1+2+1+3+1+2+1+5
010001 1+2+1+3+1+2+1+4+1+2+1+3+1+2+1+5+1



if(n%2==0)sum+=n/2*cnt;
else sum+=(n+1)/2*cnt;

这几组我们不难发现 奇数位上都是1,然后将这些1删去,奇数位上全是2,把2删去,奇数位上全是3……

所以公式为

if(n%2==0)sum+=n/2*cnt;
else sum+=(n+1)/2*cnt;//cnt 为删去的次数

代码如下
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
      LL n;
      scanf("%lld", &n);
      LL k = n;
      LL sum = 0;
      LL cnt = 1;
      while(k!=0){
         if(k%2==0)
             sum += cnt * (k / 2);
         else
             sum += cnt * ((k + 1) / 2);
         k = k / 2;
         cnt++;
      }
      printf("%lld
", sum);
    }

}
 
原文地址:https://www.cnblogs.com/kitalekita/p/13229903.html