Maximum Sum of Digits(CodeForces 1060B)

Description

You are given a positive integer nn.

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

Your task is to find two integers a,ba,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(1n1012).

Output

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

Sample Input

Input
35
Output
17
Input
10000000000
Output
91

Hint

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.

相信不少人wa是这个代码

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<string>
 6 #include<cmath>
 7 #include<map>
 8 #include<stack>
 9 #include<vector>
10 #include<queue>
11 #include<set>
12 #include<algorithm>
13 #define max(a,b)   (a>b?a:b)
14 #define min(a,b)   (a<b?a:b)
15 #define swap(a,b)  (a=a+b,b=a-b,a=a-b)
16 #define maxn 320007
17 #define N 100000000
18 #define INF 0x3f3f3f3f
19 #define mod 1000000009
20 #define e  2.718281828459045
21 #define eps 1.0e18
22 #define PI acos(-1)
23 #define lowbit(x) (x&(-x))
24 #define read(x) scanf("%d",&x)
25 #define put(x) printf("%d
",x)
26 #define memset(x,y) memset(x,y,sizeof(x))
27 #define Debug(x) cout<<x<<" "<<endl
28 #define lson i << 1,l,m
29 #define rson i << 1 | 1,m + 1,r
30 #define ll long long
31 //std::ios::sync_with_stdio(false);
32 //cin.tie(NULL);
33 using namespace std;
34 
35 
36 int main()
37 {
38     ll n,a,b;
39     cin>>n;
40     ll m=n,k=9;
41     while(m>=k)
42     {
43         k=k*10+9;
44     }
45     k/=10;
46     //cout<<k<<endl;
47     a=k;
48     b=n-k;
49     ll sum=0;
50     while(a)
51     {
52         sum+=a%10;
53         a/=10;
54     }
55     while(b)
56     {
57         sum+=b%10;
58         b/=10;
59     }
60     cout<<sum<<endl;
61     return 0;
62 }

你们可以尝试一下123这个样例,输出为15,但正确答案为24(99+24)。

AC代码如下:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<string>
 6 #include<cmath>
 7 #include<map>
 8 #include<stack>
 9 #include<vector>
10 #include<queue>
11 #include<set>
12 #include<algorithm>
13 #define max(a,b)   (a>b?a:b)
14 #define min(a,b)   (a<b?a:b)
15 #define swap(a,b)  (a=a+b,b=a-b,a=a-b)
16 #define maxn 320007
17 #define N 100000000
18 #define INF 0x3f3f3f3f
19 #define mod 1000000009
20 #define e  2.718281828459045
21 #define eps 1.0e18
22 #define PI acos(-1)
23 #define lowbit(x) (x&(-x))
24 #define read(x) scanf("%d",&x)
25 #define put(x) printf("%d
",x)
26 #define memset(x,y) memset(x,y,sizeof(x))
27 #define Debug(x) cout<<x<<" "<<endl
28 #define lson i << 1,l,m
29 #define rson i << 1 | 1,m + 1,r
30 #define ll long long
31 //std::ios::sync_with_stdio(false);
32 //cin.tie(NULL);
33 using namespace std;
34 
35 
36 int main()
37 {
38     ll n,a,b;
39     cin>>n;
40     ll m=n,k=9;
41     while(m>=k)
42     {
43         k=k*10+9;
44     }
45     k/=10;
46     //cout<<k<<endl;
47     a=k;
48     b=n-k;
49     ll sum=0;
50     while(a)
51     {
52         sum+=a%10;
53         a/=10;
54     }
55     while(b)
56     {
57         sum+=b%10;
58         b/=10;
59     }
60     cout<<sum<<endl;
61     return 0;
62 }
View Code
原文地址:https://www.cnblogs.com/baiyi-destroyer/p/9745335.html