intToRoman

/*

q = x / 1000; x = x % 1000;

for q->m;

wb = x / 500; x = x % 500;

判断 wb 是否等于 9;for wb->d;

yb = x / 100; x = x % 100;

yb if 4; for yb->c;

以此类推;

*/

class Solution {

    public String intToRoman(int num) {

        int x = num;

        StringBuilder result = new StringBuilder();

        while(x != 0) {

            int temp = x / 1000;

            x = x % 1000;

            for(int i = 0; i < temp; i++) {

                result.append('M');

            }

            temp = x / 100;

            x = x % 100;

            if(temp == 9) {

                result.append("CM");

                temp = 0;

            }else if(temp >= 5) {

                result.append('D');

                temp -= 5;

            }

            if(temp == 4) {

                result.append("CD");

                temp -= 4;

            }

            for(int i = 0; i < temp; i++) {

                result.append('C');

            }

            temp = x / 10;

            x = x % 10;

            if(temp == 9) {

                result.append("XC");

                temp = 0;

            }else if(temp >= 5) {

                result.append('L');

                temp -= 5;

            }

            if(temp == 4) {

                result.append("XL");

                temp -= 4;

            }

            for(int i = 0; i < temp; i++) {

                result.append('X');

            }


            if(x == 9) {

                result.append("IX");

                x = 0;

            }else if(x >= 5) {

                result.append('V');

                x -= 5;

            }

            if(x == 4) {

                result.append("IV");

                x -= 4;

            }

            for(int i = 0; i < x; i++) {

                result.append('I');

            }

            x = x / 10;

        }


        return result.toString();

    }

}

你可能感兴趣的:(intToRoman)