C趣味编程百例(31)将阿拉伯数字转换为罗马数字

C趣味编程百例(31)将阿拉伯数字转换为罗马数字,第1张

C趣味编程百例(31)将阿拉伯数字转换为罗马数字,第2张

95.将阿拉伯数字转换为罗马数字
将大于0但小于1000的阿拉伯数字转换为罗马数字。阿拉伯数字和罗马数字的对应关系如下:


*问题分析及算法设计
题目中给出了阿拉伯数字和罗马数字的对应关系,题目中的数字转换实际上是查表翻译。即从整数中依次分解出整数的百、十、位,在表中查找对应的行后输出对应的字符。
*程序与编程
# include
void main()
{
static char * a[][10]= { " "," I "," II "," III "," XX "," XXX "," XL "," L "," LX "," LXX "," LXXX "," LXXX "," XCC "," C "," CC "," CCC "," CD "," D "," DC "," DCC "," DCCC "," CM "
};/*建立对照表*/
int n,t,I,m;
printf("请输入数字:");
scanf("%d ",& n);/*输入整数*/
printf("%d= ",n);
for(m=0,i = 1000m {
t =(n % I)/(I/10);/*从高位到低位取数字*/
printf("%s ",a[2-m][t]);/*通过对照表翻译输出*/
}
printf(" \ n ");
}

*运行结果
1。请输入号码:863
863 = dcclxiii
2。请输入号码:256
256 = cclvi
3。请输入数字:355 [/ br/] 355=CCCLV
4。请输入号码:522
522 = dxii
5。请输入数字:15
15=XV

*思考题
输入一个正整数n,生成相应的英文数字串并输出,例如:
1 one 2 two 3 three
10 EN 11十一
135ONE一百三十五。

位律师回复
DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
白度搜_经验知识百科全书 » C趣味编程百例(31)将阿拉伯数字转换为罗马数字

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情