gpt4 book ai didi

C++收银员代码

转载 作者:行者123 更新时间:2023-11-28 07:02:03 25 4
gpt4 key购买 nike

问题:

Giving change. Implement a program that directs a cashier how to give change. The program has two inputs: the amount due and the amount received from the customer. Display the dollars, quarters, dimes, nickels, and pennies that the customer should receive in return.

我目前拥有的:

#include <iostream>
using namespace std;


int main()
{
double amount_due;
double amount_recieved;

cout << "Enter amount due: " << endl;
cin >> amount_due;
cout << "Enter amount received: ";
cin >> amount_recieved;

int change = amount_recieved - amount_due;
int dollar = 100;
int quarters = 25;
int dimes = 10;
int nickels = 5;
int pennies = 1;



//Return change in full dollars

cout << "dollars: " << change % 100 << endl;


//Return change in quarters

cout << "quarters: " << (change % 100) % 25 << endl;

//Return change in dimes

cout << "dimes: " << ((change % 100) % 25) % 10 << endl;

// Return change in nickels

cout << "nickels: " << (((change % 100) % 25) % 10) % 5 << endl;

//Return change in pennies

cout << "pennies: " << ((((change % 100) % 25) % 10) % 5) % 1 << endl;



system("pause");
return 0;

}

我知道还有其他一些答案,但它们可能更高级,无法在我的代码中使用,我做错了什么?

最佳答案

你想做的和收银员一样。

首先确保零钱表示为整便士。

然后提供足够的美元,直到剩下的零钱少于一美元。然后转到 25 美分硬币,然后是 10 美分硬币、5 分硬币和 1 美分硬币。

所以对于美元的情况,伪代码是:

dollars = change / 100        # get integral number of dollars
change = change % 100 # and reduce change-left-to-go accordingly
print dollars, " dollars"

然后按照降低值(value)的顺序将该逻辑应用于其他硬币类型应该是一件简单的事情。

关于C++收银员代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22291356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com