12. 整数转罗马数字

本文介绍了如何将整数转换为罗马数字,详细解析了罗马数字的字符及其对应数值,包括特殊情况如4和9的表示。通过示例展示了1到3999范围内整数的罗马数字形式,并提出了利用映射和取余数的方法来实现转换算法。
摘要由CSDN通过智能技术生成

罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。

字符 数值
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。

通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:

I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
给定一个整数,将其转为罗马数字。输入确保在 1 到 3999 的范围内。

示例 1:

输入: 3
输出: “III”
示例 2:

输入: 4
输出: “IV”
示例 3:

输入: 9
输出: “IX”
示例 4:

输入: 58
输出: “LVIII”
解释: L = 50, V = 5, III = 3.
示例 5:

输入: 1994
输出: “MCMXCIV”
解释: M = 1000, CM = 900, XC = 90, IV = 4.

分析:这东西一看就是一一映射,怎么去做?当时使用Map了,关于阿拉伯数字的处理可以使用取余数的方式来做。
注意:4和9需要特殊处理,对于4,9本人不想做过多处理就直接放在map映射里面了

package test算法;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * Title:
 * Date: 2019/2/14
 *
 * @author liujinlei
 * @version 1.0
 */
public class Solution12 {
    Map<Integer,String> map = new HashMap<>();
    @Test
    public void  test(){
        int num = 0;
        for(int i = 1;i<=3999;i++){
            if(num == 5 ){
                System.out.println();
                num = 0 ;
            }
            System.out.print(i+ ":" +intToRoman(i)+" ");
            num++;

        }
    }
    public String intToRoman(int num) {
        StringBuffer result = new StringBuffer();
        //添加映射规则,正常来说这玩意得放在外边
        map.put(1,"I");
        map.put(4,"IV");
        map.put(5,"V");
        map.put(9,"IX");
        map.put(10,"X");
        map.put(40,"XL");
        map.put(50,"L");
        map.put(90,"XC");
        map.put(100,"C");
        map.put(400,"CD");
        map.put(500,"D");
        map.put(900,"CM");
        map.put(1000,"M");
        //千
        int thousandPlace = (num / 1000 % 10);
        //百
        int hundredPlace = (num / 100 % 10);
        //十
        int tenPlace = (num / 10 % 10);
        //个
        int unitPlace = num / 1 % 10;

        StringBuffer 千 = getString(1000,thousandPlace);
        //System.out.println("千"+千);
        StringBuffer 百 = getString(100,hundredPlace);
        //System.out.println("百"+百);
        StringBuffer 十 = getString(10,tenPlace);
        //System.out.println("十"+十);
        StringBuffer 个 = getString(1,unitPlace);
        //System.out.println("个"+个);


        return result.append(千)
                .append(百)
                .append(十)
                .append(个).toString();

    }

    private StringBuffer getString(int flag, int num) {
    StringBuffer sb = new StringBuffer();
        if(num == 4 || num == 9){
            sb.append(map.get(num*flag));
        }else if(num > 0 && num < 5){
            for(int i = 1;i <= num;i++){
                sb.append(map.get(flag));
            }
        }else if(num == 5){
            sb.append(map.get(num*flag));
        }else if (num > 5 ){
            sb.append(map.get(5*flag));
            for(int i =1;i <= num - 5;i++){
                sb.append(map.get(flag));
            }
        }
        return sb;
    }
}

运行结果

