51nod 1090 3 count and 0 & amp; 51nod 1267 4 count and 0 (marked as binary)

Source: Internet
Author: User

51nod 1090 3 count and 0 & amp; 51nod 1267 4 count and 0 (marked as binary)

Question meaning:

The sum of 3 is 0:

 

An unordered array with the length of N is given. The elements in the array are integers, positive and negative values include 0, and they are not equal to each other. Find the combination of all three numbers with and = 0. If there is No such combination, No Solution is output. If there are multiple, the minimum number of three numbers is sorted from small to large. If the minimum number is equal, the minimum number is sorted by the second small number.

Input
1st rows, 1 count N, N is the length of the array (0 <= N <= 1000) 2nd-N + 1 row: A [I] (-10 ^ 9 <= A [I] <= 10 ^ 9)
Output
If No combination meets the conditions, No Solution is output. If there are more than one, the smallest of the three numbers is sorted from small to large. If the smallest number is equal, the sorting continues according to the second small number. There are three numbers in each row, separated by spaces in the middle, and these three numbers are arranged in ascending order.
Input example
7-3-2-10123
Output example
-3 0 3-3 1 2-2 -1 3-2 0 2-1 0 1

Question Analysis:

This question can be divided into two parts. First, the sum of any two numbers is calculated and saved to array B to sort arrays a (original array) and B, judge a [I = 0] + B [j = (n * (n-1)] = 0 ?, If the numbers of three are not equal to 0, the result array is saved (the results are not repeated during output), and the result set is output. For details, refer to the code. It is a bit messy. Sorry!

 

AC code:

#include
 
  #include#include
  
   #define MAX 1001using namespace std;int a[MAX],b[3];struct node{    int si,sj;}s[MAX*(MAX-1)];struct Snode{    int si,sj,sk;}p[MAX*(MAX+5)];int cmp1(node a,node b){    if(a.si+a.sj<=b.si+b.sj) return 1;    return 0;}int cmp2(Snode a,Snode b){    if(a.si
   
    >n){        int k=0;        for(int i=0;i
    
     >a[i];            for(int j=0;j
     
      =0){            if(s[j].si+s[j].sj+a[i]==0){                if(a[i]!=s[j].si&&a[i]!=s[j].sj){                    ok=0;                    b[0]=a[i]; b[1]=s[j].si; b[2]=s[j].sj;                    sort(b,b+3);                    p[kk].si=b[0];                    p[kk].sj=b[1];                    p[kk++].sk=b[2];                }                j--;            }            else if(s[j].si+s[j].sj+a[i]<0){                i++;            }            else if(s[j].si+s[j].sj+a[i]>0){                j--;            }        }        //cout<
      
     
    
   
  
 

Whether the sum of the four numbers is 0

Http://www.51nod.com/onlineJudge/questionCode.html! ProblemId = 1267

 

Given N integers, you can determine whether four numbers can be selected. If their sum is 0, Yes is output; otherwise, No is output. Input
1st rows, 1 count N, N is the length of the array (4 <= N <= 1000) 2nd-N + 1 rows: A [I] (-10 ^ 9 <= A [I] <= 10 ^ 9)
Output
If you can select four numbers so that their sum is 0, Yes is output; otherwise, No is output.
Input example
5-11-524
Output example
Yes

Question Analysis:
This question and the sum of the three numbers is 0, and the algorithm is the same, but here the sum of array B and oneself is 0, whether to compare the subscript when the two are equal, as long as the addition is to save the subscript of the two numbers.

 

AC code:

/*** Number of duplicates in a sequence * first, the two numbers are added to a sequence, and then the sequence and the sequence are added to 0 * to determine whether the four numbers are equal, because there may be repeated numbers, you can only use the * position judgment and the subscript of the two numbers recorded for comparison. * If the number is equal to 0 and not equal, YES. Otherwise, NO */# include
 
  
# Include
  
   
# Define MAX 1001 using namespace std; int a [MAX]; struct node {int I, j; int sum;} s [MAX * (MAX-1)]; int cmp1 (node a, node B) {if (. sum <= B. sum) return 1; return 0;} int main () {int n; while (cin> n) {int k = 0; for (int I = 0; I
   
    
> A [I]; for (int j = 0; j
    
     
0) {j -- ;}} if (OK) cout <
     
    
   
  
 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.