C实现 LeetCode->Integer to Roman

Given an integer, convert it to a roman numeral.

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



将一个 整形转换为 罗马字符  int范围(1-3999)

C实现 LeetCode->Integer to Roman_第1张图片

//
//  IntegerToRoman.c
//  Algorithms
//
//  Created by TTc on 15/6/11.
//  Copyright (c) 2015年 TTc. All rights reserved.
//

#include "IntegerToRoman.h"

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*
 给定一个整数,将其转换成罗马数字。
 输入在1到3999的范围内。
 
 从num的高位到低位依次转换。

 
 */




char*
intToRoman01(int num) {
    const char  *map[13] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
    const int   value[13] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
    
    char *result = (char*)malloc(sizeof(char)*100);
    
    memset(result,0,100);
    for (int i = 0; num > 0; i++) {
        
        int count = num/value[i];
        num %= value[i];
        while (count > 0) {
            strcat(result, map[i]);
            --count;
        }
    }
    return result;
}


你可能感兴趣的:(C实现 LeetCode->Integer to Roman)