根据用户输入计算尽可能少的硬币
Calculating the fewest possible coins possible from user input
相当新的编码员正在学习创建一个程序,该程序将从 0 到 99 之间的任何给定美分输入中输出尽可能少的硬币。我不需要为此代码添加任何限制,这就是为什么你不看不到 0 到 99 之间的任何限制。
这是我所拥有的,但我似乎无法弄清楚如何正确使用模数来结转之前计算的剩余数字。
非常感谢您提供的任何建议!我想我已经很接近了,但是我在 % 模数的心理壁垒上。我知道我可以使用 long 算术计算前一个数字,但我想弄清楚如何使用 % 修饰符使其更短。
#include <iostream>
using namespace std;
int main()
{
int cents;
const int quarter = 25;
const int dime = 10;
const int nickel = 5;
const int penny = 1;
// Get the amount of cents
cout << "Please enter an amount in cents less than a dollar." << endl;
cin >> cents;
// Calculate how many of each coin you can get from cent input
int Q = cents % quarter;
int D = Q % dime;
int N = D % nickel;
int P = N % penny;
// Display the coins you can get from cent input
cout << "Your change will be:" << endl;
cout << "Q: " << Q << endl;
cout << "D: " << D << endl;
cout << "N: " << N << endl;
cout << "P: " << P << endl;
return 0;
}
%
returns除法的余数,不是四舍五入的数字。所以它分配给 Q
除法的剩余部分。建议你先算出X
型硬币的个数,再把余数留到下一次计算。像
int Q = cents / quarter;
int D = (cents%quarter) / dime;
以此类推
每种硬币类型的总数:
int Q = cents / quarter;
int D = cents / dime;
int N = cents / nickel;
int P = cents / penny;
最少的硬币数量
int Q = cents / quarter; cents %= quarter;
int D = cents / dime; cents %= dime;
int N = cents / nickel; cents %= nickel;
int P = cents;
我不太明白你想要什么,但据我了解,这就是我相信你正在尝试做的事情
int cointypes[4]={25,10,5,1};
int coinnumber[4];
int temp=cents;
for(int i=0;i<=3;i++)
{
coinnumber[i]=temp/cointypes[i];
temp=temp%cointypes[i];
}
cout << "Your change will be:" << endl;
cout << "Q: " << coinnumber[0] << endl;
cout << "D: " <<coinnumber[1] << endl;
cout << "N: " << coinnumber[2]<< endl;
cout << "P: " << coinnumber[3]<< endl;
这是另一个使用 std::div
和 C++17 结构化绑定的解决方案:
#include <iostream>
#include <string>
#include <cstdlib>
#include <utility>
#include <vector>
int main()
{
std::vector<std::pair<std::string, int>> coins {
{"quarter", 25},
{"dime", 10},
{"nickel", 5},
{"penny", 1} };
std::cout << "Please enter an amount in cents less than a dollar.\n";
int cents;
std::cin >> cents;
std::cout << "Your change will be:\n";
for(const auto& [coin_name, value] : coins) {
auto[coin_count, remainder] = std::div(cents, value);
cents = remainder;
std::cout << coin_name << "\t: " << coin_count << '\n';
}
}
相当新的编码员正在学习创建一个程序,该程序将从 0 到 99 之间的任何给定美分输入中输出尽可能少的硬币。我不需要为此代码添加任何限制,这就是为什么你不看不到 0 到 99 之间的任何限制。
这是我所拥有的,但我似乎无法弄清楚如何正确使用模数来结转之前计算的剩余数字。
非常感谢您提供的任何建议!我想我已经很接近了,但是我在 % 模数的心理壁垒上。我知道我可以使用 long 算术计算前一个数字,但我想弄清楚如何使用 % 修饰符使其更短。
#include <iostream>
using namespace std;
int main()
{
int cents;
const int quarter = 25;
const int dime = 10;
const int nickel = 5;
const int penny = 1;
// Get the amount of cents
cout << "Please enter an amount in cents less than a dollar." << endl;
cin >> cents;
// Calculate how many of each coin you can get from cent input
int Q = cents % quarter;
int D = Q % dime;
int N = D % nickel;
int P = N % penny;
// Display the coins you can get from cent input
cout << "Your change will be:" << endl;
cout << "Q: " << Q << endl;
cout << "D: " << D << endl;
cout << "N: " << N << endl;
cout << "P: " << P << endl;
return 0;
}
%
returns除法的余数,不是四舍五入的数字。所以它分配给 Q
除法的剩余部分。建议你先算出X
型硬币的个数,再把余数留到下一次计算。像
int Q = cents / quarter;
int D = (cents%quarter) / dime;
以此类推
每种硬币类型的总数:
int Q = cents / quarter;
int D = cents / dime;
int N = cents / nickel;
int P = cents / penny;
最少的硬币数量
int Q = cents / quarter; cents %= quarter;
int D = cents / dime; cents %= dime;
int N = cents / nickel; cents %= nickel;
int P = cents;
我不太明白你想要什么,但据我了解,这就是我相信你正在尝试做的事情
int cointypes[4]={25,10,5,1};
int coinnumber[4];
int temp=cents;
for(int i=0;i<=3;i++)
{
coinnumber[i]=temp/cointypes[i];
temp=temp%cointypes[i];
}
cout << "Your change will be:" << endl;
cout << "Q: " << coinnumber[0] << endl;
cout << "D: " <<coinnumber[1] << endl;
cout << "N: " << coinnumber[2]<< endl;
cout << "P: " << coinnumber[3]<< endl;
这是另一个使用 std::div
和 C++17 结构化绑定的解决方案:
#include <iostream>
#include <string>
#include <cstdlib>
#include <utility>
#include <vector>
int main()
{
std::vector<std::pair<std::string, int>> coins {
{"quarter", 25},
{"dime", 10},
{"nickel", 5},
{"penny", 1} };
std::cout << "Please enter an amount in cents less than a dollar.\n";
int cents;
std::cin >> cents;
std::cout << "Your change will be:\n";
for(const auto& [coin_name, value] : coins) {
auto[coin_count, remainder] = std::div(cents, value);
cents = remainder;
std::cout << coin_name << "\t: " << coin_count << '\n';
}
}