黑龙江大学程序设计竞赛(重现赛)K - Maximum Sum of Digits(贪心)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sugarbliss/article/details/89526497

题目链接:https://ac.nowcoder.com/acm/contest/877/K

思路:贪心找到不大于x的数中含有9的数越多越好。

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5+7;
ll solve(ll x)
{
    ll ans = 0;
    while(x)
    {
        ans += x % 10;
        x /= 10;
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll x; scanf("%lld",&x);
        ll y = 0;
        while(y <= x) y = y * 10 + 9;
        y /= 10;
        printf("%lld\n",solve(y)+solve(x-y));
    }
}

猜你喜欢

转载自blog.csdn.net/sugarbliss/article/details/89526497