日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

【 CodeForces - 1060B 】Maximum Sum of Digits(思维,构造)

發(fā)布時(shí)間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【 CodeForces - 1060B 】Maximum Sum of Digits(思维,构造) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題干:

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

35

Output

17

Input

10000000000

Output

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.

題目大意:

? ? 給一個(gè)n,找兩個(gè)數(shù)a,b,使得在滿足a+b=n的前提下,a和b的各位數(shù)的和最大。

解題報(bào)告:

? ? ?這題有點(diǎn)小坑啊,但是這么想啊,肯定是9越多越好,所以先找出最多的9999之類的,剩下的再隨便挑。(因?yàn)椴浑y想到,肯定沒有比這個(gè)的和? 更大的了)所以就這么構(gòu)造就行,反正是SJ題。

? ?給一個(gè)錯(cuò)誤的想法,up=1,然后每次都*10+9,找到最大的滿足的,這樣就大錯(cuò)特錯(cuò)了。對(duì)于123這個(gè)樣例,輸出為15,但正確答案為24(99+24)。所以啊要先湊9,因?yàn)榭梢宰C明其他的任何最大組合,都可以由這個(gè)a來借給b。

AC代碼:

#include<bits/stdc++.h> #define ll long long using namespace std;int main() {ll n,a,b;cin>>n;ll up=1;while(n>=up) {up=up*10;}up/=10;up--; // cout<<up<<endl;a=up;b=n-up;ll sum=0;while(a) {sum+=a%10;a/=10;}while(b) {sum+=b%10;b/=10;}printf("%d\n",sum);return 0; }

其實(shí)寫成更賞心悅目,思路清晰。

while(1){temp1*=10;if(temp1*10>=n)break;}temp1--;

?

總結(jié)

以上是生活随笔為你收集整理的【 CodeForces - 1060B 】Maximum Sum of Digits(思维,构造)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。