WIKIOI-1264 芳香数

1人推荐 收藏 发题解

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<stdio.h>
#include<string.h>
#include<ctype.h>
char a[50];
int b[50];
int c[50];
int Change(char st)
{
    int n;
    switch(st)
    {
       case 'I':n=1;break;
       case 'V':n=5;break;
       case 'X':n=10;break;
       case 'L':n=50;break;
       case 'C':n=100;break;
       case 'D':n=500;break;
       case 'M':n=1000;break;
    }
    return n;
}
int main()
{
   int i,j,n,sum,k,v;
   scanf("%s",a); 
   n=strlen(a);k=0;v=0;
   for(i=0;i<n;i++)
   {
       if(isdigit(a[i]))
       b[k++]=a[i]-'0';
       else
       {
         c[v++]=Change(a[i]);
       }            
   }
   sum=0;
   for(i=0;i<k;i++)
   {
     if(c[i]<c[i+1]&&i+1<k)
     sum-=b[i]*c[i];
     else
     sum+=b[i]*c[i];
   }
   printf("%d\n",sum);
   return 0;   
}


你可能感兴趣的:(C++,C语言,ACM,OJ,WIKIOI,1264,芳香数)