0%

HDU5491 The Next

2018年8月19日 下午3:54

2015 ACM/ICPC合肥网赛&HDU5491 The Next - CSDN博客

这个程序中对for-if同时要处理三种情况:使用了continue+break。

  1. 主要是学习这个for-if的复杂处理
  2. 这道题一个数组有两个要求,我一开始没有利用好a<L<b这个条件,既然有那就肯定有用,不用感觉都不对。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <cmath>
    using namespace std;
    const int maxn = 55;
    int T,a,b,num[maxn],sum[maxn],cases = 0;
    long long n,t;
    int main(){
    scanf("%d",&T);
    while(T--){
    memset(num,0,sizeof(num));
    memset(sum,0,sizeof(sum));
    scanf("%lld%d%d",&n,&a,&b);
    t=n;
    int all = 0,pos = 0;
    while(t){
    if(t&1) all++,num[pos]=1;
    t>>=1;pos++;
    }
    sum[pos-1]=num[pos-1];
    for(int i=pos-2;i>=0;i--)
    sum[i]=sum[i+1]+num[i];//这里我省了+

    for(int i=0;i<=pos;i++){
    if(!num[i]){
    int tempall = sum[i]+1;
    if(tempall>b) continue;//for-if中第一层处理:>b的情况
    long long ans = 0;
    int post = 0;
    while(tempall<a){//for-if中第二层处理:<b的情况
    ans += pow(2,post);
    post++;
    tempall++;
    }
    ans+=pow(2,i);
    for(int j=i+1;j<pos;j++)//for-if中第三层处理:我们要找最小的数,处理够了break就行了
    ans += num[j]*pow(2,j);
    printf("Case #%d: %lld\n",++cases,ans);
    break;
    }
    }
    }
    return 0;
    }