Power button set of 11 questions String Integer to Roman numerals

https://leetcode-cn.com/problems/integer-to-roman/

Roman numeral characters comprising the following seven: I, V, X, L, C, D and M.

Numerical character
the I. 1
V. 5
X-10
L 50
C 100
D 500
M 1000
, for example, 2 written as Roman numerals II, namely 1.12 written as two parallel XII, namely X + II. 27 written as XXVII, namely XX + V + II.

Typically, small numbers of Roman numerals to the right in large numbers. But there are exceptions, for example, do not write 4 IIII, but IV. In the left number 5 number 1, the number of large numbers equal to the value represented by a reduced number 5 obtained 4. Likewise, the number 9 is represented as IX. This special rule applies only to the following six cases:

I may be on the left V (5) and X (10), and to represent 4 and 9.
X L can be placed on the left (50) and C (100), and 40 and 90 are represented. 
C may be placed on D (500) and M (1000) to the left, to 400 and 900 represent.
Given an integer, convert it to a Roman numeral. To ensure that the input is in the range of 1 to 3999.

Example 1:

Input: 3
Output: "III"
Example 2:

Input: 4
Output: "IV"
Example 3:

Input: 9
Output: "IX"
Example 4:

Input: 58
Output: "LVIII"
explanation: L = 50, V = 5 , III = 3.
Example 5:

Input: 1994
Output: "MCMXCIV"
explanation: M = 1000, CM = 900 , XC = 90, IV = 4.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/integer-to-roman
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

I use the method of logic silly spent writing

 

The following code

package Demo;
import java.util.Scanner;
public class Yisheng {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int s=sc.nextInt();
        System.out.println(intToRoman(s));
    }
    public static String intToRoman(int num) {
        String a="";
        int yushu=num;
        if(yushu/1000!=0) {
            int b=yushu/1000;
            for(int i=0;i<b;i++) {
                a=a+"M";
            }
            yushu=yushu%1000;
        }
        if(yushu/100==9) {
            a=a+"CM";
            yushu=yushu%900;
        }else if(yushu/100!=0){
            int b=yushu/100;
            if(b>=5) {
                a=a+"D";
                if(b-5>0) {
                    for(int i=0;i<b-5;i++) {
                        a=a+"C";
                    }
                }
            }else if(b==4) {
                a=a+"CD";
            }else {
                for(int i=0;i<b;i++) {
                    a=a+"C";
                }
            }
            yushu=yushu%100;
        }
        if(yushu/10==9) {
            a=a+"XC";
            yushu=yushu%90;
        }else if(yushu/10!=0){
            int b=yushu/10;
            if(b>=5) {
                a=a+"L";
                if(b-5>0) {
                    for(int i=0;i<b-5;i++) {
                        a=a+"X";
                    }
                }
            }else if(b==4) {
                a=a+"XL";
            }else {
                for(int i=0;i<b;i++) {
                    a=a+"X";
                }
            }
            yushu=yushu%10;
        }
        if(yushu==9) {
            a=a+"IX";
            yushu=yushu%9;
        }else if(yushu!=0){
            
            int b=yushu;
            if(b>=5) {
                a=a+"V";
                System.out.println(b);
                if(b-5>0) {
                    for(int i=0;i<b-5;i++) {
                        a=a+"I";
                    }
                }
            }else if(b==4) {
                a=a+"IV";
            }else {
                for(int i=0;i<b;i++) {
                    a=a+"I";
                }
            }
        }
        return a;

    }
}
 

 

Published 14 original articles · won praise 2 · Views 931

Guess you like

Origin blog.csdn.net/zoooo_/article/details/96631625