2014年5月12日 星期一

n顆不同的球,抽m次,每次都放回,列出所有可能(由小而大)

#include <iostream>
#include <cmath>
#include <stack>
using namespace std;
int N,M; //#balls, #draw
int main () { 
 cin >> N >> M;
 int size=(int)pow((float)N,(float)M);
 for (int i=0;i<size;i++){
  int a=i;
  stack<int> s;
  for (int j=0;j<M;j++){
   s.push(a%N);
   a = a/N;
  }
  while(!s.empty()){
   cout << s.top() << ' ';
   s.pop();
  }
  cout << endl;
 }
}


3 2

0 0 
0 1 
0 2 
1 0 
1 1 
1 2 
2 0 
2 1 
2 2 

沒有留言:

張貼留言