cf#513 B. Maximum Sum of Digits
B. Maximum Sum of Digits
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
You are given a positive integer nn.
Let S(x)S(x) be sum of digits in base 10 representation of xx, for example, S(123)=1+2+3=6S(123)=1+2+3=6, S(0)=0S(0)=0.
Your task is to find two integers a,ba,b, such that 0≤a,b≤n0≤a,b≤n, a+b=na+b=n and S(a)+S(b)S(a)+S(b) is the largest possible among all such pairs.
Input
The only line of input contains an integer nn (1≤n≤1012)(1≤n≤1012).
Output
Print largest S(a)+S(b)S(a)+S(b) among all pairs of integers a,ba,b, such that 0≤a,b≤n0≤a,b≤n and a+b=na+b=n.
Examples
input
Copy
35
output
Copy
17
input
Copy
10000000000
output
Copy
91
Note
In the first example, you can choose, for example, a=17a=17 and b=18b=18, so that S(17)+S(18)=1+7+1+8=17S(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=5000000001a=5000000001 and b=4999999999b=4999999999, with S(5000000001)+S(4999999999)=91S(5000000001)+S(4999999999)=91. It can be shown that it is impossible to get a larger answer.
题意:给出一个n,需要a和b,两个数,使得a+b=n且a与b各位上的数加起来和最大
样例会让人误解 其实可以其中一个都又9组成,这样各位数上的和最后就会是最大的。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int cnt(ll a)
{
int sum=;
while(a)
{
sum+=a%;
a/=;
}
return sum;
}
ll judge(ll a)
{
ll b=;
while(b<=a)
{
if(b*+>=a)break;
else b=b*+;
}
return b; }
int main()
{ ll n;
cin>>n;
if(n<=)cout<<n<<endl;
else
{
int ans=;
ans+=cnt(judge(n))+cnt(n-judge(n));
cout<<ans<<endl;
}
return ;
}
cf#513 B. Maximum Sum of Digits的更多相关文章
- CodeForces 1060 B Maximum Sum of Digits
Maximum Sum of Digits You are given a positive integer n. Let S(x)S(x) be sum of digits in base 10 r ...
- Maximum Sum of Digits(CodeForces 1060B)
Description You are given a positive integer nn. Let S(x) be sum of digits in base 10 representation ...
- CF1060B:Maximum Sum of Digits
我对贪心的理解:https://www.cnblogs.com/AKMer/p/9776293.html 题目传送门:http://codeforces.com/problemset/problem/ ...
- Codeforces_B.Maximum Sum of Digits
http://codeforces.com/contest/1060/problem/B 题意:将n拆为a和b,让a+b=n且S(a)+S(b)最大,求最大的S(a)+S(b). 思路:考虑任意一个数 ...
- CF 276C Little Girl and Maximum Sum【贪心+差分】
C. Little Girl and Maximum Sum time limit per test2 seconds memory limit per test256 megabytes input ...
- CodeForces 489C Given Length and Sum of Digits... (贪心)
Given Length and Sum of Digits... 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/F Descr ...
- Codeforces Round #277.5 (Div. 2)C——Given Length and Sum of Digits...
C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...
- codeforces#277.5 C. Given Length and Sum of Digits
C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...
- CodeForces 489C Given Length and Sum of Digits... (dfs)
C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...
随机推荐
- [18/11/29] 继承(extends)和方法的重写(override,不是重载)
一.何为继承?(对原有类的扩充) 继承让我们更加容易实现类的扩展. 比如,我们定义了人类,再定义Boy类就只需要扩展人类即可.实现了代码的重用,不用再重新发明轮子(don’t reinvent w ...
- [18/11/7] Java的基础概念
java语言的优势是跨平台 ,计算机界的英语,是IT行业的第一大语言 特点是多线程 分布式 健壮性 面向对象 java和JavaScript的关系 雷锋和雷峰塔的关系 或卡巴斯基和巴基斯坦的关系有个基 ...
- labelme也可以标注polygan
https://blog.csdn.net/wc781708249/article/details/79595174 按照这个安装就可以了 sudo apt-get install python-qt ...
- [luoguP1090][Noip2004]合并果子
合并果子 首先来看一下题目: (OI2004合并果子) [题目描述] 果园里,多多已经将所有的果子打了下来,而且按果子的 ...
- 消息中间件JMS(三)
1. Spring整合JMS 1.1消息生产者 创建工程springJMS_producer,并在pom文件中引入SpringJms .activeMQ以及单元测试相关依赖 <propertie ...
- unity3D引擎:2D游戏自动瞄准算法实现
转:http://blog.csdn.net/naitu/article/details/39555373 在很多飞行射击类游戏里,都有敌人向玩家自动瞄准并开火的功能.在这里本人用unity3D引擎新 ...
- webpack4——打包html报错解决
①先引入html-webpack-plugin插件,然后在终端下载 npm install --save-dev html-webpack-plugin ②我的文件结构 ③修改webpack.dev. ...
- Controller类的方法上的RequestMapping一定要写在Controller类里吗?
转载请标明出处: https://blog.csdn.net/forezp/article/details/80069961 本文出自方志朋的博客 使用Spring Cloud做项目的同学会使用Fei ...
- 使用js获取表单元素的值
function getParams(formName) { var frmMain = document.getElementById(formName)?document.getElementBy ...
- 【JAVA】学习笔记
对程序员来说,学习一门新的语言,最开始的代码当然是hello world!下面我们从这段代码入手,一步一步来学习Java基础知识. class Hello{ public static void ma ...