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 ...
随机推荐
- ASP.NET MVC有用工具
Route Debugger https://www.nuget.org/packages/routedebugger 在Asp.Net MVC程序中,路由(Route)是一个非常核心的概念,可以说是 ...
- 关于eclipse中引入项目报错或者没有JRE System Library问题(jre报错)或者jre1.7(8)改为jre1.8(7)等问题
解决方法: 右键项目工程-->>properties->>java bulid path -->>>libraries -->>add libra ...
- HDU 2005 第几天?(闰年判断)
传送门: acm.hdu.edu.cn/showproblem.php?pid=2005 第几天? Time Limit: 2000/1000 MS (Java/Others) Memory L ...
- sql树形查询
sql: 使用Common As:递归公用表 https://docs.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expressi ...
- Access用OleDbParameter更新/插入数据
/// <summary> /// 更新一条数据 /// </summary> public void Update(ZPY.Model.News model) { Strin ...
- 上传文件,经过Zuul,中文文件名乱码解决办法
转载请标明出处: http://blog.csdn.net/forezp/article/details/77170470 本文出自方志朋的博客 问题描述 在项目中又一个上传文件的oss服务,直接调用 ...
- Linux_vsftpd服务配置
首先安装Linux 企业版第一张光盘中的vsftpd-2.0.1-5.i386.rpm#rpm –ivh /media/cdrom/RedHat/RPMS/vsftpd-3.0.1-5.i386.rp ...
- Flask—03-bootstrap与表单
bootstrap与表单 Bootstrap是美国Twitter公司的设计师Mark Otto和Jacob Thornton合作基于HTML.CSS.JavaScript 开发的简洁.直观.强悍的前端 ...
- 转载:Python中的if __name__ == '__main__'
刚开始学习Python时,对于有些书出现的函数带有“if __name__ == '__main__'”总是迷惑不解,比如<dive into Python>中开头的哪个根据输入的数字计算 ...
- iOS:GCD理解1(串行-并行、同步-异步)
1.获取并行.创建串行 队列 1-1).获取 并行(全局) 队列 ,DISPATCH_QUEUE_PRIORITY_DEFAULT 为默认优先级. dispatch_queue_t global_qu ...