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

1 篇文章 0 订阅
1 篇文章 0 订阅

传送门: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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值