CodeForces 1060 B Maximum Sum of Digits

Maximum Sum of Digits

  You are given a positive integer n.

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

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

Input

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

Output

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

Examples

Input
35
Output
17
Input
10000000000
Output
91

Note

  In the first example, you can choose, for example, a=17a=17 and b=18b=18 , so that S(17)+S(18)=1+7+1+8=17S(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=5000000001a=5000000001 and b=4999999999b=4999999999 , with S(5000000001)+S(4999999999)=91S(5000000001)+S(4999999999)=91 . It can be shown that it is impossible to get a larger answer.

解题思路:
  给出一个数字n,将他拆分为2个数字,使拆分的两个数字每一位相加的和最大,输出相加后的和。

  个人感觉不用管下面提示。我们本着贪心的思想,希望获得尽可能多的9,就是将35,拆分为9与26,将10000000000,拆分为9999999999与1,既将原始数字拆分为比其第一位的全由9组成的数字与另一个补偿数字,补偿数字为原始数字减去拆分的全9数字。之后将拆分的两个数字所有位都相加便可以得到答案。

 1 #include<iostream>
 2 #include<sstream>
 3 #include<string>
 4 #include<cstdio>
 5 //CodeForces不支持万能头文件bits/stdc++.h
 6 using namespace std;
 7 typedef long long ll;
 8 string n;
 9 ll power(int a, int b){ //快速幂
10     ll ans = 1;
11     while(b){
12         if(b & 1){
13             ans = ans * a;
14         }
15         a = a * a;
16         b >>= 1;
17     }
18     return ans;
19 }
20 int main()
21 {
22     ll a, b;
23     while(cin >> n)
24     {
25         int suma=0;
26         int sumb=0;
27         a = power(10, n.size() - 1);
28         //若想拆分出小于且9最多的数字,只需要找到n的位数n.size() - 1
29         //之后便可以用10的n.size() - 1次幂找到与n位数相等数字中最小数字
30         //减一便可以得到我们所要拆分的数字
31         istringstream cinn(n);
32         cinn >> b;
33         //先用istringstream读取n中的值输入到整形b中
34         //b - a就是补偿的数字。
35         a--;
36         b = b - a;
37         while(a)    //将a的每一位加起来
38         {
39             suma += a % 10;
40             a/=10;
41         }
42         while(b)
43         {
44             sumb += b % 10; //将b的每一位加起来
45             b/=10;
46         }
47         cout << suma + sumb << endl;    //所有位数加和
48     }
49     return 0;
50 }
原文地址:https://www.cnblogs.com/suvvm/p/10054826.html