首页 > 其他 > 详细

12. Integer to Roman

时间:2016-04-09 00:06:03      阅读:304      评论:0      收藏:0      [点我收藏+]

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

char* intToRoman(int num) {
    char* ret = malloc(sizeof(char)*13); //Each digit at maximum be written as 3 Roman character
    char* p = ret;
    char roman[4][2]={
        M,  ,
        C, D,
        X, L,
        I, V
    };
    int result;
    int benchmark = 1000;
    
    for(int len = 0; len < 4; len++){
        result = num/benchmark;
        num = num%benchmark;
        if(9 == result){
            *p = roman[len][0];
            p++;
            *p = roman[len-1][0];
            p++;
        }
        else if( result >= 5){
            *p = roman[len][1];
            p++;
            for(int i = 0; i < result-5; i++){
                *p = roman[len][0];
                p++;
            }
        }
        else if(4 == result){
            *p = roman[len][0];
            p++;
            *p = roman[len][1];
            p++;
        }
        else{
            for(int i = 0; i < result; i++){
                *p = roman[len][0];
                p++;
            }
        }
        
        benchmark /= 10 ;
    }
    *p = \0;
    return ret;
}

 

12. Integer to Roman

原文:http://www.cnblogs.com/qionglouyuyu/p/5370293.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!