NC25245. 张老师数数
描述
输入描述
第一行一个整数x(1<=x<=2000)
输出描述
输出一行,表示整数x所对应的罗马数字表示
示例1
输入:
5
输出:
V
示例2
输入:
1926
输出:
MCMXXVI
C++14(g++5.4) 解法, 执行用时: 6ms, 内存消耗: 608K, 提交时间: 2019-05-01 13:04:19
#include<bits/stdc++.h> using namespace std; int a[13]={1,4,5,9,10,40,50,90,100,400,500,900,1000}; string b[13]={"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"}; int main() { int n; cin>>n; while(n) { int i; for(i=12;i>=0;i--) { if(n/a[i]) break; } for(int j=0;j<n/a[i];j++) cout<<b[i]; n=n%a[i]; } cout<<endl; return 0; }
C++11(clang++ 3.9) 解法, 执行用时: 5ms, 内存消耗: 504K, 提交时间: 2019-04-22 19:31:10
#include<stdio.h> #include<bits/stdc++.h> using namespace std; string S[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; int num[]={1000,900,500,400,100,90,50,40,10,9,5,4,1}; int main(){ int n;cin>>n; for(int i=0;i<=12;i++){ if(n>=num[i]){ n-=num[i]; cout<<S[i]; i--; } } return 0; }
Python3(3.5.2) 解法, 执行用时: 32ms, 内存消耗: 3576K, 提交时间: 2019-04-20 14:32:02
a = [1000,900,500,400,100,90,50,40,10,9,5,4,1] b = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I'] c = [0 for i in range(13)] n = int(input()) for i in range(13): c[i] = b[i] * (n // a[i]) n %= a[i] for i in c: print(i,end="") print()