直接上代码吧:
// OneFenTwoFen2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
int solve(int total)
{
int max1=total/5+1;
int max2=0,max3=0;
int count=0;
for (int i=0;i<max1;i++)
{
max2=(total-i*5)/2+1;
for (int j=0;j<max2;j++)
{
max3=(total-i*5-j*2)/1;
if (i||j||max3)
{
count++;
cout<<" 有 "<<i<<" 个5 "<<j<<" 个2 "<<max3<<" 个1"<<endl;
}
}
}
return count;
}
int _tmain(int argc, _TCHAR* argv[])
{
int total=10;
cout<<"有 "<<solve(total)<<" 种情况"<<endl;
system("pause");
return 0;
}
// OneFenTwoFen2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
int solve(int total)
{
int max1=total/5+1;
int max2=0,max3=0;
int count=0;
for (int i=0;i<max1;i++)
{
max2=(total-i*5)/2+1;
for (int j=0;j<max2;j++)
{
max3=(total-i*5-j*2)/1;
if (i||j||max3)
{
count++;
cout<<" 有 "<<i<<" 个5 "<<j<<" 个2 "<<max3<<" 个1"<<endl;
}
}
}
return count;
}
int _tmain(int argc, _TCHAR* argv[])
{
int total=10;
cout<<"有 "<<solve(total)<<" 种情况"<<endl;
system("pause");
return 0;
}
本文介绍了一个使用C++实现的算法,该算法计算出组成特定金额的所有可能的硬币组合数,涉及1分、2分和5分面额的硬币,并展示了具体的代码实现。
2493

被折叠的 条评论
为什么被折叠?



