codeforces 1060b 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

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
inputCopy
35
outputCopy
17
inputCopy
10000000000
outputCopy
91
Note
In the first example, you can choose, for example, 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, witS(5000000001)+S(4999999999)=91. It can be shown that it is impossible to get a larger answer.

要求求一个数n的两个可以加和为n的数的各个位上数字加和的最大值。。
像这个题,我们要做的就是多出9。。先求不大于n的10的幂次数,然后再减一。例如:101,不大于它的数是100,100-1=99。这样就有了两个9。思路就是这个。
代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;

ll n;
ll temp1;
ll temp2;

int main()
{
	while(scanf("%I64d",&n)!=EOF)
	{
		int sum1=0;
		int sum2=0;
		temp1=1;
		while(1)
		{
			temp1*=10;
			if(temp1*10>=n)
			break;
		}
		temp1--;
		temp2=n-temp1;
		while(temp1)
		{
			sum1+=temp1%10;
			temp1/=10;
		}
		while(temp2)
		{
			sum2+=temp2%10;
			temp2/=10;
		}
		cout<<sum1+sum2<<endl;
	}
	return 0;
}

努力加油a啊,(o)/~

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/83002074
今日推荐