codeforces 1060 B

本文解析了一个算法竞赛题目,目标是找到两个数A和B,使A+B等于给定数C,同时A的位数和达到最大。通过确定接近C的最大10的幂次倍数,减一获取最多9的个数,进而分解得到A和B,最后计算并输出A和B的位数和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://codeforces.com/contest/1060/problem/B

题意:给你一个数C ,你要找到两个数A、B,使得A+B=C并且A的每个位的数的和最大,求最大的和是多少

题解:肯定是9最大了,那么我们就要找最多有多少个9

   1.先找离这个数最大的10的幂次倍是多少,比如101最大的是100

   2.将离他最大的数减1即可得到最多的9的个数

   3.将这个数减去已经数过的9的那个数,得到另一个数,分解即可

代码如下:

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define PI acos(-1)
#define eps 1e-8
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int maxn = 1e5+5;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,LL b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}

int main(){
#ifndef ONLINE_JUDGE
    FIN
#endif
    LL n;
    cin>>n;
    LL m=n;
    LL cnt=0;
    LL tmp=1;
    while(m>9){
        tmp*=10;
        cnt++;
        m/=10;
    }
    tmp--;
    LL t1=tmp;
    LL t2=n-t1;
    LL ans=0;
    while(t1){
        ans+=t1%10;
        t1/=10;
    }
    while(t2){
        ans+=t2%10;
        t2/=10;
    }
    cout<<ans<<endl;


}
View Code

 

   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值