Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

การแปลงเลขทศนิยมที่อยู่ระหว่าง 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