传送门
题目:就是让你把n分成a和b,且a和b位数上的各权值相加要得到最大。。。。。
思路:就是先求出n的位数x,然后令a等于x-1位的9,如果n的最高位大于1,则在a的最前面加1,b = n - a;最后求出sum即可。
例如:n = 202, 则 a = 199, b = 202-199 = 3,所以,sum = 22;
注意:一定要用long long,否则会红的。
//#include<bits/stdc++.h>
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<stack>;
using namespace std ;
typedef long long ll;
#define MAXN 10005*5
#define INF 0x3f3f3f3f
#define MALL (BiTnode *)malloc(sizeof(BiTnode));
int main()
{
int t;
cin >> t;
while(t--)
{
ll a, sum=0;
cin >> a;
ll x=0, tem=0, ans=1;
ll a2 = a;
while(a2)
{
x++;
if(a2/10 == 0)
tem = a2%10;
a2/=10;
}
if(tem!=1)
{
ans=tem-1;
x = x-1;
}
else
{
ans=9;
x = x-2;
}
sum+=ans;
for(int i=0; i<x; ++i)
{
sum+=9;
ans*=10;
ans+=9;
}
ll a1 = a-ans;
// cout << x << ' ' << tem << ' ' << ans << ' ' << a1 << '\n';
while(a1)
{
sum += a1%10;
a1/=10;
}
cout << sum << '\n';
}
return 0;
}