日月食 在线查询:如何C++编程解决用最少的张数人民币凑成所需钱数?

来源:百度文库 编辑:高校问答 时间:2024/04/29 19:48:03
编程序,输入正整数m代表钱数,求方案用最少的人民币(面值为100,50,20,10,5,2,1)凑成这个钱数,输出每种面值人民币各需多少张.要求用类,对象,函数.

//有浓重的C风格,习惯,不好意思
// zd_60.cpp : Defines the entry point for the console application.
//

#include <string.h>
#include <stdio.h>
#include <conio.h>

class money
{
private:
int total;
int result[7];
public:
money(int total);
void num();
int *getresult();
};

money::money(int a)
{
total=a;
for(int i=0;i<7;i++)
result[i]=0;
}

void money::num()
{
if(total>=100)
{
total-=100;
result[0]++;
this->num();
}
else if(total>=50)
{
total-=50;
result[1]++;
this->num();
}
else if(total>=20)
{
total-=20;
result[2]++;
this->num();
}
else if(total>=10)
{
total-=10;
result[3]++;
this->num();
}
else if(total>=5)
{
total-=5;
result[4]++;
this->num();
}
else if(total>=2)
{
total-=2;
result[5]++;
this->num();
}
else if(total>=1)
{
total-=1;
result[6]++;
this->num();
}
else
return;
}

int* money::getresult()
{
return result;
}

int main(int argc, char* argv[])
{
int rmb;
printf("输入钱数:\n");
scanf("%d",&rmb);
money a=money(rmb);
a.num();
int *result=a.getresult();
printf("需要的张数为:\n100\t 50\t 20\t 10\t 5\t 2\t 1\t\n");
for(int i=0;i<7;i++)
printf("%3d\t",result[i]);
getch();
return 0;
}

输入钱数:
8759
需要的张数为:
100 50 20 10 5 2 1
87 1 0 0 1 2 0

给你写个伪代码吧:
typedef struct 钞票
{
int 百元大钞;//这里是张数
int 一巴掌;//五十元
……//以下略
}RMB;
class 财富
{
public:
财富();
void 换零钱(int 金额,RMB *);
~财富();
private:
long int 钱;
long int 个人净资产;
RMB 票子;
int *实际面额;//这个留着做洗_钱()时候用吧
void 搞定银行(long 钱);
void 搞定官员(long 钱);
void 玩房地产(long 钱);
}
财富:财富()
{
int *p;
实际面额=new int[7];
p=实际面额;
*p=100;
*(p+1)=50;
……
*(p+6)=1;
搞定银行(long 钱);
搞定官员(long 钱);
玩房地产(long 钱);
}
void 财富:换零钱(int 金额,RMB *现钞)
{
int temp;
temp=金额;
现钞->百元大钞=temp/100;temp=temp%100;
现钞->一巴掌=temp/50; temp%=50;
……
}
本来可以用一个指针一次分配7个空间,不过为清晰起见,还是用个RMB结构吧。
做这样一个函数,根本不需要用类啥的,不过既然有此要求,就这样做了

int main()
{
int money,zhangshu[7];
int mianzhi[7]={100,50,20,10,5,2,1};
cin>>money;
for(int x=0;x<=6;x++)
{
zhangshu[i]=money/mianzhi[i];
money=money%mianzhi[i];
}
cout<<"需要的张数各为";
for(int x=0;x<=6;x++)
{
cout<<" "<<zhangshu[i];
}
return 0;
}
我没有调试,只是按照自己想法写的,写完才看到要用类,可惜
不过这个题目有必要抽象一个类吗?

现在做java
C++
都快忘啦