首页 > 代码库 > codecs 1264 芳香数
codecs 1264 芳香数
1264 芳香数
This question involves calculating the value of aromatic numbers which are a combination of Arabic digits and Roman numerals.
本题是关于计算芳香数数值的问题,芳香数是阿拉伯数字和罗马数字的组合。
An aromatic number is of the form ARARAR...AR, where each A is an Arabic digit, and each R is a Roman numeral. Each pair AR contributes a value described below, and by adding or subtracting these values together we get the value of the entire aromatic number.
芳香数的格式是ARARAR..ARA,其中A代表阿拉伯数字,R代表罗马数字。每一对AR按照下面的计算方式计算一个值,通过把这些数值加减起来,就得到了整个芳香数的数值。
An Arabic digit A can be 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9. A Roman numeral R is one of the seven letters I, V, X, L, C, D, or M. Each Roman numeral has a base value:
阿拉伯数字是0,1,2..9,罗马数字是I,V,X,L,C,D,M。
Symbol I V X L C D M Base value 1 5 10 50 100 500 1000
符号I V X L C D M的值是1 5 10 50 100 500 1000。
The value of a pair AR is A times the base value of R. Normally, you add up the values of the pairs to get the overall value. However, wherever there are consecutive symbols ARA0R0 with R0 having a strictly bigger base value than R, the value of pair AR must be subtracted from the total, instead of being added.
一对AR的值计算为A乘以R。一般的,我们把所有的AR的值加起来就得到了芳香数的值。但是如果存在连续的两个数对ARA0R0,其中R0严格大于R的话,则需要减去AR的值,而不是加上。
For example, the number 3M1D2C has the value 3?1000+1?500+2?100 = 3700 and 3X2I4X has the value 3 ? 10 ? 2 ? 1 + 4 ? 10 = 68.
举个例子,3M1D2C 的值为3*1000+1*500+2*100=3700,而3X2I4X的值为3*10-2*1+4*10=68
Write a program that computes the values of aromatic numbers.
你的任务是写一个程序来计算一个给定的芳香数的值。
The input is a valid aromatic number consisting of between 2 and 20 symbols.
输入是一个合法的芳香数,包含了2-20个字符。
The output is the decimal value of the given aromatic number.
输出是一个十进制的整数代表这个芳香数的值。
样例输入 1: 3M1D2C
样例输入 2: 2I3I2X9V1X
样例输出 1: 3700
样例输出 2: -16
题目挺吓人,但其实就那么回事,耐心读就好了
#include<cstdio> #include<cmath> #include<cstring> #include<iostream> #include<string> #include<algorithm> using namespace std; int tot,x,flag,y; int a[100000]; int b[100000]; string n; int main() { ios::sync_with_stdio(false); cin>>n; for(int i=0;i<n.size();i+=2) { a[x]=n[i]-48; switch(n[i+1]) { case ‘I‘: b[x++]=1; break; case ‘V‘: b[x++]=5; break; case ‘X‘: b[x++]=10; break; case ‘L‘: b[x++]=50; break; case ‘C‘: b[x++]=100; break; case ‘D‘: b[x++]=500; break; case ‘M‘: b[x++]=1000; } } for(int i=0;i<x;i++) { if(b[i+1]>b[i]) tot-=a[i]*b[i]; else tot+=a[i]*b[i]; } cout<<tot; return 0; }
codecs 1264 芳香数