1264 芳香数

1264 芳香数

2012年CCC加拿大高中生信息学奥赛

时间限制: 1 s

空间限制: 128000 KB

题目等级 : 白银 Silver

题解

查看运行结果

题目描述 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

数据范围及提示 Data Size & Hint

分类标签 Tags 点此展开

没难度

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
map<int,int>g;
char s[21];
int tot;
int main(){
    g[‘I‘]=1,
    g[‘V‘]=5,
    g[‘X‘]=10,
    g[‘L‘]=50,
    g[‘C‘]=100,
    g[‘D‘]=500,
    g[‘M‘]=1000;
    scanf("%s",s);
    for(int i=0;i<strlen(s);i++){
        if(i%2==1){
            if(g[s[i]]<g[s[i+2]])
               tot-=(s[i-1]-48)*g[s[i]];
            else
               tot+=(s[i-1]-48)*g[s[i]];
        }
    }
    printf("%d\n",tot);
    return 0;
}
时间: 2024-07-30 23:54:14

1264 芳香数的相关文章

codecs 1264 芳香数

1264 芳香数 题目描述 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

【基础练习】【字符串处理】codevs1264 芳香数题解

题目来源 2012CCC加拿大高中信息学奥赛(这个系列基本都是基础练习题,想打好最基础的基础的同学们可以试一试) 题目描述 Description This question involves calculating the value of aromatic numbers which are a combination of Arabic digits and Roman numerals. 本题是关于计算芳香数数值的问题,芳香数是阿拉伯数字和罗马数字的组合. An aromatic num

【蓝桥杯】历届试题 连号区间数(运行超时)

  历届试题 连号区间数   时间限制:1.0s   内存限制:256.0MB 问题描述 小明这些天一直在思考这样一个奇怪而有趣的问题: 在1~N的某个全排列中有多少个连号区间呢?这里所说的连号区间的定义是: 如果区间[L, R] 里的所有元素(即此排列的第L个到第R个元素)递增排序后能得到一个长度为R-L+1的“连续”数列,则称这个区间连号区间. 当N很小的时候,小明可以很快地算出答案,但是当N变大的时候,问题就不是那么简单了,现在小明需要你的帮助. 输入格式 第一行是一个正整数N (1 <=

推荐一个代码行数统计工具cloc

代码行数统计工具cloc,它不但能统计代码行数,还能自动过滤掉代码中的注释,使用起来也很方便,强力推荐. 安装很方便,Ubuntu上直接udo apt-get install cloc就可以了 下面用mysql来做个例子吧,首先进入mysql的源代码文件夹: [email protected] ~/aproject $ cd mysql-5.6.19/ [email protected] ~/aproject/mysql-5.6.19 $ cloc . defined(%hash) is dep

hdoj-1280-前m大的数【计数排序】

前m大的数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13743 Accepted Submission(s): 4683 Problem Description 还记得Gardon给小希布置的那个作业么?(上次比赛的1005)其实小希已经找回了原来的那张数表,现在她想确认一下她的答案是否正确,但是整个的答案是很庞大的表,小希只想让你把

Problem1261 字串数

字串数 http://acm.hdu.edu.cn/showproblem.php?pid=1261 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5281    Accepted Submission(s): 1398 Problem Description 一个A和两个B一共可以组成三种字符串:"ABB","BA

【noip2003】 麦森数

题目描述 形如2P-1的素数称为麦森数,这时P一定也是个素数.但反过来不一定,即如果P是个素数,2P-1不一定也是素数.到1998年底,人们已找到了37个麦森数.最大的一个是P=3021377,它有909526位.麦森数有许多重要应用,它与完全数密切相关. 任务:从文件中输入P(1000<P<3100000),计算2P-1的位数和最后500位数字(用十进制高精度数表示) 输入 文件中只包含一个整数P(1000<P<3100000) 输出 第一行:十进制高精度数2P-1的位数. 第2

CCF - 201409-1 - 相邻数对

问题描述 试题编号: 201409-1 试题名称: 相邻数对 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 给定n个不同的整数,问这些数中有多少对整数,它们的值正好相差1. 输入格式 输入的第一行包含一个整数n,表示给定整数的个数. 第二行包含所给定的n个整数. 输出格式 输出一个整数,表示值正好相差1的数对的个数. 样例输入 610 2 6 3 7 8 样例输出 3 样例说明 值正好相差1的数对包括(2, 3), (6, 7), (7, 8). 评测用例规模与约定 1

java实现求一个数组里最大值和最小值之前缺省的数的算法

问题描述: 求一个数组里最大值和最小值之间缺省的数,例如 int arrDemo = {1, 3, 7};  那么就要输出最小值1和最大值7之间缺少的数字2,4,5,6 代码如下,有更好的思路欢迎大家在评论区留言讨论 1 package test; 2 3 public class Test { 4 5 static int[] array = { 6 -10,0,3,3,9 7 }; 8 9 private static void printEmptyItems(int[] array) {