将介于 1 至 3999 之间的十进制数转换为罗马字母在 C++ 中
在本教程中,我们将讨论一个将介于 1 至 3999 之间的十进制数转换为罗马字母的程序。
为此,我们将使用一个随机整数。我们的任务是将给定数字转换为其罗马字母的等价数字。
示例
#include <bits/stdc++.h> using namespace std; //converting decimal to roman numeral 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
广告