1CNY = 100fen
1JPY=100sen
1HKD=100cents
1EUR=100eurocents
1GBP=100pence
汇率表格如下
CNY JPY HKD EUR GBP 100 1825 123 14 12
输入:一个整数n,接着n行为各个国家的钱
3
1CNY
123HKD123cents
输出:所有钱换算为fen的总和
解释:1CNY=100fen,123HKD=100CNY=10000fen,123cents=1.23HKD=1CNY=100fen
10200
思路
分解字符串,分成数字和名字,然后通过一个函数换算成fen,最后累加就得到结果
代码
#include
#include
using namespace std;
double get_fen(double x, string s) { //换算为fen
if (s == "CNY") return x * 100;
if (s == "fen") return x;
if (s == "JPY") return x * 10000 / 1825;
if (s == "sen") return x * 100 / 1825;
if (s == "HKD") return x * 10000 / 123;
if (s == "cents") return x * 100 / 123;
if (s == "EUR") return x * 10000 / 14;
if (s == "eurocents") return x * 100 / 14;
if (s == "GBP") return x * 10000 / 12;
if (s == "pence") return x * 100 / 12;
return 0;
}
int main() {
int n;
string s;
double res = 0;
cin >> n;
while (n--) {
cin >> s;
int i = 0;
while (i < s.length()) {
double money = 0;
string money_name = "";
while (s[i] >= '0' && s[i] <= '9') { //得到数字
money = money * 10 + s[i] - '0';
i++;
}
while ((s[i] < '0' || s[i]>'9') && i < s.length()) { //得到money名字
money_name += s[i];
i++;
}
res += get_fen(money, money_name); //换算成fen再累加
}
}
cout << (int)res;
return 0;
}
1、判断需要输出的字符串长度,比较箱子数量和空地宽度,取较小的即为输出字符串个数,定义字符串数组
2、遍历字符串,将遍历到的字符添加到字符串数组对于的字符串,最后输出所有字符串即可
字符串数组的每个字符串的字符对应原字符串的下标如下(字符串个数为d):
第一个字符 第二个字符 第三个字符 第四个字符 … 0 2d-1 2d 4d-1 … … … … … d-1 d 3d-1 3d 奇数列:下标 i 的字符对应的为第 i%(2d) 个字符串(从第0个字符串开始)
偶数列:下标 i 的字符对应的尾第 2d-1-i%(2d) 个字符串
#include
#include
using namespace std;
int main() {
string s;
int d;
cin >> s >> d;
vector<string> vs;
if (s.length() > d) vs = vector<string>(d, ""); //判断出要输出字符串的长度,定义字符串数组
else vs = vector<string>(s.length(), "");
for (int i = 0; i < s.length(); i++) { //根据对应法则将遍历到字符添加到字符串数组
if (i % (2 * d) <= d - 1) vs[i % (2 * d)] += s[i]; //位于奇数列的字符串
else vs[2 * d - 1 - i % (2 * d)] += s[i]; //位于偶数列的字符串
}
for (auto x : vs) cout << x << endl;
return 0;
}
将需要计算的表格加入队列
通过字符串分析,进行分支,一步一步进行计算
(可惜只通过了80%)
代码
#include
using namespace std;
int get_1(string s) { //将字符串转化为数字
int value = 0;
for (int i = 0; i < s.length(); i++) {
value = value * 10 + s[i] - '0';
}
return value;
}
int main() {
int row, col;
long long res = 0;
string query;
queue<vector<int>> q;
cin >> row >> col;
vector<vector<string>> excel(100, vector<string>(100));
vector<vector<int>> value(100, vector<int>(100)); //存转化为数字的excel
vector<vector<int>> val(100, vector<int>(100)); //标记是否转化为数字
for (int i = 0; i < row; i++) { //输入excel
for (int j = 0; j < col; j++) {
cin >> excel[i][j];
if (excel[i][j][0] != '=') {
value[i][j] = get_1(excel[i][j]);
val[i][j] = 1;
}
else q.push({ i,j }); //入队为转化为数字的坐标
}
}
while (!q.empty()) { //计算队列中所有需要计算的表格
vector<int> ax = q.front();
q.pop();
string s = excel[ax[0]][ax[1]];
string s1 = "";
string s2 = "";
int index = 0;
for (int i = 1; i < s.length(); i++) { //分割运算符和单元格和数字
if (s[i] != '+' && s[i] != '-') s1 += s[i];
else {
index = i;
for (int j = i + 1; j < s.length(); j++) s2 += s[j];
break;
}
}
if (index == 0) { //等于某个单元格的值
if (val[get_1(string(s1, 1, s1.length() - 1)) - 1][s1[0] - 'A']) {
value[ax[0]][ax[1]] = value[get_1(string(s1, 1, s1.length() - 1)) - 1][s1[0] - 'A'];
val[ax[0]][ax[1]] = 1;
}
else q.push(ax);
}
else { //双目运算
if ((s1[0]>='0'&&s1[0]<='9') || (s2[0] >= '0' && s2[0] <= '9')) { //单元格和数字双目运算
int aa, bb;
if (s1[0] == '=') {
if (val[get_1(string(s1, 1, s1.length() - 1)) - 1][s1[0] - 'A']) {
aa = value[get_1(string(s1, 1, s1.length() - 1)) - 1][s1[0] - 'A'];
bb = get_1(s2);
if (s[index] == '+') value[ax[0]][ax[1]] = aa + bb;
else value[ax[0]][ax[1]] = aa - bb;
val[ax[0]][ax[1]] = 1;
}
else q.push(ax);
}
else {
if (val[get_1(string(s2, 1, s2.length() - 1)) - 1][s2[0] - 'A']) {
aa = get_1(s1);
bb = value[get_1(string(s2, 1, s2.length() - 1)) - 1][s2[0] - 'A'];
if (s[index] == '+') value[ax[0]][ax[1]] = aa + bb;
else value[ax[0]][ax[1]] = aa - bb;
val[ax[0]][ax[1]] = 1;
}
else q.push(ax);
}
}
else if (val[get_1(string(s1, 1, s1.length() - 1)) - 1][s1[0] - 'A'] && val[get_1(string(s2, 1, s2.length() - 1)) - 1][s2[0] - 'A']) { //单元格与单元格的双目运算
if (s[index] == '+') {
value[ax[0]][ax[1]] = value[get_1(string(s1, 1, s1.length() - 1)) - 1][s1[0] - 'A'] + value[get_1(string(s2, 1, s2.length() - 1)) - 1][s2[0] - 'A'];
val[ax[0]][ax[1]] = 1;
}
else {
value[ax[0]][ax[1]] = value[get_1(string(s1, 1, s1.length() - 1)) - 1][s1[0] - 'A'] - value[get_1(string(s2, 1, s2.length() - 1)) - 1][s2[0] - 'A'];
val[ax[0]][ax[1]] = 1;
}
}
else q.push(ax);
}
}
cin >> query;
string s1 = "";
string s2 = "";
for (int i = 0; i < query.length(); i++) {
if (query[i] != ':') s1 += query[i];
else {
for (int j = i + 1; j < query.length(); j++) s2 += query[j];
break;
}
}
for (int i = get_1(string(s1, 1, s1.length() - 1)) - 1; i < get_1(string(s2, 1, s2.length() - 1)); i++) {
for (int j = s1[0] - 'A'; j <= s2[0] - 'A'; j++) res += value[i][j];
}
cout << res << endl;
return 0;
}