【牛客网】A 张老师数数 左手的ㄟ右手 2022-04-24 08:36 126阅读 0赞 链接:[https://ac.nowcoder.com/acm/contest/637/A][https_ac.nowcoder.com_acm_contest_637_A] 来源:牛客网 Problem Description: 张老师需要用数字来统计学生们的得分情况,但阿拉伯数字已经不能满足他的要求了,所以想请你帮忙将学生的成绩转换成罗马数字。 罗马数字是由七个不同的符号来表示(I,V,X,L,C,D和M),其中每个符号对应的值如下表 符号 值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 罗马数字通常从左到右从最大写到最小,表示他们的总和。但是,数字4的罗马数字表示不是IIII,而是IV,表示5-1=4。同样的,有6个应用相同原理的规则: I可以放在V(5)和X(10)之前代表4和9。 X可以在L(50)和C(100)之前代表40和90。 C可以放在D(500)和M(1000)之前代表400和900。 现在给定一个整数,张老师请你计算出其罗马数字的表示。 Input: 第一行一个整数x(1<=x<=2000) Output: 输出一行,表示整数x所对应的罗马数字表示 Sample Input 1: 5 Sample Output 1: V Sample Input 2: 1926 Sample Output 2: MCMXXVI 思路: 穷举 My Code: #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> #include<queue> #include<math.h> #include<set> using namespace std; #define INF 1e9 typedef long long ll; int main() { int x; cin >> x; while(x) { if(x/1000 != 0) { int a = x/1000; for(int i = 0; i < a; i++) printf("M"); x %= 1000; } else if(x/100 != 0) { int a = x/100; if(a == 1) printf("C"); else if(a == 4) printf("CD"); else if(a == 5) printf("D"); else if(a == 9) printf("CM"); else if(a > 1 && a < 4) { for(int i = 0; i < a; i++) printf("C"); } else if(a > 5 &&a < 9) { printf("D"); for(int i = 0; i < a-5; i++) printf("C"); } x %= 100; } else if(x/10 != 0) { int a = x/10; if(a == 1) printf("X"); else if(a == 4) printf("XL"); else if(a == 5) printf("L"); else if(a == 9) printf("XC"); else if(a > 1 && a < 4) { for(int i = 0; i < a; i++) printf("X"); } else if(a > 5 &&a < 9) { printf("L"); for(int i = 0; i < a-5; i++) printf("X"); } x %= 10; } else { if(x == 1) printf("I"); else if(x == 4) printf("IV"); else if(x == 5) printf("V"); else if(x == 9) printf("IX"); else if(x > 1 && x < 4) { for(int i = 0; i < x; i++) printf("I"); } else if(x > 5 &&x < 9) { printf("V"); for(int i = 0; i < x-5; i++) printf("I"); } x %= 1; } } cout << endl; return 0; } [https_ac.nowcoder.com_acm_contest_637_A]: https://ac.nowcoder.com/acm/contest/637/A
还没有评论,来说两句吧...