1. 程式人生 > >codeforces 1060b Maximum Sum of Digits(思維題)

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)/~