From a3219c2c0bf96a374d31e02b2a6532266b2de1e3 Mon Sep 17 00:00:00 2001 From: Zerroi Date: Sun, 3 Mar 2024 19:33:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E6=95=B0=E8=BD=AC=E7=BD=97=E9=A9=AC?= =?UTF-8?q?=E6=95=B0=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ThreeAndThree.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/ThreeAndThree.java b/ThreeAndThree.java index a5f9d46..3314184 100644 --- a/ThreeAndThree.java +++ b/ThreeAndThree.java @@ -4,6 +4,17 @@ public class ThreeAndThree { class Solution { public String intToRoman(int num) { - + String[] a1 = new String[] { "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M" }; + int[] a2 = new int[]{1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000}; + + StringBuilder builder = new StringBuilder(); + for (int i = a1.length-1; i >=0; i--) { + while (num >= a2[i]) { + builder.append(a1[i]); + num -= a2[i]; + } + } + + return builder.toString(); } } \ No newline at end of file