12
21是两种方案。
谢谢!
/*---------------------------------------------------------------------------
File name: main.cpp
Author: HJin (email: fish_sea_bird [at] yahoo [dot] com )
Created on: 7/14/2007 00:18:59
Environment: Windows XP Professional SP2 English +
Visual Studio 2005 v8.0.50727.762
Modification history:
===========================================================================
Problem statement:
---------------------------------------------------------------------------
小明一顿饭吃N个包子,他有时一口吃1个、有时一口吃2个、有时一口吃3个,
问吃完全部包子有几种吃法?用阁下最擅长的语言写,最好能够在屏幕输出
吃的方案序列。(不考虑溢出等问题。)
12
21
是两种方案。
Question: suppose you have 5 包子. 2 + 3 = 5. Are 2+3 and
3+2 两种方案? The poster could make your problem statement
clearer.
My code treats them as 两种方案.
Analysis:
---------------------------------------------------------------------------
Find all x, y, and z such that 1*x + 2*y + 3*z = n and perumte them.
Sample output:
---------------------------------------------------------------------------
Method 1: 3个* 3
Method 2: 2个* 3 + 3个* 1
Method 3: 3个* 1 + 2个* 3
Method 4: 1个* 1 + 2个* 1 + 3个* 2
Method 5: 1个* 1 + 3个* 2 + 2个* 1
Method 6: 2个* 1 + 1个* 1 + 3个* 2
Method 7: 2个* 1 + 3个* 2 + 1个* 1
Method 8: 3个* 2 + 1个* 1 + 2个* 1
Method 9: 3个* 2 + 2个* 1 + 1个* 1
Method 10: 1个* 1 + 2个* 4
Method 11: 2个* 4 + 1个* 1
Method 12: 1个* 2 + 2个* 2 + 3个* 1
Method 13: 1个* 2 + 3个* 1 + 2个* 2
Method 14: 2个* 2 + 1个* 2 + 3个* 1
Method 15: 2个* 2 + 3个* 1 + 1个* 2
Method 16: 3个* 1 + 1个* 2 + 2个* 2
Method 17: 3个* 1 + 2个* 2 + 1个* 2
Method 18: 1个* 3 + 3个* 2
Method 19: 3个* 2 + 1个* 3
Method 20: 1个* 3 + 2个* 3
Method 21: 2个* 3 + 1个* 3
Method 22: 1个* 4 + 2个* 1 + 3个* 1
Method 23: 1个* 4 + 3个* 1 + 2个* 1
Method 24: 2个* 1 + 1个* 4 + 3个* 1
Method 25: 2个* 1 + 3个* 1 + 1个* 4
Method 26: 3个* 1 + 1个* 4 + 2个* 1
Method 27: 3个* 1 + 2个* 1 + 1个* 4
Method 28: 1个* 5 + 2个* 2
Method 29: 2个* 2 + 1个* 5
Method 30: 1个* 6 + 3个* 1
Method 31: 3个* 1 + 1个* 6
Method 32: 1个* 7 + 2个* 1
Method 33: 2个* 1 + 1个* 7
Method 34: 1个* 9
Totally, there are 34种方案 to eat all the steamed bread.
Press any key to continue . . .
Reference:
---------------------------------------------------------------------------
1. http://bbs.bc-cn.net/viewthread.php?tid=155095
*/
#include <iostream>
using namespace std;
/**
O(n^3) algorithm.
*/
int count(int n);
/**
Permute x, y, z since 1 2 and 2 1 are treated as different methods.
*/
void permute(int x, int y, int z, int n, int&m);
int main(int argc, char** argv)
{
int n=9;
count(n);
return 0;
}
int count(int n)
{
int m=0; // a counter
for(int x=0; x<=n; ++x)
{
for(int y=0; y<=n/2; ++y)
{
for(int z=0; z<=n/3; ++z)
{
if(x+2*y+3*z == n)
{
permute(x, y, z, n, m);
}
}
}
}
cout<<"\nTotally, there are "<<m<<"种方案 to eat all the steamed bread.\n";
return m;
}
void permute(int x, int y, int z, int n, int&m)
{
if(x!=0 && y!=0 && z!=0)
{
++m;
cout<<"Method "<<m<<":\t1个* "<<x<<" + 2个* "<<y <<" + 3个* "<<z<<endl;
++m;
cout<<"Method "<<m<<":\t1个* "<<x<<" + 3个* "<<z <<" + 2个* "<<y<<endl;
++m;
cout<<"Method "<<m<<":\t2个* "<<y<<" + 1个* "<<x <<" + 3个* "<<z<<endl;
++m;
cout<<"Method "<<m<<":\t2个* "<<y<<" + 3个* "<<z <<" + 1个* "<<x<<endl;
++m;
cout<<"Method "<<m<<":\t3个* "<<z<<" + 1个* "<<x <<" + 2个* "<<y<<endl;
++m;
cout<<"Method "<<m<<":\t3个* "<<z<<" + 2个* "<<y <<" + 1个* "<<x<<endl;
return;
}
// after this point, only two of them could be non-zero.
if(x!=0 && y!=0) // z == 0
{
++m;
cout<<"Method "<<m<<":\t"<<"1个* "<<x <<" + 2个* "<<y<<endl;
++m;
cout<<"Method "<<m<<":\t"<<"2个* "<<y <<" + 1个* "<<x<<endl;
return;
}
if(x!=0 && z!=0) // y == 0
{
++m;
cout<<"Method "<<m<<":\t"<<"1个* "<<x <<" + 3个* "<<z<<endl;
++m;
cout<<"Method "<<m<<":\t"<<"3个* "<<z <<" + 1个* "<<x<<endl;
return;
}
if(y!=0 && z!=0) // x == 0
{
++m;
cout<<"Method "<<m<<":\t"<<"2个* "<<y <<" + 3个* "<<z<<endl;
++m;
cout<<"Method "<<m<<":\t"<<"3个* "<<z <<" + 2个* "<<y<<endl;
return;
}
// after this point, only one of them could be non-zero.
if(x!=0)
{
++m;
cout<<"Method "<<m<<":\t"<<"1个* "<<x<<endl;
}
if(y!=0)
{
++m;
cout<<"Method "<<m<<":\t"<<"2个* "<<y<<endl;
}
if(z!=0)
{
++m;
cout<<"Method "<<m<<":\t"<<"3个* "<<z<<endl;
}
}
/*---------------------------------------------------------------------------
File name: eatSteamedBread.cpp
Author: HJin (email: fish_sea_bird [at] yahoo [dot] com )
Created on: 7/14/2007 00:18:59
Environment: Windows XP Professional SP2 English +
Visual Studio 2005 v8.0.50727.762
Modification history:
===========================================================================
7/16/2007 05:11:28
-----------------------------------------------------------
modified the algorithm so that is O(n^2) in time.
Problem statement:
---------------------------------------------------------------------------
小明一顿饭吃N个包子,他有时一口吃1个、有时一口吃2个、有时一口吃3个,
问吃完全部包子有几种吃法?用阁下最擅长的语言写,最好能够在屏幕输出
吃的方案序列。(不考虑溢出等问题。)
12
21
是两种方案。
Question: suppose you have 5 包子. 2 + 3 = 5. Are 2+3 and
3+2 两种方案? The poster could make your problem statement
clearer.
My code treats them as 两种方案.
Analysis:
---------------------------------------------------------------------------
Find all x, y, and z such that 1*x + 2*y + 3*z = n and perumte them.
Sample output:
---------------------------------------------------------------------------
Method 1: 3个* 4
Method 2: 2个* 3 + 3个* 2
Method 3: 3个* 2 + 2个* 3
Method 4: 2个* 6
Method 5: 1个* 1 + 2个* 1 + 3个* 3
Method 6: 1个* 1 + 3个* 3 + 2个* 1
Method 7: 2个* 1 + 1个* 1 + 3个* 3
Method 8: 2个* 1 + 3个* 3 + 1个* 1
Method 9: 3个* 3 + 1个* 1 + 2个* 1
Method 10: 3个* 3 + 2个* 1 + 1个* 1
Method 11: 1个* 1 + 2个* 4 + 3个* 1
Method 12: 1个* 1 + 3个* 1 + 2个* 4
Method 13: 2个* 4 + 1个* 1 + 3个* 1
Method 14: 2个* 4 + 3个* 1 + 1个* 1
Method 15: 3个* 1 + 1个* 1 + 2个* 4
Method 16: 3个* 1 + 2个* 4 + 1个* 1
Method 17: 1个* 2 + 2个* 2 + 3个* 2
Method 18: 1个* 2 + 3个* 2 + 2个* 2
Method 19: 2个* 2 + 1个* 2 + 3个* 2
Method 20: 2个* 2 + 3个* 2 + 1个* 2
Method 21: 3个* 2 + 1个* 2 + 2个* 2
Method 22: 3个* 2 + 2个* 2 + 1个* 2
Method 23: 1个* 2 + 2个* 5
Method 24: 2个* 5 + 1个* 2
Method 25: 1个* 3 + 3个* 3
Method 26: 3个* 3 + 1个* 3
Method 27: 1个* 3 + 2个* 3 + 3个* 1
Method 28: 1个* 3 + 3个* 1 + 2个* 3
Method 29: 2个* 3 + 1个* 3 + 3个* 1
Method 30: 2个* 3 + 3个* 1 + 1个* 3
Method 31: 3个* 1 + 1个* 3 + 2个* 3
Method 32: 3个* 1 + 2个* 3 + 1个* 3
Method 33: 1个* 4 + 2个* 1 + 3个* 2
Method 34: 1个* 4 + 3个* 2 + 2个* 1
Method 35: 2个* 1 + 1个* 4 + 3个* 2
Method 36: 2个* 1 + 3个* 2 + 1个* 4
Method 37: 3个* 2 + 1个* 4 + 2个* 1
Method 38: 3个* 2 + 2个* 1 + 1个* 4
Method 39: 1个* 4 + 2个* 4
Method 40: 2个* 4 + 1个* 4
Method 41: 1个* 5 + 2个* 2 + 3个* 1
Method 42: 1个* 5 + 3个* 1 + 2个* 2
Method 43: 2个* 2 + 1个* 5 + 3个* 1
Method 44: 2个* 2 + 3个* 1 + 1个* 5
Method 45: 3个* 1 + 1个* 5 + 2个* 2
Method 46: 3个* 1 + 2个* 2 + 1个* 5
Method 47: 1个* 6 + 3个* 2
Method 48: 3个* 2 + 1个* 6
Method 49: 1个* 6 + 2个* 3
Method 50: 2个* 3 + 1个* 6
Method 51: 1个* 7 + 2个* 1 + 3个* 1
Method 52: 1个* 7 + 3个* 1 + 2个* 1
Method 53: 2个* 1 + 1个* 7 + 3个* 1
Method 54: 2个* 1 + 3个* 1 + 1个* 7
Method 55: 3个* 1 + 1个* 7 + 2个* 1
Method 56: 3个* 1 + 2个* 1 + 1个* 7
Method 57: 1个* 8 + 2个* 2
Method 58: 2个* 2 + 1个* 8
Method 59: 1个* 9 + 3个* 1
Method 60: 3个* 1 + 1个* 9
Method 61: 1个* 10 + 2个* 1
Method 62: 2个* 1 + 1个* 10
Method 63: 1个* 12
Totally, there are 63种方案 to eat all the steamed bread.
Press any key to continue . . .
Reference:
---------------------------------------------------------------------------
1. http://bbs.bc-cn.net/viewthread.php?tid=155095
*/
#include <iostream>
using namespace std;
/**
O(n^2) algorithm.
*/
int count(int n);
/**
Permute x, y, z since 1 2 and 2 1 are treated as different methods.
Note that if you treat 1 2 and 2 1 as the same, you don't need to permute.
*/
void permute(int x, int y, int z, int n, int&m);
int main(int argc, char** argv)
{
int n=12;
count(n);
return 0;
}
int count(int n)
{
int m=0; // a counter
int rem;
int nMinusxOver2; // stores (n-x)/2
for(int x=0; x<=n; ++x)
{
nMinusxOver2 = (n - x) >> 1;
for(int y=0; y<=nMinusxOver2; ++y)
{
rem = n - x - 2*y;
if(rem % 3 == 0)
{
permute(x, y, rem/3, n, m);
}
}
}
cout<<"\nTotally, there are "<<m<<"种方案 to eat all the steamed bread.\n";
return m;
}
void permute(int x, int y, int z, int n, int&m)
{
if(x!=0 && y!=0 && z!=0)
{
++m;
cout<<"Method "<<m<<":\t1个* "<<x<<" + 2个* "<<y <<" + 3个* "<<z<<endl;
++m;
cout<<"Method "<<m<<":\t1个* "<<x<<" + 3个* "<<z <<" + 2个* "<<y<<endl;
++m;
cout<<"Method "<<m<<":\t2个* "<<y<<" + 1个* "<<x <<" + 3个* "<<z<<endl;
++m;
cout<<"Method "<<m<<":\t2个* "<<y<<" + 3个* "<<z <<" + 1个* "<<x<<endl;
++m;
cout<<"Method "<<m<<":\t3个* "<<z<<" + 1个* "<<x <<" + 2个* "<<y<<endl;
++m;
cout<<"Method "<<m<<":\t3个* "<<z<<" + 2个* "<<y <<" + 1个* "<<x<<endl;
return;
}
// after this point, only two of them could be non-zero.
if(x!=0 && y!=0) // z == 0
{
++m;
cout<<"Method "<<m<<":\t"<<"1个* "<<x <<" + 2个* "<<y<<endl;
++m;
cout<<"Method "<<m<<":\t"<<"2个* "<<y <<" + 1个* "<<x<<endl;
return;
}
if(x!=0 && z!=0) // y == 0
{
++m;
cout<<"Method "<<m<<":\t"<<"1个* "<<x <<" + 3个* "<<z<<endl;
++m;
cout<<"Method "<<m<<":\t"<<"3个* "<<z <<" + 1个* "<<x<<endl;
return;
}
if(y!=0 && z!=0) // x == 0
{
++m;
cout<<"Method "<<m<<":\t"<<"2个* "<<y <<" + 3个* "<<z<<endl;
++m;
cout<<"Method "<<m<<":\t"<<"3个* "<<z <<" + 2个* "<<y<<endl;
return;
}
// after this point, only one of them could be non-zero.
if(x!=0)
{
++m;
cout<<"Method "<<m<<":\t"<<"1个* "<<x<<endl;
}
if(y!=0)
{
++m;
cout<<"Method "<<m<<":\t"<<"2个* "<<y<<endl;
}
if(z!=0)
{
++m;
cout<<"Method "<<m<<":\t"<<"3个* "<<z<<endl;
}
}