Codeforces 514A Chewbaсca and Number【贪心】这题好劲啊

175 篇文章 2 订阅

A. Chewbaсca and Number
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Luke Skywalker gave Chewbacca an integer number x. Chewbacca isn't good at numbers but he loves inverting digits in them. Inverting digit t means replacing it with digit 9 - t.

Help Chewbacca to transform the initial number x to the minimum possible positive number by inverting some (possibly, zero) digits. The decimal representation of the final number shouldn't start with a zero.

Input

The first line contains a single integer x (1 ≤ x ≤ 1018) — the number that Luke Skywalker gave to Chewbacca.

Output

Print the minimum possible positive number that Chewbacca can obtain after inverting some digits. The number shouldn't contain leading zeroes.

Examples
Input
27
Output
22
Input
4545
Output
4444

题目大意:

给你一个LL范围内的数字X,每一位子上的数字t都可以替换为9-t,让你输出最小的,不含有前导0的正整数,不能以0开头。


思路:


贪心角度很好想,ans【i】=min(a【i】,9-a【i】);

一开始读题没看到正整数三个字,那么990的ans我认为是0.Wa一发、

后来发现了,那么990的ans我认为是9.又Wa一发、

哦,结果要求没有0开头啊。那么ans==900.

这是一道A题.恩.满满的Hack点,我不禁开始幻想当时打这场比赛的小伙伴们会Hack多少发。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define ll __int64
char a[200];
int ans[200];
ll output;
int n;
int main()
{

    while(~scanf("%s",a))
    {
        n=strlen(a);
        output=0;
        int f=0;
        for(int i=0;i<n;i++)
        {
            ans[i]=min(a[i]-'0',9-a[i]+'0');
            if(ans[i]!=0&&f==0)f=1;
            if(ans[i]==0&&f==0)ans[i]=max(a[i]-'0',9-a[i]+'0'),f=1;
        }
        for(int i=0;i<n;i++)
        {
            output=output*10+ans[i];
        }
        printf("%I64d\n",output);
    }
}





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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值