| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 644 人关注过本帖
标题:[求助]求一算法
只看楼主 加入收藏
bennyhe
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2005-11-28
收藏
 问题点数:0 回复次数:4 
[求助]求一算法
小明一顿饭吃N个包子,他有时一口吃1个、有时一口吃2个、有时一口吃3个,问吃完全部包子有几种吃法?用阁下最擅长的语言写,最好能够在屏幕输出吃的方案序列。(不考虑溢出等问题。)
12
21是两种方案。
谢谢!
搜索更多相关主题的帖子: 算法 
2007-07-13 14:51
leeco
Rank: 4
等 级:贵宾
威 望:10
帖 子:1029
专家分:177
注 册:2007-5-10
收藏
得分:0 

最好能够在屏幕输出吃的方案序列恐怕不是题目的要求吧


#include <stdio.h>

int s[100]={1,1,2},n;

void init_s()
{
int i;
for(i=2;i<1000;i++){
s[i]=s[i-1]+s[i-2]+s[i-3];
}
}

int main()
{
init_s();
while(scanf(\"%d\",&n)!=EOF){
printf(\"%d\n\",s[n]);
}
}

2007-07-14 02:08
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
回复:(bennyhe)[求助]求一算法

/*---------------------------------------------------------------------------
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;
}
}


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-07-14 15:23
bennyhe
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2005-11-28
收藏
得分:0 
强人,谢谢大家。
2007-07-16 19:36
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
回复:(bennyhe)强人,谢谢大家。

/*---------------------------------------------------------------------------
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;
}
}


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-07-16 20:36
快速回复:[求助]求一算法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012155 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved