在C ++中将介于1到3999之间的十进制数转换为罗马数字
在本教程中,我们将讨论将1到3999之间的十进制数字转换为罗马数字的程序。
为此,我们将提供一个随机整数。我们的任务是将给定数字转换为其等效的罗马数字。
示例
#include <bits/stdc++.h> using namespace std; //将十进制转换为罗马数字 int printRoman(int number){ int num[] = {1,4,5,9,10,40,50,90,100,400,500,900,1000}; string sym[] = {"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"}; int i=12; while(number>0){ int div = number/num[i]; number = number%num[i]; while(div--){ cout<<sym[i]; } i--; } } int main(){ int number = 3949; printRoman(number); return 0; }
输出结果
MMMCMXLIX