Codeforces Round #513 by Barcelona Bootcamp , problem: (B) Maximum Sum of Digit

传送门:http://codeforces.com/contest/1060/problem/B

       这题没咋分析,是纯打表然后猜的规律,觉得9越多越好,那么就构造一个比n小的数,除开最高位之外全是9,那么另一个数就是n减去这个数了,然后求和,这个就是最大的sum值

代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
inline int read(){
    int f = 1, x = 0;char ch = getchar();
    while (ch > '9' || ch < '0'){if (ch == '-')f = -f;ch = getchar();}
    while (ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * f;
}
int main(){
    LL n,now,len = 0,st = 0;
    cin >> n; now = n;
    while (now > 0) {
        if (now / 10 == 0) st = now;
        now /= 10;len++;
    }
    LL num1 = st-1;
    for (int i = 1; i < len; ++i) {
        num1 = num1*10 + 9;
    }
    LL ans1 = num1,ans2 = n - num1,sum = 0;
    while(ans1 > 0){
        sum += ans1%10;
        ans1 /= 10;
    }
    while(ans2 > 0){
        sum += ans2%10;
        ans2 /= 10;
    }
    cout << sum << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/CCCCTong/article/details/82942716