zoukankan      html  css  js  c++  java
  • 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.

    Sample test(s)
    Input
    27
    Output
    22
    Input
    4545
    Output
    4444


    题意:给一个数字x,比较x的每一位m与9-m的大小,取较小的那一位替换原来的位,最高位不能为0,输出这个最小数;
    分析:给出数据已超过int范围改用longlong,如果数据更大考虑用字符串,同时数组存储每一位,注意最高位不能为0;

     1 #include<cstring>
     2 #include<cstdio>
     3 #include<iostream>
     4 
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     long long n;
    10     int a[20],j=0;
    11     cin>>n;
    12     for(;n!=0;n/=10)
    13     {
    14         int m=n%10;
    15         if(n/10!=0&&9-m<m) m=9-m;
    16         else if(n/10==0&&m!=9&&9-m<m) m=9-m;
    17         a[j++]=m;
    18     }
    19     for(j--;j>=0;j--)
    20         cout<<a[j];
    21     cout<<endl;
    22 }
  • 相关阅读:
    TweenMax参数补充
    jQuery.lazyload详解
    js函数和jquery函数详解
    数数苹果手机中的不科学
    网页全栈工程师要点分析
    瞄了一眼墙外的世界,只能给差评
    脑洞大开的自然语言验证码
    别再迷信 zepto 了
    产品列表页分类筛选、排序的算法实现(PHP)
    大学回顾和C与PHP之路
  • 原文地址:https://www.cnblogs.com/wsaaaaa/p/4292890.html
Copyright © 2011-2022 走看看