< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

编辑器加载中...

CIVIC DILL MIX

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 134    Accepted Submission(s): 86

Problem Description

Roman numerals are an ancient numbering system used extensively throughout Europe through the 13th century (where it was eventually replaced by our current positional system). Vestiges of this system still exist today on clock faces, building cornerstones, Super Bowls and Star Wars episodes. The system uses the following 7 symbols:


Symbols I, X, C and M can be repeated as needed (though never more than three times for I, X and C), so that 3 is represented as III, 27 as XXVII and 4865 as MMMMDCCCLXV. The symbols are always written from the highest value to the lowest, but for one exception: if a lower symbol precedes a higher one, it is subtracted from the higher. Thus 4 is written not as IIII but as IV, and 900 is written as CM. The rules for this subtractive behavior are the following:
1. Only I, X and C can be subtracted.
2. These numbers can only appear once in their subtractive versions (e.g., you can’t write 8 as IIX).
3. Each can only come before symbols that are no larger than 10 times their value. Thus we can not write IC for 99 or XD for 490 (these would be XCIX and CDXC, respectively). Note that the first two words in this problem title are invalid Roman numerals, but the third is fine.
Your task for this problem is simple: read in a set of Roman numeral values and output their sum as a Roman numeral.

Input

Input will consist of multiple test cases. Each test case starts with a positive integer n indicating the number of values to add. After this will come n values (potentially several on a line), all valid Roman numerals with whitespace only coming between values. A value of n = 0 will indicate end of input. All sums will be less than 5000.

Output

For each test case, output the case number and the sum, both as Roman numerals, using the format shown below. Case numbers should start at I.

Sample Input

2
XII MDL
4
I I I
I
0

Sample Output

Case I: MDLXII
Case II: IV

Source

2007 East Central North America

 解题报告:这道题就是模拟题吧,因为得按照规则来, I, X, C and M可以出现多次,只允许I, X and C 可以被减!注意这点就行了,但是昨天内部赛的时候就是WA哎!最后还是队友改了一下我的程序才AC

复制代码
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
const int N = 5010;//这儿必须是大于5000,要不然就Runtime error
char str[N][25];
int Judge(char c)
{
if (c == 'I')
{
return 1;
}
else if (c == 'V')
{
return 5;
}
else if (c == 'X')
{
return 10;
}
else if (c == 'L')
{
return 50;
}
else if (c == 'C')
{
return 100;
}
else if (c == 'D')
{
return 500;
}
else if (c == 'M')
{
return 1000;
}
return 0;
}
void Print(int ans)//输出函数
{
while (ans)
{
if (ans >= 1000)
{
printf("M");
ans -= 1000;
}
else if (ans >= 900)//特殊
{
printf("CM");
ans -= 900;
}
else if (ans >= 500)
{
printf("D");
ans -= 500;
}
else if (ans >= 400)
{
printf("CD");
ans -= 400;
}
else if (ans >= 100)
{
printf("C");
ans -= 100;
}
else if (ans >= 90)
{
printf("XC");
ans -= 90;
}
else if (ans >= 50)
{
printf("L");
ans -= 50;
}
else if (ans >= 40)
{
printf("XL");
ans -= 40;
}
else if (ans >= 10)
{
printf("X");
ans -= 10;
}
else if (ans==9)
{
printf("IX");
ans -= 9;
}
else if (ans >= 5 && ans < 9)
{
printf("V");
ans -= 5;
}
else if (ans == 4)
{
printf("IV");
ans -= 4;
}
else if (ans >= 1 && ans < 4)
{
printf("I");
ans -= 1;
}
}
}
int main()
{
int n, i, ans, j, count = 1;
while (scanf("%d", &n) != EOF && n)
{
memset(str, 0, sizeof(str));
for (i = 0; i < n; ++i)
{
scanf("%s", str[i]);
}
ans = 0;
for(i = 0; i < n; ++i)
{
int len = strlen(str[i]);
for (j = 0; j < len; ++j)
{
//只要前面的比后面的小,例如IV表示的是4,应该V- I
if (Judge(str[i][j]) < Judge(str[i][j + 1]) && j < len - 1 && (str[i][j] == 'I' || str[i][j] == 'X' || str[i][j] == 'C'))
{
ans -= Judge(str[i][j]);
}
else
{
ans += Judge(str[i][j]);
}
}
}
printf("Case ");
Print(count);
count ++;
printf(": ");
Print(ans);
printf("\n");
}
return 0;
}
复制代码



posted on   Stephen Li  阅读(218)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示