zoukankan      html  css  js  c++  java
  • ZOJ-1204 Additive equations

    Additive equations

    Time Limit: 10 Seconds      Memory Limit: 32768 KB

        We all understand that 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 what an additive equation is, let's look at the following examples: 
        1+2=3 is an additive equation of the set {1,2,3}, since all 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=3 and 2+1=3 the same equation, and will always output the numbers on the left-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 only additive equation. For example, the set {1,2,5} has no addtive equation and the set {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 become impossible 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 computer to 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 of test cases. 
    Each test case will first contain an integer M (1<=M<=30), which is the number of integers in the set, and then is followed by M distinct positive integers in the same line.

    Output

    For each test case, you are supposed to output all the additive equations of the 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 same length will be sorted according to the numbers from left to right, just like the sample output shows. When there is no such equation, simply output "Can't find any equations." in a line. Print a blank line after each test 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 any equations.
    
    1+2=3
    1+3=4
    1+4=5
    1+5=6
    2+3=5
    2+4=6
    1+2+3=6
    

    Source: Zhejiang University Local Contest 2002, Preliminary

    很长时间没有AC了,手都僵了,还有十几天就决赛了,得努力了。。。。。

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <algorithm>
     4 #include <cstring>
     5 
     6 using namespace std;
     7 
     8 int n;
     9 int a[35];
    10 bool vis[35], flag;
    11 
    12 void DFS(int start, int len, int sum)
    13 {
    14     if(len == 0)
    15     {
    16         for(int i = start; i<n && sum>=a[i]; ++i)
    17         {
    18             if(sum == a[i])
    19             {
    20                 flag = true;
    21                 for(int j = 0; j <= i; ++j)
    22                 {
    23                     if(vis[j])
    24                     {
    25                         if(sum == a[j])  //说明是最后一个加数了,这里每次sum都减去加数就是为了方面判断
    26                             printf("%d=%d
    ", a[j], a[i]);
    27                         else
    28                             printf("%d+", a[j]);
    29                         sum -= a[j];
    30                     }
    31                 }
    32             }
    33         }
    34     }
    35     else
    36     {
    37         for(int i = start; i<n; ++i)
    38         {
    39             if(sum + a[i] <= a[n-1])
    40             {
    41                 sum += a[i];
    42                 vis[i] = true;
    43                 --len;
    44                 DFS(i+1, len, sum);
    45                 sum -= a[i];
    46                 vis[i] = false;
    47                 ++len;
    48             }
    49         }
    50     }
    51 }
    52 
    53 int main()
    54 {
    55     int T;
    56     scanf("%d", &T);
    57     while(T--)
    58     {
    59         scanf("%d", &n);
    60         for(int i = 0; i < n; ++i)
    61             scanf("%d", &a[i]);
    62         sort(a, a+n);
    63         memset(vis, false, sizeof(vis));
    64         flag = false;
    65         for(int i = 2; i < n; ++i)
    66             DFS(0, i, 0);
    67         if(!flag)
    68             printf("Can't find any equations.
    
    ");
    69         else
    70             printf("
    ");
    71     }
    72     return 0;
    73 }
  • 相关阅读:
    lua 逻辑运算 and, or, not
    redis和memcache列出所有key
    Linux protobuf
    CGI,FastCGI,PHP-CGI与PHP-FPM
    lua使用笔记2:Linux 中安装php的lua扩展
    lua使用笔记1:Linux 中安装lua
    Android APK反编译详解
    git 常用命令
    git push 403错误解决方法
    linux(centos)搭建SVN服务器
  • 原文地址:https://www.cnblogs.com/dongsheng/p/3153523.html
Copyright © 2011-2022 走看看