、输入一个正整数m,它代表一个人民币钱数(元数)。求取这样一个方案,使用最少张数的人民币纸币,凑成上述的钱数m。
注:共有七种元以上面值的人民币纸币,分别为:100,50,20,10,5,2,1。假设输入数据为196时,则最少要用6张人民币纸币:196 = 1*100 + 1*50 + 2*20 + 1*5 + 1*1。当输入数据为908时,则最少要用12张人民币纸币:908 = 9*100 + 1*5 + 1*2 + 1*1。
可设计一个程序,使执行后的屏幕显示结果具有如下形式:
Input a positive integer: 196<回车>
196 (yuan) = 1*100 + 1*50 + 2*20 + 1*5 + 1*1 (yuan)
total_sheet = 6
、输入一个正整数m,它代表一个人民币钱数(元数)。求取这样一个方案,使用最少张数的人民币纸币,凑成上
答案:1 悬赏:60 手机版
解决时间 2021-01-30 12:45
- 提问者网友:低吟詩仙的傷
- 2021-01-29 12:07
最佳答案
- 五星知识达人网友:我住北渡口
- 2021-01-29 13:13
#include
using namespace std;
const int n = 7;
const int a[] = {100, 50, 20, 10, 5, 2, 1};
int main()
{
int num[n];
int sum=0;
int ts=0;
cout<<"Input a positive integer: ";
cin>>sum;
while(sum <= 0) {
cout<<"The number is not positive, please input a positive integer: ";
cin>>sum;
}
for (int i=0, res=sum; i
num[i] = res/a[i];
res -= num[i]*a[i];
ts += num[i];
}
cout<
bool start = true;
for (int i=0; i
if (num[i] != 0) {
if (start) {
cout<<" = ";
start = false;
} else {
cout<<" + ";
}
cout<
}
}
cout<<" (yuan)"<
cout<<"total_sheet = "<
return 0;
}
编译、链接、运行程序,输入与输出如下:
:!g++ -Wall sheet.cpp -o sheet
:! ./sheet
Input a positive integer: 196
196 (yuan) = 1*100 + 1*50 + 2*20 + 1*5 + 1*1 (yuan)
total_sheet = 6
:! ./sheet
Input a positive integer: 908
908 (yuan) = 9*100 + 1*5 + 1*2 + 1*1 (yuan)
total_sheet = 12
using namespace std;
const int n = 7;
const int a[] = {100, 50, 20, 10, 5, 2, 1};
int main()
{
int num[n];
int sum=0;
int ts=0;
cout<<"Input a positive integer: ";
cin>>sum;
while(sum <= 0) {
cout<<"The number is not positive, please input a positive integer: ";
cin>>sum;
}
for (int i=0, res=sum; i
res -= num[i]*a[i];
ts += num[i];
}
cout<
for (int i=0; i
if (start) {
cout<<" = ";
start = false;
} else {
cout<<" + ";
}
cout<
}
cout<<" (yuan)"<
cout<<"total_sheet = "<
return 0;
}
编译、链接、运行程序,输入与输出如下:
:!g++ -Wall sheet.cpp -o sheet
:! ./sheet
Input a positive integer: 196
196 (yuan) = 1*100 + 1*50 + 2*20 + 1*5 + 1*1 (yuan)
total_sheet = 6
:! ./sheet
Input a positive integer: 908
908 (yuan) = 9*100 + 1*5 + 1*2 + 1*1 (yuan)
total_sheet = 12
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