1:I 2:II 3:III 4:IV 5:V 
6:VI 7:VII 8:VIII 9:IX 10:X 
11:XI 12:XII 13:XIII 14:XIV 15:XV 
16:XVI 17:XVII 18:XVIII 19:XIX 20:XX 
21:XXI 22:XXII 23:XXIII 24:XXIV 25:XXV 
26:XXVI 27:XXVII 28:XXVIII 29:XXIX 30:XXX 
31:XXXI 32:XXXII 33:XXXIII 34:XXXIV 35:XXXV 
36:XXXVI 37:XXXVII 38:XXXVIII 39:XXXIX 40:XL 
41:XLI 42:XLII 43:XLIII 44:XLIV 45:XLV 
46:XLVI 47:XLVII 48:XLVIII 49:XLIX 50:L 
51:LI 52:LII 53:LIII 54:LIV 55:LV 
56:LVI 57:LVII 58:LVIII 59:LIX 60:LX 
61:LXI 62:LXII 63:LXIII 64:LXIV 65:LXV 
66:LXVI 67:LXVII 68:LXVIII 69:LXIX 70:LXX 
71:LXXI 72:LXXII 73:LXXIII 74:LXXIV 75:LXXV 
76:LXXVI 77:LXXVII 78:LXXVIII 79:LXXIX 80:LXXX 
81:LXXXI 82:LXXXII 83:LXXXIII 84:LXXXIV 85:LXXXV 
86:LXXXVI 87:LXXXVII 88:LXXXVIII 89:LXXXIX 90:XC 
91:XCI 92:XCII 93:XCIII 94:XCIV 95:XCV 
96:XCVI 97:XCVII 98:XCVIII 99:XCIX 100:C 
101:CI 102:CII 103:CIII 104:CIV 105:CV 
106:CVI 107:CVII 108:CVIII 109:CIX 110:CX 
111:CXI 112:CXII 113:CXIII 114:CXIV 115:CXV 
116:CXVI 117:CXVII 118:CXVIII 119:CXIX 120:CXX 
121:CXXI 122:CXXII 123:CXXIII 124:CXXIV 125:CXXV 
126:CXXVI 127:CXXVII 128:CXXVIII 129:CXXIX 130:CXXX 
131:CXXXI 132:CXXXII 133:CXXXIII 134:CXXXIV 135:CXXXV 
136:CXXXVI 137:CXXXVII 138:CXXXVIII 139:CXXXIX 140:CXL 
141:CXLI 142:CXLII 143:CXLIII 144:CXLIV 145:CXLV 
146:CXLVI 147:CXLVII 148:CXLVIII 149:CXLIX 150:CL 
151:CLI 152:CLII 153:CLIII 154:CLIV 155:CLV 
156:CLVI 157:CLVII 158:CLVIII 159:CLIX 160:CLX 
161:CLXI 162:CLXII 163:CLXIII 164:CLXIV 165:CLXV 
166:CLXVI 167:CLXVII 168:CLXVIII 169:CLXIX 170:CLXX 
171:CLXXI 172:CLXXII 173:CLXXIII 174:CLXXIV 175:CLXXV 
176:CLXXVI 177:CLXXVII 178:CLXXVIII 179:CLXXIX 180:CLXXX 
181:CLXXXI 182:CLXXXII 183:CLXXXIII 184:CLXXXIV 185:CLXXXV 
186:CLXXXVI 187:CLXXXVII 188:CLXXXVIII 189:CLXXXIX 190:CXC 
191:CXCI 192:CXCII 193:CXCIII 194:CXCIV 195:CXCV 
196:CXCVI 197:CXCVII 198:CXCVIII 199:CXCIX 200:CC 
201:CCI 202:CCII 203:CCIII 204:CCIV 205:CCV 
206:CCVI 207:CCVII 208:CCVIII 209:CCIX 210:CCX 
211:CCXI 212:CCXII 213:CCXIII 214:CCXIV 215:CCXV 
216:CCXVI 217:CCXVII 218:CCXVIII 219:CCXIX 220:CCXX 
221:CCXXI 222:CCXXII 223:CCXXIII 224:CCXXIV 225:CCXXV 
226:CCXXVI 227:CCXXVII 228:CCXXVIII 229:CCXXIX 230:CCXXX 
231:CCXXXI 232:CCXXXII 233:CCXXXIII 234:CCXXXIV 235:CCXXXV 
236:CCXXXVI 237:CCXXXVII 238:CCXXXVIII 239:CCXXXIX 240:CCXL 
241:CCXLI 242:CCXLII 243:CCXLIII 244:CCXLIV 245:CCXLV 
246:CCXLVI 247:CCXLVII 248:CCXLVIII 249:CCXLIX 250:CCL 
251:CCLI 252:CCLII 253:CCLIII 254:CCLIV 255:CCLV 
256:CCLVI 257:CCLVII 258:CCLVIII 259:CCLIX 260:CCLX 
261:CCLXI 262:CCLXII 263:CCLXIII 264:CCLXIV 265:CCLXV 
266:CCLXVI 267:CCLXVII 268:CCLXVIII 269:CCLXIX 270:CCLXX 
271:CCLXXI 272:CCLXXII 273:CCLXXIII 274:CCLXXIV 275:CCLXXV 
276:CCLXXVI 277:CCLXXVII 278:CCLXXVIII 279:CCLXXIX 280:CCLXXX 
281:CCLXXXI 282:CCLXXXII 283:CCLXXXIII 284:CCLXXXIV 285:CCLXXXV 
286:CCLXXXVI 287:CCLXXXVII 288:CCLXXXVIII 289:CCLXXXIX 290:CCXC 
291:CCXCI 292:CCXCII 293:CCXCIII 294:CCXCIV 295:CCXCV 
296:CCXCVI 297:CCXCVII 298:CCXCVIII 299:CCXCIX 300:CCC 
301:CCCI 302:CCCII 303:CCCIII 304:CCCIV 305:CCCV 
306:CCCVI 307:CCCVII 308:CCCVIII 309:CCCIX 310:CCCX 
311:CCCXI 312:CCCXII 313:CCCXIII 314:CCCXIV 315:CCCXV 
316:CCCXVI 317:CCCXVII 318:CCCXVIII 319:CCCXIX 320:CCCXX 
321:CCCXXI 322:CCCXXII 323:CCCXXIII 324:CCCXXIV 325:CCCXXV 
326:CCCXXVI 327:CCCXXVII 328:CCCXXVIII 329:CCCXXIX 330:CCCXXX 
331:CCCXXXI 332:CCCXXXII 333:CCCXXXIII 334:CCCXXXIV 335:CCCXXXV 
336:CCCXXXVI 337:CCCXXXVII 338:CCCXXXVIII 339:CCCXXXIX 340:CCCXL 
341:CCCXLI 342:CCCXLII 343:CCCXLIII 344:CCCXLIV 345:CCCXLV 
346:CCCXLVI 347:CCCXLVII 348:CCCXLVIII 349:CCCXLIX 350:CCCL 
351:CCCLI 352:CCCLII 353:CCCLIII 354:CCCLIV 355:CCCLV 
356:CCCLVI 357:CCCLVII 358:CCCLVIII 359:CCCLIX 360:CCCLX 
361:CCCLXI 362:CCCLXII 363:CCCLXIII 364:CCCLXIV 365:CCCLXV 
366:CCCLXVI 367:CCCLXVII 368:CCCLXVIII 369:CCCLXIX 370:CCCLXX 
371:CCCLXXI 372:CCCLXXII 373:CCCLXXIII 374:CCCLXXIV 375:CCCLXXV 
376:CCCLXXVI 377:CCCLXXVII 378:CCCLXXVIII 379:CCCLXXIX 380:CCCLXXX 
381:CCCLXXXI 382:CCCLXXXII 383:CCCLXXXIII 384:CCCLXXXIV 385:CCCLXXXV 
386:CCCLXXXVI 387:CCCLXXXVII 388:CCCLXXXVIII 389:CCCLXXXIX 390:CCCXC 
391:CCCXCI 392:CCCXCII 393:CCCXCIII 394:CCCXCIV 395:CCCXCV 
396:CCCXCVI 397:CCCXCVII 398:CCCXCVIII 399:CCCXCIX 400:CD 
401:CDI 402:CDII 403:CDIII 404:CDIV 405:CDV 
406:CDVI 407:CDVII 408:CDVIII 409:CDIX 410:CDX 
411:CDXI 412:CDXII 413:CDXIII 414:CDXIV 415:CDXV 
416:CDXVI 417:CDXVII 418:CDXVIII 419:CDXIX 420:CDXX 
421:CDXXI 422:CDXXII 423:CDXXIII 424:CDXXIV 425:CDXXV 
426:CDXXVI 427:CDXXVII 428:CDXXVIII 429:CDXXIX 430:CDXXX 
431:CDXXXI 432:CDXXXII 433:CDXXXIII 434:CDXXXIV 435:CDXXXV 
436:CDXXXVI 437:CDXXXVII 438:CDXXXVIII 439:CDXXXIX 440:CDXL 
441:CDXLI 442:CDXLII 443:CDXLIII 444:CDXLIV 445:CDXLV 
446:CDXLVI 447:CDXLVII 448:CDXLVIII 449:CDXLIX 450:CDL 
451:CDLI 452:CDLII 453:CDLIII 454:CDLIV 455:CDLV 
456:CDLVI 457:CDLVII 458:CDLVIII 459:CDLIX 460:CDLX 
461:CDLXI 462:CDLXII 463:CDLXIII 464:CDLXIV 465:CDLXV 
466:CDLXVI 467:CDLXVII 468:CDLXVIII 469:CDLXIX 470:CDLXX 
471:CDLXXI 472:CDLXXII 473:CDLXXIII 474:CDLXXIV 475:CDLXXV 
476:CDLXXVI 477:CDLXXVII 478:CDLXXVIII 479:CDLXXIX 480:CDLXXX 
481:CDLXXXI 482:CDLXXXII 483:CDLXXXIII 484:CDLXXXIV 485:CDLXXXV 
486:CDLXXXVI 487:CDLXXXVII 488:CDLXXXVIII 489:CDLXXXIX 490:CDXC 
491:CDXCI 492:CDXCII 493:CDXCIII 494:CDXCIV 495:CDXCV 
496:CDXCVI 497:CDXCVII 498:CDXCVIII 499:CDXCIX 500:D 
501:DI 502:DII 503:DIII 504:DIV 505:DV 
506:DVI 507:DVII 508:DVIII 509:DIX 510:DX 
511:DXI 512:DXII 513:DXIII 514:DXIV 515:DXV 
516:DXVI 517:DXVII 518:DXVIII 519:DXIX 520:DXX 
521:DXXI 522:DXXII 523:DXXIII 524:DXXIV 525:DXXV 
526:DXXVI 527:DXXVII 528:DXXVIII 529:DXXIX 530:DXXX 
531:DXXXI 532:DXXXII 533:DXXXIII 534:DXXXIV 535:DXXXV 
536:DXXXVI 537:DXXXVII 538:DXXXVIII 539:DXXXIX 540:DXL 
541:DXLI 542:DXLII 543:DXLIII 544:DXLIV 545:DXLV 
546:DXLVI 547:DXLVII 548:DXLVIII 549:DXLIX 550:DL 
551:DLI 552:DLII 553:DLIII 554:DLIV 555:DLV 
556:DLVI 557:DLVII 558:DLVIII 559:DLIX 560:DLX 
561:DLXI 562:DLXII 563:DLXIII 564:DLXIV 565:DLXV 
566:DLXVI 567:DLXVII 568:DLXVIII 569:DLXIX 570:DLXX 
571:DLXXI 572:DLXXII 573:DLXXIII 574:DLXXIV 575:DLXXV 
576:DLXXVI 577:DLXXVII 578:DLXXVIII 579:DLXXIX 580:DLXXX 
581:DLXXXI 582:DLXXXII 583:DLXXXIII 584:DLXXXIV 585:DLXXXV 
586:DLXXXVI 587:DLXXXVII 588:DLXXXVIII 589:DLXXXIX 590:DXC 
591:DXCI 592:DXCII 593:DXCIII 594:DXCIV 595:DXCV 
596:DXCVI 597:DXCVII 598:DXCVIII 599:DXCIX 600:DC 
601:DCI 602:DCII 603:DCIII 604:DCIV 605:DCV 
606:DCVI 607:DCVII 608:DCVIII 609:DCIX 610:DCX 
611:DCXI 612:DCXII 613:DCXIII 614:DCXIV 615:DCXV 
616:DCXVI 617:DCXVII 618:DCXVIII 619:DCXIX 620:DCXX 
621:DCXXI 622:DCXXII 623:DCXXIII 624:DCXXIV 625:DCXXV 
626:DCXXVI 627:DCXXVII 628:DCXXVIII 629:DCXXIX 630:DCXXX 
631:DCXXXI 632:DCXXXII 633:DCXXXIII 634:DCXXXIV 635:DCXXXV 
636:DCXXXVI 637:DCXXXVII 638:DCXXXVIII 639:DCXXXIX 640:DCXL 
641:DCXLI 642:DCXLII 643:DCXLIII 644:DCXLIV 645:DCXLV 
646:DCXLVI 647:DCXLVII 648:DCXLVIII 649:DCXLIX 650:DCL 
651:DCLI 652:DCLII 653:DCLIII 654:DCLIV 655:DCLV 
656:DCLVI 657:DCLVII 658:DCLVIII 659:DCLIX 660:DCLX 
661:DCLXI 662:DCLXII 663:DCLXIII 664:DCLXIV 665:DCLXV 
666:DCLXVI 667:DCLXVII 668:DCLXVIII 669:DCLXIX 670:DCLXX 
671:DCLXXI 672:DCLXXII 673:DCLXXIII 674:DCLXXIV 675:DCLXXV 
676:DCLXXVI 677:DCLXXVII 678:DCLXXVIII 679:DCLXXIX 680:DCLXXX 
681:DCLXXXI 682:DCLXXXII 683:DCLXXXIII 684:DCLXXXIV 685:DCLXXXV 
686:DCLXXXVI 687:DCLXXXVII 688:DCLXXXVIII 689:DCLXXXIX 690:DCXC 
691:DCXCI 692:DCXCII 693:DCXCIII 694:DCXCIV 695:DCXCV 
696:DCXCVI 697:DCXCVII 698:DCXCVIII 699:DCXCIX 700:DCC 
701:DCCI 702:DCCII 703:DCCIII 704:DCCIV 705:DCCV 
706:DCCVI 707:DCCVII 708:DCCVIII 709:DCCIX 710:DCCX 
711:DCCXI 712:DCCXII 713:DCCXIII 714:DCCXIV 715:DCCXV 
71
在C语言中,将整数换为罗马数字可以分为以下几个步骤: 1. 定义罗马数字的字符映射表,存储各个数值对应的罗马数字字符,如1为'I', 4为'IV', 5为'V', 9为'IX', 等等。 2. 定义一个函数`int_to_roman(int num)`,接受一个正整数作为输入。 3. 使用循环结构,从大到小遍历罗马数字的字符映射表。对于每个字符及其对应值,检查当前数字是否大于等于这个值,如果是,则添加相应的罗马数字字符,并从原数减去该值。 4. 如果当前值小于当前数字,就需要处理特殊的组合规则。例如,如果当前是9,而前面的是4,那么会添加'IV'而不是'I'两次。 5. 当遍历完所有可能的字符和它们对应的值后,如果还有剩余的数字未换,说明输入的不是一个有效的罗马数字范围内的整数,需要特殊处理(通常返回错误提示或特定表示非罗马数字的字符串)。 6. 函数结束时,返回换后的罗马数字字符串。 下面是一个简单的C语言示例,注意这只是基本的实现,实际项目中可能需要考虑边界条件、异常处理和性能优化: ```c #include <stdio.h> #include <string.h> char* intToRoman(int num) { // ... (罗马数字字符映射表) const char* roman_numerals = "MCMXCIV"; const int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; const char* symbols[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; if (num <= 0) return "Invalid input"; int i; char* result = malloc(sizeof(char) * 20); // 预留足够空间 for (i = 0; values[i] > 0 && num >= values[i]; i++) { result[strlen(result)] = symbols[i]; num -= values[i]; } return result; } int main() { int num = 2023; char* roman_num = intToRoman(num); printf("Integer %d in Roman numerals is %s\n", num, roman_num); free(roman_num); // 不忘记释放内存 return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值