标签:
这是数字转换的第一道题,要求是把阿拉伯数字转换为罗马数字,基本的思想与罗马数字转换为阿拉伯数字差不多,就是提取几个关键的数字作为比较即可。#include<stdio.h> #include<string.h> #include<stdlib.h> char *link(char *s1,char *s2) { int len1=strlen(s1); int len2=strlen(s2); for(int i=len1;i<len1+len2;i++) s1[i]=s2[i-len1]; return s1; } char *integerToRoman(int x) { char *arr[13]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; int value[13]= {1000,900,500,400,100,90,50,40,10,9,5,4,1}; char *roman=(char*)malloc(sizeof(char)*100); memset(roman,0,100); for(int i=0;x!=0;i++) { while(x>=value[i]) { x-=value[i]; roman=link(roman,arr[i]); } } return roman; } int main() { int x; while(scanf("%d",&x)!=EOF) { printf("%s\n",integerToRoman(x)); } return 0; }
标签:
原文地址:http://blog.csdn.net/zyh920521/article/details/44750247