首页 > 代码库 > Leetcode#12 Integer to Roman

Leetcode#12 Integer to Roman

原题地址

 

找规律+模拟,真没啥可说的。

 

代码:

 1 string intToRoman(int num) { 2         string res; 3          4         while (num >= 1000) { 5             res += "M"; 6             num -= 1000; 7         } 8         if (num >= 900) { 9             res += "CM";10             num -= 900;11         }12         if (num >= 500) {13             res += "D";14             num -= 500;15         }16         if (num >= 400) {17             res += "CD";18             num -= 400;19         }20         while (num >= 100) {21             res += "C";22             num -= 100;23         }24         if (num >= 90) {25             res += "XC";26             num -= 90;27         }28         if (num >= 50) {29             res += "L";30             num -= 50;31         }32         if (num >= 40) {33             res += "XL";34             num -= 40;35         }36         while (num >= 10) {37             res += "X";38             num -= 10;39         }40         if (num >= 9) {41             res += "IX";42             num -= 9;43         }44         if (num >= 5) {45             res += "V";46             num -= 5;47         }48         if (num >= 4) {49             res += "IV";50             num -= 4;51         }52         while (num >= 1) {53             res += "I";54             num--;55         }56         57         return res;58 }

 

Leetcode#12 Integer to Roman