罗马数字,蛮有意思的。
之前做过一样的题目,不过比这个数字大,方法也很好,直接用在这题上了。
题目大意是:
罗马数字有几个字母组成,Ⅰ(1)、Ⅴ(5)、Ⅹ(10)、L(50)、C(100)、D(500)、M(1000)(罗马人认识的数字可真少。。。)。几个最简单的数字Ⅰ, Ⅱ , Ⅲ ,Ⅳ ,Ⅴ ,Ⅵ ,Ⅶ ,Ⅷ ,Ⅸ ,Ⅹ ,Ⅺ ,Ⅻ,分别是1到12,。规律很容易发现,一般情况下,数字是小的在后,大的在前的,如果小的数字跑到了大数字前面,那么说明这个数字比较特殊(废话-。-),那么我们就把这个小数字给减去,其他数字依次相加,这样我们就能算出这个罗马数字代表的数字是多少了。字符串,顺序刷一遍就能AC了。
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
char num[300];
char s[100];
int t,n,ans;
int main()
{
num['I'] = 1;
num['V'] = 5;
num['X'] = 10;
num['L'] = 50;
scanf("%d",&t);
gets(s);
while (t > 0)
{
t--;
gets(s);
n = strlen(s);
ans = 0;
for (int i = 0 ; i < n-1 ; i ++)
{
if (num[s[i]] < num[s[i+1]]) ans -= num[s[i]];
else ans += num[s[i]];
}
ans += num[s[n-1]];
printf("%d\n",ans);
}
return 0;
}