閱讀262 返回首頁    go 阿裏雲 go 技術社區[雲棲]


WIKIOI-1264 芳香數

1264 芳香數

1人推薦收藏發題解

 

 

 

題目描述 Description

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.

你的任務是寫一個程序來計算一個給定的芳香數的值。

輸入描述 Input Description

 The input is a valid aromatic number consisting of between 2 and 20 symbols.

輸入是一個合法的芳香數,包含了2-20個字符。

輸出描述 Output Description

 The output is the decimal value of the given aromatic number.

輸出是一個十進製的整數代表這個芳香數的值。

樣例輸入 Sample Input

樣例輸入 1: 3M1D2C

樣例輸入 2: 2I3I2X9V1X

樣例輸出 Sample Output

樣例輸出 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;   
}


最後更新:2017-04-03 12:55:26

  上一篇:go Oracle EXTRACT (datetime)
  下一篇:go Android ViewPager實現上一頁下一頁左右滑動的正確方法