c语言编程,输入金额,输出并显示用100,50,20,10,5,1块钱 各需要多少张,优先使用金额

c语言编程,输入金额,输出并显示用100,50,20,10,5,1块钱 各需要多少张,优先使用金额较高的币种,求大神解答下。

#include <stdio.h>
int main()
{
    int type[] = {100, 50, 20, 10, 5, 1}, nu = 6;
    int cash, i;
    scanf ("%d", &cash);
    for (i = 0; i < nu; ++i){
        printf ("%d:%d\n", type[i], cash/type[i]);
        cash %= type[i];
    }
    return 0;
}

 程序执行结果如下

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-10-10
这个很易懂。
#include<math.h>
#include<stdio.h>
int main(void)
{
long money,n,b,
a[]={100,50,20,10,5,1};
printf ("请输入金额(元)。\n");
scanf ("%ld",&money);
for (n=0;n<6;n++)
{
if (money/a[n]==0)
goto next;
b=money/a[n];
printf ("%ld元%ld张,",
a[n],b);
money-=a[n]*b;
next:;
}
printf ("%c%c。",0x8,0x8);
}本回答被提问者采纳
第2个回答  2015-07-06
以下是最少张数算法

#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;

bool greater(const int &i, const int &j);
class money {
public:
money(vector<int> &m);
~money();
void show(int value);
private:
vector<int> m_money;
};

money::money(vector<int> &m): m_money(m)
{
sort(m_money.begin(), m_money.end(), greater);
}

money::~money()
{
}
bool greater(const int &i, const int &j)
{
return i > j;
}

void money::show(int value)
{
size_t count = 0;

while (value > 0) {
vector<int>::iterator i = m_money.begin();
while (i != m_money.end()) {
if (*i > value) {
i++;
}
else {
break;
}
}
if (i == m_money.end()) {
printf("Impossible\n");
return;
}
count += value / *i;
value = value % *i;
}
printf("%d\n", count);
}

int main(int argc, char *argv[])
{
vector<int> input;
int value;
int num;
int n;
scanf("%d", &value);
scanf("%d", &num);
while(true) {
scanf("%d", &n);
if (n == 0) {
break;
}
if (num > 0) {
input.push_back(n);
num--;
}
}

money m(input);
m.show(value);

return 0;
}
main函数中,第一个输入要拆解的钱数额,如1223元,第二个输入的是币种数,如6,然后循环输入币种数额,然后调用show显示每种币种多少张
第3个回答  2015-07-06
int hundred ,fifty ,twenty ,ten ,five ,one, amount = 0;
scanf("Please input amount of money: %d/n", &amount );
int temp = amount;
while(temp >= 100) {
temp -= 100;
++hundred;
}
while(temp >= 50) {
temp -= 50;
++fifty;
}
while(temp >= 20) {
temp -= 20;
++twenty;
}
while(temp >= 10) {
temp -= 10;
++ten;
}
while(temp >= 5) {
temp -= 5;
++five;
}
one = temp;
printf("%d = %d*hundred + %d*fifty + %d*twenty + %d*ten + %d*five + %d*one",amount,hundred,fifty,twenty,ten,five,one);
第4个回答  2015-07-06
除了取余追答

懂我的意思吗

相似回答