2014年5月29日 星期四

只有列出符合的

a.out

1+1+3+5=10
1+1+5+3=10
1+3+1+5=10
1+3+3+3=10
1+3+5+1=10
1+5+1+3=10
1+5+3+1=10
3+1+1+5=10
3+1+3+3=10
3+1+5+1=10
3+3+1+3=10
3+3+3+1=10
3+5+1+1=10
5+1+1+3=10
5+1+3+1=10
5+3+1+1=10

#include <iostream>
#include <cmath>
using namespace std;
int N,M; //#balls, #draw
int target; //目標值
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]; 
 }
 
 cin >> target;
 
 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; 
  amount+=B[A[M-1]];
  for (int j=1;j<M;j++){
   
   amount+=B[A[M-j-1]];
  }
  
  if (amount==target){
   cout << B[A[M-1]];
   for (int j=1;j<M;j++){
    cout << '+' << B[A[M-j-1]] ;   
   }
   cout << "=" << amount ;
   cout << endl;
  }
 }
}

沒有留言:

張貼留言