问题及代码:
/*
*copyright (c) 2014,烟台大学计算机学院
*all rights reserved.
*文 件 名 : test .cpp
*作 者 :白云飞
*完成日期 :2014年10月28号
*版 本 号 :v1.1
*
*问题描述 :用一元人民币兑换成1分,2分,5分硬币,有多少种不同的兑换方法?
*输入描述 :无
*程序输出 :不同种的兑换方法
*/
#include <iostream> //预处理指令。
#include <cmath> //使用数学符号。
using namespace std; //使用C++的命名空间 std。
int main() //函数首部。
{
int iO_cent,iD_cent,iNickel,icase=0;//声明四个变量为整型。四个变量分别代表一分硬币,两分硬币,五分硬币和方案数。
cout<<"兑换方法如下:"<<endl;
for (iO_cent=0;iO_cent<=100;iO_cent++)//循环语句
for (iD_cent=0; iD_cent<=50; iD_cent++)
for (iNickel=0; iNickel<=20; iNickel++)
if ( iO_cent+iD_cent*2+iNickel*5==100)
{ icase++;
cout<<"方案 "<<icase<<":";//输出结果
cout<<"一分硬币有"<<":"<<iO_cent<<" ";
cout<<"两分硬币有"<<":"<<iD_cent<<" ";
cout<<"五分硬币有"<<":"<<iNickel<<" "<<endl;
}return 0;
}
运行结果:
知识点总结:
用for语句编写程序。
学习心得:
幸好方案上有提示,加上有上一片的经验,所以做起来比较快。