CodeForces - 1060B Maximum Sum of Digits (思维。)

B. Maximum Sum of Digits

You are given a positive integer n.

Let S(x) be sum of digits in base 10 representation of x, for example, S(123)=1+2+3=6, S(0)=0.

Your task is to find two integers a,b, such that 0≤a,b≤n, a+b=n and S(a)+S(b) is the largest possible among all such pairs.

Input
The only line of input contains an integer n (1≤n≤1012).

Output
Print largest S(a)+S(b) among all pairs of integers a,b, such that 0≤a,b≤n and a+b=n.

Examples
input
35
output
17


input
10000000000
output
91


Note
In the first example, you can choose, for example, a=17 and b=18, so that S(17)+S(18)=1+7+1+8=17. It can be shown that it is impossible to get a larger answer.

In the second test example, you can choose, for example, a=5000000001 and b=4999999999, with S(5000000001)+S(4999999999)=91. It can be shown that it is impossible to get a larger answer.

 

题意:告诉你 s(x) = x 这个数的各位位数之和。 如 s(123) = 1 + 2 + 3 = 6.  现在给你一个整数n, 要求你在 0 到 n  的范围内找两个数a 和 b 。是 a + b == n && s(a) + s(b) 最大;

思路:对于一个 数 x ,如果 x <= 18, 那么 将 x 分成两个数a, b  ..  s(a) + s(b) 就是等于 x 的。 所以我们可以对 一个数 x 的每一位进行计算,  如果当前位 是 9,那么 ans += 9, 如果不是,就向前一位借 1, 然后 ans += 10 + 当前位。

AC代码:

#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define INT(t) int t; scanf("%d",&t)
#define LLI(t) LL t; scanf("%I64d",&t)

using namespace std;

int main()
{
    string n;
    while(cin >> n){
        int a[15] = {0};
        int p = 0;
        for(int i = 0;i < n.size();i ++)
            a[p ++] = n[i] - '0';

        LL ans = 0;
        for(int i = p - 1;i >= 0;i --){
            //debug(ans); debug(a[i]);
            if(a[i] < 0){
                a[i] += 10;
                a[i - 1] --;
            }
            if(a[i] == 9)
                ans += 9;
            else {
                if(i != 0){
                    ans += 10 + a[i];
                    a[i - 1] --;
                }
                else ans += a[i];
            }
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值