您的位置:首页 > 其它

C实现 LeetCode->Integer to Roman

2015-06-11 11:26 302 查看
Given an integer, convert it to a roman numeral.

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

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



//
//  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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: