gpt4 book ai didi

c++ - 尝试将给定金额分成不同的账单

转载 作者:行者123 更新时间:2023-11-30 16:11:19 25 4
gpt4 key购买 nike

所以基本上,我想编写一个程序,接受任何给定金额(例如 93)并将该数字划分为不同的账单,以显示我可以如何付款。在这种情况下,93 应该变成

4张20美元的钞票1 张 10 美元钞票3 张 1 美元钞票

#include <stdio.h>

int main()

{
int money;

printf(" In this program you will be able to put in any certain amount of money and we will divide it into $20 bills, $10 bills, $5 bills and $1 bills. \n");
printf(" How much money do you have?:");
scanf("%d", &money);

int twentyb = money/20;
int tenb = (money - (twentyb*20))/10 ;
int fiveb = (money - (tenb*10-twentyb*20))/5;
int oneb = (money - (tenb*10-twentyb*20-fiveb*65))/1;

printf("\n The given amount can be divided into:");
printf("\n $20 bills:%d", twentyb);
printf("\n $10 bills:%d", tenb);
printf("\n $5 bills:%d ", fiveb);
printf("\n $1 bills:%d \n", oneb);
return 0;

}

输入93美元后我得到的是

$20 bills: 4
$10 bills: 1
$5 bills: 16
$1 bills: 90

最佳答案

你的算术是错误的。您混淆了 +-。另外,65 应该是 5,但我猜这只是一个拼写错误。

int twentyb = money / 20;
int tenb = (money - (twentyb * 20)) / 10;
int fiveb = (money - (tenb * 10 + twentyb * 20)) / 5;
int oneb = (money - (tenb * 10 + twentyb * 20 + fiveb * 5)) / 1;

关于c++ - 尝试将给定金额分成不同的账单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58645201/

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