求解簡(jiǎn)單應(yīng)用題
[問(wèn)題描述]
用n元錢(qián)買(mǎi)雞,其中母雞3元/只,公雞2元/只,小雞3元/只,且每種雞至少買(mǎi)一只,列出所有可能的購(gòu)買(mǎi)方案。
[輸入格式]
一個(gè)整數(shù)n(0<=n<=30),表示錢(qián)數(shù)。
[輸出格式]
如果存在方案,則在輸出中每行輸出一組方案,以\t鍵隔開(kāi)。所有的方案按照母雞的只數(shù)從小到大的順序輸出。如果母雞的只數(shù)相等,則按照公雞的只數(shù)從小到大的順序輸出。
如果沒(méi)有組合方案,輸出三個(gè)0。
[樣例輸入1]
20
[樣例輸出1]
1 1 5
1 4 3
1 7 1
2 1 4
2 4 2
3 1 3
3 4 1
4 1 2
5 1 1
[樣例輸入2]
3
[樣例輸出2]
0 0 0
#include<iostream>
using namespace std;
int main()

{
int n;
cin>>n;
int x,y,z,temp;
int NumberOfSolutions=0;
if (n<3)
{
cout<<"0\t0\t0"<<endl;
}
for (x=1;3*x<=n;x++)
{
for (y=1;2*y<=n;y++)
{
temp=n-3*x-2*y;
if (temp<=0)
{
break;
}
if (temp%3==0)
{
z=temp/3;
NumberOfSolutions++;
cout<<x<<'\t'<<y<<'\t'<<z<<'\t'<<endl;
}
}
}
if (NumberOfSolutions==0)
{
cout<<"0\t0\t0"<<endl;
}
return 0;
} 


