用C语言打印1到100的罗马数字

在这里插入图片描述
罗马字母对照表作为参考
10,40,50,90,100都是特殊处理的分界线

#include"stdafx.h"
#include<stdio.h>


int main()
{   
	int num[7] = { 1, 5,10,50,100,500,1000 }; //罗马数字转换模板
	char str[7] = { 'I','V','X','L','C','D','M' };
	char rom[10][5] = { "\0", "I","II","III","IV","V","VI","VII","VIII\0","IX"};
	for (int j = 1;j <= 100;j++)
	{
		int i = j;
		while (i)
		{   
			if (i < 10)
			{
				puts(rom[i]);
				i = 0;
			}
			if (i == 100) {
				puts("C");
				i = 0;
			}
			if (i >= 90)
			{
				putchar('X');
				putchar('C');
				i -= 90;
			}
			if (i >= 50)
			{
				putchar(str[3]);
				i -= 50;
			}
			if (i >= 40)
			{
				putchar('L');
				putchar('X');
				i -= 40;
			}
			while (i >= 10)
			{
				putchar('X');
				i -= 10;
			}
		}
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/huaiyingdetective/article/details/83302401