回溯法之Additive equations

来源:互联网 发布:如何做好软件项目经理 编辑:程序博客网 时间:2024/06/05 21:14

Additive equations


Time Limit: 10 Seconds      Memory Limit: 32768 KB


    We all understandthat an integer set is a collection of distinct integers. Now the question is:given an integer set, can you find all its addtive equations? To explain whatan additive equation is, let's look at the following examples: 
    1+2=3 is an additive equation of the set {1,2,3}, sinceall the numbers that are summed up in the left-hand-side of the equation,namely 1 and 2, belong to the same set as their sum 3 does. We consider 1+2=3and 2+1=3 the same equation, and will always output the numbers on theleft-hand-side of the equation in ascending order. Therefore in this example,it is claimed that the set {1,2,3} has an unique additive equation 1+2=3.
    It is not guaranteed that any integer set has its onlyadditive equation. For example, the set {1,2,5} has no addtive equation and theset {1,2,3,5,6} has more than one additive equations such as 1+2=3, 1+2+3=6,etc. When the number of integers in a set gets large, it will eventually becomeimpossible to find all the additive equations from the top of our minds --unless you are John von Neumann maybe. So we need you to program the computerto solve this problem.

Input

The input data consists of several test cases. 
The first line of the input will contain an integer N, which is the number oftest cases. 
Each test case will first contain an integer M (1<=M<=30), which is thenumber of integers in the set, and then is followed by M distinct positiveintegers in the same line.

Output

For each test case, you are supposed to output all the additive equations ofthe set. These equations will be sorted according to their lengths first( i.e,the number of integer being summed), and then the equations with the samelength will be sorted according to the numbers from left to right, just likethe sample output shows. When there is no such equation, simply output"Can't find any equations." in a line. Print a blank line after eachtest case.

Sample Input

3

3 1 2 3

3 1 2 5

6 1 2 3 5 4 6

Output for the Sample Input

1+2=3

 

Can't find anyequations.

 

1+2=3

1+3=4

1+4=5

1+5=6

2+3=5

2+4=6

1+2+3=6

 

题目意思:本题要求找出所有满足等式的式子并输出。

解题思路:本题是回溯法的典型应用。需要注意的是对输出序列的排序。

具体的代码如下:

#include<iostream>

#include<algorithm>

#include<vector>

using namespacestd;

#define maxN 35

int value[maxN];

int key[maxN];

int N;

int current;

vector<vector<int>> display;

 

void DFS(int i,intlen,int sum);

boolcomp(vector<int> v1,vector<int> v2);

int main()

{

       int n;

       cin>>n;

       while(n>0)

       {

              display.clear();

              cin>>N;

              for(int i=0;i<N;i++)

              {

                     cin>>value[i];

                     key[i]=0;

              }

              sort(value,value+N);

              for(int i=2;i<N;i++)

              {

                     current=0;

                     DFS(0,i,value[i]);

              }

              if(display.size()>0)

              {

                     sort(display.begin(),display.end(),comp);

                     for(inti=0;i<display.size();i++)

                     {

                            int j;

                            for(j=0;j<display[i].size()-1;j++)

                            {

                                   if(j==0)

                                          cout<<display[i][j];

                                   else

                                          cout<<"+"<<display[i][j];

                            }

                            cout<<"="<<display[i][j]<<endl;

                     }

              }

              else

              {

                     cout<<"Can'tfind any equations."<<endl;

              }

              cout<<endl;

              n-=1;

       }

       return 0;

}

void DFS(int i,intlen,int sum)

{

       if(i==len)

       {

              if(current==sum)

              {

                     vector<int> temp;

                     for(int j=0;j<len;j++)

                     {

                            if(key[j]!=0)

                            {

                                   temp.push_back(key[j]);

                            }

                     }

                     temp.push_back(sum);

                     display.push_back(temp);

              }

       }

       else

       {

              if(current+value[i]<=sum)

              {

                     current+=value[i];

                     key[i]=value[i];

                     DFS(i+1,len,sum);

                     key[i]=0;

                     current-=value[i];

              }

                     DFS(i+1,len,sum);

       }

}

boolcomp(vector<int> v1,vector<int> v2)

{

       if(v1.size()>v2.size())

              return false;

       else if(v1.size()<v2.size())

              return true;

       else

       {

              for(int i=0;i<v1.size();i++)

              {

                     if(v1[i]>v2[i])

                            return false;

                     if(v1[i]<v2[i])

                            return true;

              }

       }

}

 

 

0 0
原创粉丝点击