Codeforces Round #247 (Div. 2) A. Black Square

http://codeforces.com/problemset/problem/431/A

题意:
手机有4个区域,每个区域有1,2,3,4种方块。
a1,a2,a3,a4为每种方块消掉后获得的分数。
然后告诉你出现方块的区域。问,最后获得了多少分数。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)

int main() {
int a[4];
FOR(i, 0, 4) cin >> a[i];
string s;
cin >> s;
long long res = 0;
FOR(i, 0, s.size()) {
int idx = s[i] - '1';
// 把传入的字符串转换为数字
// 例:‘1’ - ‘1’ = 0 即为第一块区域的下标
res += a[idx];
}
cout << res << endl;
return 0;
}