CF770-B-Maximize Sum of Digits

B. Maximize Sum of Digits
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Anton has the integer x. He is interested what positive integer, which doesn't exceed x, has the maximum sum of digits.

Your task is to help Anton and to find the integer that interests him. If there are several such integers, determine the biggest of them.

Input

The first line contains the positive integer x (1 ≤ x ≤ 1018) — the integer which Anton has.

Output

Print the positive integer which doesn't exceed x and has the maximum sum of digits. If there are several such integers, print the biggest of them. Printed integer must not contain leading zeros.

Examples
input
100
output
99
input
48
output
48
input
521
output
499
 
        
用了二维数组存大数,结构体比较各个位的和以及大数。然而看同学的做法很简单,此题很水。
 
        
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;


struct zz
{
    int sum;
    __int64 num;
}w[20];
bool f(zz x,zz y)
{
    if(x.sum==y.sum)
        return x.num>y.num;
    else
        return x.sum>y.sum;
}
int main()
{
    char n[111];
    scanf("%s",n);
    int i,l,j;
    int a[111][20];
    l=strlen(n);
    w[l-1].sum=0;
    w[l-1].num=0;
    for(j=0;j<l;j++)
    {
        a[l-1][j]=n[j]-'0';
        w[l-1].sum+=a[l-1][j];
        w[l-1].num=w[l-1].num*10+a[l-1][j];
    }
    for(i=0;i<l-1;i++)
    {
        w[i].sum=0;
        w[i].num=0;
        for(j=0;j<l;j++)
        {
            if(i==j)
            {
                a[i][j]=n[j]-'0'-1;
            }
            else if(j<i)
            {
                a[i][j]=n[j]-'0';
            }
            else
            {
                a[i][j]=9;
            }
            w[i].sum+=a[i][j];
            w[i].num=w[i].num*10+a[i][j];
        }
    }
    sort(w,w+l,f);
    printf("%I64d\n", w[0].num);




    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值