我是 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++ 标准库包含表示 vector 、列表、字符串等的容器(用于管理值的集合)。这些允许您完全消除原始指针或原始数组,以及操作(如调整大小、插入元素,删除元素等)比手工处理更干净。还有一组算法(在标准标题 <algorithm>
中)可以对容器的每个元素进行操作 - 如果您正在编写遍历容器(甚至原始数组)的每个元素的循环,那么通常 (不总是)一种可以用来做同样事情的算法 - 更简洁的代码更容易阅读,因此更容易正确。
关于c++ - 我怎样才能使这个 C++ 代码更短? (零钱计算器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58038554/