#include<stdio.h>
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int num[266];
int count=0;
for(int i=1;i<4;i++)
{
count=0;
num[count]=i;
n=n-i;
if(n==0){
for(int h=0;h<=count;h++){
printf("%d ",num[h]);
}
printf("\n");
}
if(n<0){
break;
}
count++;
for(int j=1;j<4;j++)
{
n=n-j;
num[count]=j;
if(n==0){
for(int h=0;h<=count;h++){
printf("%d ",num[h]);
}
printf("\n");
}
if(n<0){
break;
}
count++;
for(int i1=1;i1<4;i1++)
{
num[count]=i1;
n=n-i1;
if(n==0)
{
for(int h=0;h<=count;h++)
{
printf("%d ",num[h]);
}
printf("\n");
}
if(n<0){
break;
}
count++;
for(int m=1;m<4;m++)
{
num[count]=m;
n=n-m;
if(n==0)
{
for(int h=0;h<=count;h++)
{
printf("%d ",num[h]);
}
printf("\n");
}
if(n<0)
{
break;
}
count++;
}
}
}
}
}
return 0;
}
要求輸出所有情況
示例:比如輸入4(最多到達四層),每次走1/2/3三種情況
輸出 1111
112
121
13
211
22
31
uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
僅供參考://每一步只能邁上1個或2個臺階。先邁左腳,然后左右交替,最后一步是邁右腳,也就是說一共要走偶數步。那么,上完39級臺階,有多少種不同的上法呢?
//-------------------------------------------------------
//狀態c[i][0]表示走到第i個樓梯時最后一步是左腳的方法數,c[i][1]是右腳的方法數。。
//那么,由于每一步能上一到兩級,c[i][0] = c[i-1][1]+c[i-2][1](因為最后一步為左腳,
//倒數第二步肯定為右腳。。)。。然后一直遞推。。最后c[39][1]即上到第39級而且是右腳的方法數即為答案。
#include <stdio.h>
int c[40][2];//0為左腳,1為右腳。。
int main(){
int i;
c[0][0] = c[1][1] = 0;
c[0][1] = c[1][0] = 1;
for(i = 2; i <= 39; ++i){
c[i][0] = c[i - 1][1] + c[i - 2][1];
c[i][1] = c[i - 1][0] + c[i - 2][0];
}
printf("%d\n", c[39][1]);
return 0;
}
//-------------------------------------------------------
//走到第n級臺階的時候,他的上一步不是在n-1級就是在n-2級上,
// f(n) = f(n - 1) + f(n - 2) 很簡單的遞回思想
int num=0;
void fun(int n,int step)
{
if(n<0)
return;
if(n==0)
{
if(step%2==0) num++;
return;
}
fun(n-1,step+1);
fun(n-2,step+1);
}
void main()
{
fun(39,0);
printf("%d",num);
}
//-------------------------------------------------------
int aux(int steps, int cnt)
{
if (steps <= 0)
{
if (0 == steps && (cnt && !(cnt % 2)))
return 1;
return 0;
}
return aux(steps - 1, cnt + 1) + aux(steps - 2, cnt + 1);
}<br>
inline int func(steps)
{
return aux(steps, 0);
}
//-------------------------------------------------------
#include <stdio.h>
static int trace[40];
static int count=0;
void go(int, int, int);
int main()
{
go(39, 0, 1);
go(39, 0, 2);
printf("count=%d\n",count);
}
//
// left: left steps
// mt: mount_times
// step: steps of current movement
//
void go(int left, int mt, int step)
{
int i;
if(left < 0)return;
if(left == 0){
if(!(mt%2)&&(step==1)){
/* 用來記錄小明的腳步,當然題目里并不要求我們記下來。
for(i=0;i<mt;i++){
printf("%3d",trace[i]);
}
printf("\n");
for(i=mt;i<sizeof(trace);i++){
trace[i]=0;
}
*/
count++;
}
return;
}
trace[mt] = step;
left -= step;
mt++;
go(left, mt, 1);
go(left, mt, 2);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/270280.html
標籤:C++ 語言
上一篇:copy建構式問題
