我是一个C++初学者。我刚刚做了一个硬币兑换计算器,现在我希望代码更短(带有循环或其他东西(。我该怎么做?
#include <iostream>
int main() {
int amount = 0;
int result[4] = { 0, 0, 0, 0 };
int values[4] = { 25, 10, 5, 1 };
int x = 0;
std::cout << "Welcome to this super advanced (not) coin change calculator! n";
std::cout << "Please enter the amount in cents: ";
std::cin >> amount;
x = amount;
result[0] = x / values[0];
x = x % values[0];
result[1] = x / values[1];
x = x % values[1];
result[2] = x / values[2];
x = x % values[2];
result[3] = x / values[3];
x = x % values[3];
std::cout << "Optimal change: " << result[0] << " quarter(s), " << result[1] << " dime(s), " << result[2] << " nickel(s), and " << result[3] << " pennie(s)!";
return 0;
}
一些技术适用
1(与其将多个字符串文字输出到std::cout
,不如将所有文字合并为一个,然后输出。 请记住,一对相互跟随的字符串文本是组合在一起的。 例如,构造"ab" "cd"
变为"abcd"
2(消除任何不需要的变量。 在您的情况下,您的代码读取amount
,执行赋值x = amount
,然后再也不会使用amount
。 这意味着有机会消除amount
(直接读取到x
并从那里继续(或x
(不要分配x = amount
,然后在amount
上执行所有操作(。
3( 如果要重用仅与索引(如result[0] = x/values[0]
及更高result[1] = x/values[1]
(相差的逻辑,请考虑循环。
4(如果你有多个字符串要输出(你这样做!(考虑将它们也放在一个数组中 - 如果循环也访问该数组的元素。
5(不要害怕将语句分解成碎片,如果它允许你合理化,则重新排序操作。
6(如果我们正在做x = x op y
将其更改为x op = y
。 例如,将amount = amount % values[i]
更改为amount %= values[i]
。
把所有这些放在一起,你得到。
#include <iostream>
int main()
{
int amount = 0;
int result[4] = { 0, 0, 0, 0 };
int values[4] = { 25, 10, 5, 1 };
const char *denom[4] = {"quarter(s),",
"dime(s),",
"nickel(s), and",
"pennie(s)!"
};
std::cout << "Welcome to this super advanced (not) coin change calculator!n"
"Please enter the amount in cents: ";
std::cin >> amount;
for (i = 0; i < 4; ++i)
{
result[i] = amount / values[i];
amount %= values[i];
}
std::cout << "Optimal change: ";
for (i = 0; i < 4; ++i)
{
std::cout << result[i] << denom[i] << " ";
}
return 0;
}
但我们可以走得更远。 在上面,为了清楚起见,我将denom
的初始化分成多行,但数组的初始化可以组合成一行(以一定的可读性为代价(
const char *denom[4] = {"quarter(s),", "dime(s),", "nickel(s), and", "pennie(s)!"};
完成此操作后,我们看到,如果我们在它们之间移动输出语句,则可以将两个循环合并为一个,然后实际上不需要数组result
(元素仅在循环中计算,然后输出(。 因此,我们可以消除该数组,并使其成为单个变量 - 循环的局部变量。
std::cout << "Optimal change: ";
for (int i = 0; i < 4; ++i)
{
int result = amount / values[i];
amount %= values[i];
std::cout << result << denom[i] << " ";
}
return 0;
}
完成此操作后,查看循环内部,并注意仅计算result
以便我们可以输出它。 因此,通过将循环更改为
for (int i = 0; i < 4; ++i)
{
std::cout << amount/values[i] << denom[i] << " ";
amount %= values[i];
}
完成所有这些操作后,我们得到了
#include <iostream>
int main()
{
int amount = 0;
int values[4] = { 25, 10, 5, 1 };
const char *denom[4] = {"quarter(s),", "dime(s),", "nickel(s), and", "pennie(s)!"};
std::cout << "Welcome to this super advanced (not) coin change calculator!n"
"Please enter the amount in cents: ";
std::cin >> amount;
std::cout << "Optimal change: ";
for (int i = 0; i < 4; ++i)
{
std::cout << amount/values[i] << denom[i] << " ";
amount %= values[i];
}
return 0;
}
在执行上述操作时,我专注于使您的代码更短。 我已经自由地更改了操作的顺序,但产生的输出将是相同的。
还可以做更多的事情。 您的代码相当简单,但C++标准库包括表示向量、列表、字符串等的容器(用于管理值集合(。 这些允许您完全消除原始指针或原始数组,并且操作(如调整大小、插入元素、删除元素等(比手动操作更干净。 还有一组算法(在标准标头<algorithm>
中(可以对容器的每个元素进行操作 - 如果您正在编写在容器的每个元素(甚至是原始数组(上运行的循环,那么通常(并非总是(可以使用一种算法来做同样的事情 - 使用更简洁的代码,更易于阅读, 因此更容易正确。