我写了一个程序,但我觉得不好用的是我用了两次代码。
有问题的地方在这里:
if (2 * cell_price > large_cell_price) {
for (int i = 0; i < parquet.size(); ++i) {
for (int j = 0; j < parquet[i].size(); ++j) {
if (parquet[i][j] == '*') {
if (j + 1 < parquet[i].size() && parquet[i][j + 1] == '*') {
j++;
sum += large_cell_price;
} else {
sum += cell_price;
}
}
}
}
} else {
for (int i = 0; i < parquet.size(); ++i) {
for (int j = 0; j < parquet[i].size(); ++j) {
if (parquet[i][j] == '*') {
sum += cell_price;
}
}
}
}
我做了两次循环:
for (int i = 0; i < parquet.size(); ++i) {
for (int j = 0; j < parquet[i].size(); ++j) {
我无法将它结合起来。我怎么能这样做?
答案 0 :(得分:4)
试试这段代码:
for (int i = 0; i < parquet.size(); ++i) {
for (int j = 0; j < parquet[i].size(); ++j) {
if (parquet[i][j] == '*') {
if (j + 1 < parquet[i].size() &&
parquet[i][j + 1] == '*' &&
(2 * cell_price > large_cell_price)) {
j++;
sum += large_cell_price;
} else {
sum += cell_price;
}
}
}
}
答案 1 :(得分:0)
简单的方法可以是:
for (int i = 0; i < parquet.size(); ++i) {
for (int j = 0; j < parquet[i].size(); ++j) {
if (2 * cell_price > large_cell_price) {
if (parquet[i][j] == '*') {
if (j + 1 < parquet[i].size() && parquet[i][j + 1] == '*') {
j++;
sum += large_cell_price;
} else {
sum += cell_price;
}
}
} else {
if (parquet[i][j] == '*') {
sum += cell_price;
}
}
}
}