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 }
  • 相关阅读:
    FBI网络欺诈投诉中心:经济萧条期网络犯罪更汹涌 狼人:
    阿根廷黑客盯上中国网银用户 警惕“IK网银盗号器” 狼人:
    参数定义sql 递归查询子目录
    输入字符ASCII码排序
    程序池程序Win7(64位)中IIS配置Access数据库的asp.net程序中出现“未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序”(解决了)
    乱码中文javaEE 中文乱码问题
    地方浏览器document.getElementById('resForm1_bookTimeStart').value = "${bookTimeStartId}"; var hzfpZldm=$("#hz
    打印下标iOS 6字面量
    函数集成redis与Spring集成
    安装内容[Python]第三方库Scrapy入门使用
  • 原文地址:https://www.cnblogs.com/dongsheng/p/3153523.html
Copyright © 2011-2022 走看看