2014年5月29日 星期四

N顆球抽M次球上有數字,求抽出的球上的數字的加總,加總數字是否為某個數字

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

#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;
  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 << " ";
  if (amount==target)
   cout << "Y";
  else
   cout << "N";
  cout << endl;
 }
}

沒有留言:

張貼留言