Aromatic-Numbers-芳香数

敲代码敲了5个半小时。。

2012年的题目,网上都是C++和python的代码(老题目了),没看到Java写的…

题目:2012 S2

http://mmhs.ca/ccc/index.htm

https://www.cemc.uwaterloo.ca/contests/computing/2012/index.html

python答案:http://mmhs.ca/ccc/2012/s22012.txt

C::https://www.cnblogs.com/shenben/p/5539105.html

应该有很多更简单的方法,但是我这个小白想顺便学一下Map和try catch finally…就用上了;Map根本没必要,可以直接弄个方法,检测到I返回1啥的,try catch finally的话直接for里面加个if判断就行。。

参考

Map:https://www.jianshu.com/p/d439d0368e11

Try Catch Finally:https://www.cnblogs.com/ECJTUACM-873284962/p/7527052.html

题目:AROMATIC VALUES

Problem 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.

​ 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:

​ 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 ARA’R’ with R’ having a strictly bigger base value than R, the value of pair AR must be subtracted from the total, instead of being added.

​ 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.

​ Write a program that computes the values of aromatic numbers.

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

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

  • Sample Input 1
    3M1D2C
    Output for Sample Input 1
    3700
  • Sample Input 2
    2I3I2X9V1X
    Output for Sample Input 2
    -16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.*;

public class AromaticValues {
public static void main(String[] args) {

Map<Character, Integer> map = new HashMap();
char[] mapsChar = {'I','V','X','L', 'C', 'D', 'M'};
int[] mapsInt = { 1, 5, 10, 50, 100, 500, 1000};
for (int i=0; i<7; ++i)
map.put(mapsChar[i], mapsInt[i]);

System.out.println("Input aromatic number:");
String aroNum = new Scanner(System.in).nextLine();

int ans=0;
for (int i=0, a, s=aroNum.length(); i<s; i+=2){
a = map.get(aroNum.charAt(i+1));
try{
ans += Character.getNumericValue(aroNum.charAt(i)) * a *
((a >= map.get(aroNum.charAt(i+3)))? 1 : -1);
}
catch(Exception e){
ans += Character.getNumericValue(aroNum.charAt(i)) * a;
}
}

System.out.println(ans);
}
}

不需要Map和try catch:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.*;

public class AromaticValues {

public static int getNum(char ch){
if (ch == 'I')
return 1;
if (ch == 'V')
return 5;
if (ch == 'X')
return 10;
if (ch == 'L')
return 50;
if (ch == 'C')
return 100;
if (ch == 'D')
return 500;
if (ch == 'M')
return 1000;
return 0;
}

public static void main(String[] args) {

System.out.println("Input aromatic number:");
String aroNum = new Scanner(System.in).nextLine();

int ans=0;
for (int i=0, a, s=aroNum.length(); i<s; i+=2){
a = getNum(aroNum.charAt(i+1));

if (i+2==s)
ans += Character.getNumericValue(aroNum.charAt(i)) * a;
else
ans += Character.getNumericValue(aroNum.charAt(i)) * a *
((a >= getNum(aroNum.charAt(i+3)))? 1 : -1);
}

System.out.println(ans);
}
}
评论

:D 一言句子获取中...

加载中,最新评论有1分钟缓存...