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 }
  • 相关阅读:
    Java 和 DynamoDB
    关于Mongodb的全面总结
    utf8mb4 使用注意
    mysql 事务隔离讲的比较好的文章收藏。
    [mysql] 常用命令总结
    [JTA] Java事务api
    [Hibernate] Hibernate 参数设置一览表(转)
    Spring配置sessionFactory的几种常用方式
    [前端] org.apache.jasper.JasperException 页面有空引用
    [Hibernate] JPA与Hibernate的优缺点
  • 原文地址:https://www.cnblogs.com/wsaaaaa/p/4292890.html
Copyright © 2011-2022 走看看