c语言编程,人名币兑换
发布网友
发布时间:2022-05-04 11:20
我来回答
共3个回答
热心网友
时间:2022-06-21 10:50
#include <stdio.h>
int main(void)
{
int rmb = 0;
int temp;
int ten=0,five=0,two=0,one=0;
do {
printf("输入人民币面值(100以内):");
scanf("%d",&rmb);
}while (rmb >= 100);
temp = rmb;
ten = temp/10;
temp -= ten*10;
five = temp/5;
temp -= five*5;
two = temp/2;
temp -= two*2;
one = temp;
printf("%d 元可兑换为%d个10元,%d个5元,%d个2元,%d个1元\n",rmb,ten,five,two,one);
printf("%d %d %d 1\n",ten,five,two,one);
getchar();
getchar();
return 0;
}
热心网友
时间:2022-06-21 10:51
这是我想到最短的代码
#include <stdio.h>
int main (void)
{
int i, j, ten, five, two, one;
ten = five = two = one = 0;
printf ("请输入一个数字: ");
scanf ("%d", &i);
ten = i / 10;
j = i % 10;
if (j > 4)
{
j -= 5;
five = 1;
}
two = j / 2;
if (j % 5 != 0 && j % 2 != 0)
one = 1;
printf ("结果是: %d %d %d %d\n", ten, five, two, one);
return 0;
}
其实还可以用枚举或者十个if else 来解决,尾数的1到9对应不同的组合,不过代码太长了
热心网友
时间:2022-06-21 10:51
贪婪算法可以解决这个问题