gpt4 book ai didi

c - 使用嵌套循环仅打印一个组合

转载 作者:太空宇宙 更新时间:2023-11-04 03:44:26 25 4
gpt4 key购买 nike

我必须打印一个人应该支付给收银员的不同纸币的组合。程序首先询问要支付的总金额,然后询问他拥有的不同面额的纸币。问题是,我只需要打印一种纸币组合,但我通过以下代码(像往常一样)获得了所有组合。

#include <stdio.h>
int main(void)
{
int hundred,fifty,ten,total,hund,fif,t;
printf ("Enter amount to be paid:");
scanf ("%d",&total);
printf ("\n\nHow many currency notes do you have?\nEnter 100,50 and 10 Rupee Notes Respectively:\n");
scanf ("%d %d %d",&hund,&fif,&t);

printf ("\t\t\tPossible combination for Rs=%d/- \n",total);

for (hundred=0; hundred<=hund; hundred++)
{
for (fifty=0; fifty<=fif; fifty++)
{
for (ten=0; ten<=t; ten++)
{
if ((hundred*100+fifty*50+ten*10)==total)
{
printf("\n\n Hundred rupees notes=%d, 50 rupees notes=%d, 10 rupees notes=%d",hundred,fifty,ten);
}
}
}
}
getch();
return 0;
}

最佳答案

在嵌套循环内的 printf 之后添加 getch();return 0;

另一种方法是使用goto。类型

goto exit;

在嵌套循环中的 printf 之后输入

exit:

就在 getch(); 之前。

关于c - 使用嵌套循环仅打印一个组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25959300/

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