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

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

AC代码如下:

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

View Code

 

版权声明:本文为baiyi-destroyer原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/baiyi-destroyer/p/9745335.html