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 }
  • 相关阅读:
    js弹出文字
    javascript函数的使用
    php笔记-双引号内的变量会被解释,而单引号内的变量则原样输出
    单独编译源码树下的模块
    内核模块开机自动加载和黑名单
    [转]Linux中设置服务自启动的三种方式
    rpm打包
    APC to USB
    [转]创建一个虚拟盘
    编译打印输出重定向
  • 原文地址:https://www.cnblogs.com/wsaaaaa/p/4292890.html
Copyright © 2011-2022 走看看