本文目錄一覽:
c語言關於找零錢的問題
#include stdio.h
int main()
{
int a,b,c,d,e,t;
scanf(“%d”,t);
a=t%100;
t-=a*100;
if(a0) printf(“10元%d個 “, a);
b=t%50;
t-=b*50;
if(b0) printf(“5元%d個 “, b);
c=t%10;
t-=c*10;
if(c0) printf(“1元%d個 “, c);
d=t%5;
t-=d*5;
if(d0) printf(“5角%d個 “, d);
e=t;
if(e0) printf(“1角%d個 “, e);
printf(“\n”);
return 0;
}
急求一個c語言源代碼關於找零錢問題的
我們知道人民幣有1、2、5、10、20、50、100這幾種面值。
現在給你n(1≤n≤250)元,讓你計算換成用上面這些面額表示且總數不超過100張,共有幾種。
#includestdio.h
int main()
{int a,b,c,d,e,f,g,n,count=0;
printf(” please input n:\n”);
scanf(“%d”,n);
for(a=n/100;a=0;a–)
for(b=(n-100*a)/50;b=0;b–)
for(c=(n-100*a-50*b)/20;c=0;c–)
for(d=(n-100*a-50*b-20*c)/10;d=0;d–)
for(e=(n-100*a-50*b-20*c-10*d)/5;e=0;e–)
for(f=(n-100*a-50*b-20*c-10*d-5*e)/2;f=0;f–)
{g=n-100*a-50*b-20*c-10*d-5*e-2*f;
if(a+b+c+d+e+f+g=100)
{count++;
printf(” 100:%d–50:%d–20:%d–10:%d–5:%d–2:%d–1:%d\n”,a,b,c,d,e,f,g);
};
}
printf(” n=%d,count=%d\n”,n,count);
return 0;}
這是程序“不過n大於100效率會低一點“`像卡死了一樣“`建議n不去太大“
急求,找零錢問題的c語言程序
#includestdio.h
#define len 10
main()
{
int m[len];
int num[len]={0};
int n,j;
int i=0;
int temp=1;
printf(“please input the vertex:\n”);
printf(“if you want to end inputing,please input 0\n”);
while(ilentemp!=0)
{
scanf(“%d”,temp);
if(temp!=0)
m[i]=temp;
i++;
if(judgeorder(m)==0)
{
printf(“please reenter the vertex:\n”);
for(j=0;j=i;j++)
m[j]=0;
i=0;
}
}
printf(“please input the amount of money:\n”);
scanf(“%d”,n);
i=0;
while(m[i]!=0)
{
num[i]=n/m[i];
n=n-num[i]*m[i];
if(n==0)
break;
i++;
}
for(i=0;ilen;i++)
{
if(num[i]==0)
continue;
else
printf(“The %3d fen needs %3d\n”,m[i],num[i]);
}
getch();
}
int judgeorder(int a[len])
{
int i,count=0;
for(i=0;ilen-1a[i]!=0;i++)
{
if(a[i]a[i+1])
{
return 0;
break;
}
else count++;
}
if(count==len-2)
return 1;
}
這個就是你要的程序,如果有不懂的話加我QQ357685821,我給你解釋
c語言找零錢問題
double iChange=0;
double taget=0.72;
double Money[5] = { 0.5, 0.1, 0.05, 0.02, 0.01};
double ArrChange[100];
for語句將ArrChange置零;
int CountCoin=0;
while(iChange != taget )
{
for(int i = 0 ; i 5 ; i ++)
{
if(iChange + Money[i] taget)continue;
else
{
iChange += Money[i];
ArrChange[CountCoin] = Money[i];
CountCoin++;
}
}
}
//基本是這麼個思路,如果一點也不思考對你沒好處,原理就是找多了就換小的,盡量選大的錢
c語言 找零錢問題,謝謝
這很容易。
先輸入n值,然後從最大面值的人民幣開始減。例如:
我有238元
減最大面值的第一個。238-100=138。結果為正數且不為零。然後記錄100元張數的變數加1(這些變數都應初始化時為0)
繼續,138-100=38.結果正數且不為零,同上100面值變數加1,
38-100。結果小於零。不再用100面值的減。
38-50。結果同樣小於零,不再用50面值的減。
38-20=18.結果為正數且不為零,20元張數的變數加1,
18-20。結果小於零。不再用20面值的減。
18-10=8。結果為正數且不為零,10元張數的變數加1,
8-10.結果小於零。不再用10面值的減。
8-5=3。結果為正數且不為零,5元張數的變數加1,
3-5。結果小於零。不再用5面值的減。
3-2=1.結果為正數且不為零,2元張數的變數加1,
1-1=0.結果為零。1元張數變數加1.顯示結果。
你有:
100元(2)張,50元(0)張,20元(1)張,10元(1)張,5元(1)張,2元(1)張,1元(1)張。
用C語言編寫找零錢問題,謝謝!!
//用遞歸, C++下調試
#include iostream
using namespace std;
void compute_coins(int coin_value,int number,int amount_left){
if(amount_left = coin_value)
compute_coins(coin_value, ++number, amount_left -= coin_value);
else{
cout coin_value “:” number endl;
number = 0;
switch(coin_value){
case 25:
compute_coins(10, number, amount_left);
break;
case 10:
compute_coins(5, number, amount_left);
break;
case 5:
compute_coins(1, number, amount_left);
break;
}
}
}
int main()
{
int n;
cin n;
int number = 0;
compute_coins(25, number, n);
}
//下面是C語言的,更直觀,不需要引用
#include stdio.h
void compute_coins(int coin_value,int number,int amount_left){
if(amount_left = coin_value)
compute_coins(coin_value, number+1, amount_left-coin_value);
else{
printf(“%d : %d\n”, coin_value, number);
switch(coin_value){
case 25:
compute_coins(10, 0, amount_left);
break;
case 10:
compute_coins(5, 0, amount_left);
break;
case 5:
compute_coins(1, 0, amount_left);
break;
}
}
}
int main()
{
int n;
printf(“input amount:”);
scanf(“%d”, n);
compute_coins(25, 0, n);
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/186116.html