zoukankan      html  css  js  c++  java
  • CodeForces 514A

    闲的蛋疼,前两个月学了java,今天又学了Python,所以找了个水题,熟悉一下四种语言。

    其实语言不是问题,思路有了,换个语言,如果基本语法会的话,根本不成问题。

    确实,Python就是够简洁。因为java和Python只知道点皮毛,可能写的还不够好。

    Description

    Luke Skywalker gave Chewbacca an integer number x. Chewbacca isn't good at numbers but he loves inverting digits in them. Inverting digit tmeans 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 Input

    Input
    27
    


    Output
    22
    


    Input
    4545
    


    Output
    4444



     ANSI C:

    #include <string.h>
    #include <stdio.h>
    
    void change(char *x){
        *x =  '0' + 9 - *x + '0';
    }
    int main()
    {
        char num[32];
        scanf("%s", num);
        if(num[0]!='9' && num[0]>='5')
            change(&num[0]);
        int i;
        for(i = 1; i < strlen(num); ++i)
            if(num[i]>='5')
                change(&num[i]);
        puts(num);
        return 0;
    }

    C++:

    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <cctype>
    
    using namespace std;
    
    inline void change(char & x){
        x =  '0' + 9 - x + '0';
    }
    int main()
    {
        char num[32];
        cin >> num;
        if(num[0]!='9' && num[0]>='5')
            change(num[0]);
        for(int i = 1; i < strlen(num); ++i)
            if(num[i]>='5')
                change(num[i]);
        cout << num << endl;
        return 0;
    }

    Java:

    import java.util.*;
    
    public class Main {
    	public static void main(String[] args) {
    		Scanner cin = new Scanner(System.in);
    		String num = cin.next();
    		for(int i = 0; i < num.length(); ++i){
    			if(i==0){
    				if('5' <= num.charAt(0) && num.charAt(0) <= '8')
    					System.out.print((char)('0' + 9 - num.charAt(0) + '0'));
    				else
    					System.out.print(num.charAt(0));
    			}
    			else{
    				if('5' <= num.charAt(i) && num.charAt(i) <= '9')
    					System.out.print((char)('0' + 9 - num.charAt(i) + '0'));
    				else
    					System.out.print(num.charAt(i));
    			}
    		}
    		cin.close();
    	}
    
    }
    


    Python:

    x = list(input())
    for i in range(len(x)):
        if (i == 0 and '5' <= x[0] <= '8') or (i != 0 and '5' <= x[i] <= '9'):
            x[i] = str(9-int(x[i]))
        print(x[i], end = '')


  • 相关阅读:
    【读书笔记-《Android游戏编程之从零开始》】2.Hello,World!
    【读书笔记-《Android游戏编程之从零开始》】1.Android 平台简介与环境搭建
    .Net HttpClient 模拟登录微信公众平台发送消息
    C# DateTime的ToString()方法的使用
    SQL 2008R2 日期转换
    Dojo学习(一)—Hello Dojo
    【博客开篇】服务器配置:Windows2008R2+PHP5.6+SQLServer2008(X64)
    【翻译Autofac的帮助文档】1.入门指南
    申请免费的SSL证书(Win7,PowerShell,Let's Encrypt)
    Rms操作设置office系统文档权限
  • 原文地址:https://www.cnblogs.com/kunsoft/p/5312714.html
Copyright © 2011-2022 走看看