有10个变量,每个变量可能是1,2,3这三个数。怎样用算法把所有的排列列举出来?
比如:
1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,2
1,1,1,1,1,1,1,1,1,3,
1,1,1,1,1,1,1,1,2,1
1,1,1,1,1,1,1,1,2,2
......要求
1用递归
2不用递归答出一个给一半分,两个分全给。

解决方案 »

  1.   

    给个可借鉴的例子:有红,黄,蓝,白,黑五种颜色的球,任意取三个不同颜色的球,有多少种取法?(一个简单的组合题),下面的代码算法没有优化.........要是假如从100个里面取50个,那$@*$@&(*^$@*@&&^^!**#include<iostream.h>#include<conio.h>void main(){enum Color {red,yellow,blue,white,black};int count(0);Color pri;clrscr();for (Color i=red;i<=black;i++)for (Color j=i+1;j<=black;j++)for (Color k=j+1;k<=black;k++){count++;cout.width(10);for (int t=0;t<=2;t++){switch(t){case 0:pri=Color(i);break;case 1:pri=Color(j);break;case 2:pri=Color(k);break;default:break;}switch(pri){case red:cout<<" red";break;case yellow:cout<<" yellow";break;case blue:cout<<" blue";break;case white:cout<<" white";break;case black:cout<<" black";break;default :break;}}cout<<endl;}cout<<endl<<"Total:"<<count<<endl;return;}
      

  2.   

    递归的例子:
    123 
    132213231312321function ZsList(mStr: string): string; { 全排列 }procedure pZsList(mLeft, mRight: string);varI, L: Integer;Temp: string;beginL := Length(mLeft);if L = 0 thenResult := Result + mRight + #13#10else for I := 1 to L do beginTemp := mLeft;Delete(Temp, I, 1);pZsList(Temp, Concat(mRight, mLeft[I]));end;end; { [ZsList<pZsList>] }beginResult := '';pZsList(mStr, '');end; { ZsList }
      

  3.   

    只能用递归,你看看类pascal的例子,有很多答案的,
      

  4.   

    你要多少列就要用多少个for语句