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 ... 
随机推荐
- linux jdk环境变量配置
			export JAVA_HOME=/home/faunjoe/java/jdk1.8.0_45export PATH=$JAVA_HOME/bin:$PATHexport CLASSPATH=.:$J ... 
- HTTP请求方式中8种请求方法(简单介绍)
			简单介绍 HTTP是超文本传输协议,其定义了客户端与服务器端之间文本传输的规范.HTTP默认使用80端口,这个端口指的是服务端的端口,而客户端使用的端口是动态分配的.当我们没有指定端口访问时,浏览器会 ... 
- 【洛谷P3388】(模板)割点
			[模板]割点 割点集合:一个顶点集合V,删除该集合的所有定点以及与这些顶点相连的边后,原图不连通,就称集合V为割点集合 点连通度:最小割点集合中的顶点数 边连通度:最小割边集合中的边数 割点:割点集合 ... 
- Python—面向对象02
			1.抽象类与归一化  接口,即提供给使用者来调用自己功能的方式.方法.入口 为什么要使用接口? 接口提取了一类共同的函数,可以把接口看做一个函数的集合 然后让子类去实现接口中的函数 这么做的意义在于 ... 
- 常用的linux指令整理
			ls 列出文件目录 -a全部文件,连同隐藏文件一起列出 -d仅列出目录本身,而不是列出目录内的文件数据 -l 连同权限一同列出 cd 切换文件目录的命令 pwd显示目前所在的目录 mkdir 创建新的 ... 
- linux下重新启动oracle
			第一步.以Oracle帐户进入Linux系统 第二步.执行以下命令查看数据库监听器的状况: lsnrctl status 或者查看数据库端口是否被监听(默认1521) netstat -ano | g ... 
- 核心动画(UIView封装动画)-转
			一.UIView动画(首尾) 1.简单说明 UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画支持. 执行动画所需要的工作由UIView类自动完成 ... 
- [USACO11OPEN]奶牛跳棋Cow Checkers(博弈论)
			题目描述 One day, Bessie decides to challenge Farmer John to a game of 'Cow Checkers'. The game is playe ... 
- 【TOJ 5065】最长连续子序列(前缀和)
			Description 给定一系列非负整数,求最长的连续子序列,使其和是7的倍数. Input 第一行为正整数N(1<=N<=50000),接下来有N行,每行有一个非负整数,所有整数不大于 ... 
- 微信小程序navigator无法跳转情况
			情况有三种 跳转的页面没有在app.json中注册 跳转的路径不正确 以上两种在命令行(console)中都会提示 跳转的页面在TabBar中,需要将open-type属性是设置为switchTab 
