题目链接:https://ac.nowcoder.com/acm/contest/877/K
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
输入描述
输出描述
输入
2
35
1000000000
输出
17
82
说明
In the first example, you can choose, for example,a = 17 andb = 18, so thatS(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 = 500000001 andb = 499999999, withS(500000001) + S(499999999) = 82. It can be shown that it is impossible to get a larger answer.
解题思路
题意:将n分成两个数,使得两个数每一位的数字加起来最大.
思路:将n分离成最高项和其余项两项,然后把最高项的减一,另一项加一,使其出现最多的9。
例如:n=359=300+59=299+60, 则s(299)+s(60)=2+(3-1)*9+6+0=26;其中(3-1)指的是最高位后有多少个9.
n=99=90+9=89+10, 则s(89)+s(10)=8+(2-1)*9+1+0=18;
n=2568=2000+568=1999+569, 则s(1999)+s(569)=1+(4-1)*9+5+6+9=48。
#include <bits/stdc++.h>
using namespace std;
int main() {
char s[15];
int ans, len, c, t;
scanf("%d", &t);
while (t--) {
c = 1;
ans = 0;
scanf("%s", s);
len = strlen(s);
ans += s[0] - '0' + (len - 1) * 9 - 1;
for (int i = len - 1; i > 0; i--) {
s[i] += c;
c = (s[i] - '0') / 10;
s[i] = (s[i] - '0') % 10 + '0';
ans += s[i] - '0';
}
printf("%d\n", ans + c);
}
return 0;
}