2014年5月27日 星期二

N顆球抽M次球上有數字,求抽出的球上的數字的加總

a.in
3 4
1 3 5
a.out
1+1+1+1=4
1+1+1+3=6
1+1+1+5=8
1+1+3+1=6
1+1+3+3=8
1+1+3+5=10
1+1+5+1=8
1+1+5+3=10
1+1+5+5=12
1+3+1+1=6
1+3+1+3=8
1+3+1+5=10
1+3+3+1=8
1+3+3+3=10
1+3+3+5=12
1+3+5+1=10
1+3+5+3=12
1+3+5+5=14
1+5+1+1=8
1+5+1+3=10
1+5+1+5=12
1+5+3+1=10
1+5+3+3=12
1+5+3+5=14
1+5+5+1=12
1+5+5+3=14
1+5+5+5=16
3+1+1+1=6
3+1+1+3=8
3+1+1+5=10
3+1+3+1=8
3+1+3+3=10
3+1+3+5=12
3+1+5+1=10
3+1+5+3=12
3+1+5+5=14
3+3+1+1=8
3+3+1+3=10
3+3+1+5=12
3+3+3+1=10
3+3+3+3=12
3+3+3+5=14
3+3+5+1=12
3+3+5+3=14
3+3+5+5=16
3+5+1+1=10
3+5+1+3=12
3+5+1+5=14
3+5+3+1=12
3+5+3+3=14
3+5+3+5=16
3+5+5+1=14
3+5+5+3=16
3+5+5+5=18
5+1+1+1=8
5+1+1+3=10
5+1+1+5=12
5+1+3+1=10
5+1+3+3=12
5+1+3+5=14
5+1+5+1=12
5+1+5+3=14
5+1+5+5=16
5+3+1+1=10
5+3+1+3=12
5+3+1+5=14
5+3+3+1=12
5+3+3+3=14
5+3+3+5=16
5+3+5+1=14
5+3+5+3=16
5+3+5+5=18
5+5+1+1=12
5+5+1+3=14
5+5+1+5=16
5+5+3+1=14
5+5+3+3=16
5+5+3+5=18
5+5+5+1=16
5+5+5+3=18
5+5+5+5=20

#include <iostream>
#include <cmath>
using namespace std;
int N,M; //#balls, #draw
int power(int n,int m){
 int result=1;
 for (int i=0;i<m;i++){result*=n;}
 return result;
}
int main () { 
 cin >> N >> M;
 int size=power(N,M);
 int *A=new int[M];
 
 int *B=new int[N]; //讀進N顆球的數字
 for (int j=0;j<N;j++){
  cin >> B[j]; 
 }
 
 for (int i=0;i<size;i++){
  int a=i;
  for (int j=0;j<M;j++){
   A[j]= a%N ;
   a = a/N;
  }
  int amount=0;
  cout << B[A[M-1]];
  amount+=B[A[M-1]];
  for (int j=1;j<M;j++){
   cout << '+' << B[A[M-j-1]] ;
   amount+=B[A[M-j-1]];
  }
  cout << "=" << amount << endl;
 }
}

沒有留言:

張貼留言