Topic Meaning:
3 Number of the and for 0:
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1090
Given an unordered array of length n, the elements in the array are integers, with positive negative including 0, and not equal to each other. A combination of 3 numbers from which all and = 0 are found. If there is no such combination, the output is no solution. If there are more than one, the smallest of the 3 numbers is sorted from small to large, and if the smallest number is equal then the second small number is sorted.
Input
Line 1th, 1 number n,n the length of the array (0 <= N <= 1000) 2-n + 1 line: A[i] ( -10^9 <= a[i] <= 10^9)
Output
If there are no qualifying combinations, the output is no solution. If there are more than one, the smallest number in the 3 number is ordered from small to large, and if the smallest number is equal, it continues to be sorted by a second small number. Each line is 3 numbers, separated by a space, and the 3 numbers are arranged in order from small to large.
Input Example
7-3-2-10123
Output Example
-3 0 3-3 1 2-2-1 3-2 0 2-1 0 1
Topic Analysis:
This problem can be two points, the first calculation of any two number and stored in the array B, to sort a (the original array) and B array, to determine a[i=0]+b[j= (n (n-1))]=0?, if three number 22 and equal to 0, then to the result array, (guaranteed output results are not duplicated), The final output result set. See the code, write a bit messy, hope forgive me!
AC Code:
<span style= "FONT-SIZE:18PX;" > #include <iostream> #include <algorithm> #include <cmath> #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<b.si) return 1; else if (A.SI==B.SI&&A.SJ<B.SJ) return 1; else if (a.si==b.si&&a.sj==b.sj&&a.sk<b.sk) return 1; return 0;} int main () {int n; while (cin>>n) {int k=0; for (int i=0;i<n;i++) {cin>>a[i]; for (int j=0;j<i;j++) {s[k].si=a[i]; S[K++].SJ=A[J]; }} sort (a,a+n); Sort (S,S+K,CMP1); int i=0,j=k-1,ok=1,kk=0; while (i<n&&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<<kk<<endl; Sort (P,P+KK,CMP2); if (OK) cout<< "No solution" <<endl; else{cout<<p[0].si<< "" <<p[0].sj<< "" <<p[0].sk<<endl; for (i=1;i<kk;i++) {if (p[i].si==p[i-1].si&&p[i].sj==p[i-1].sj&&p[i].sk==p[i-1].sk) contin Ue else cout<<p[i].si<< "" <<p[i].sj<< "" <<p[i].sk<<endl; }}} return 0;} </span>
Four-digit and whether 0
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1267
given n integers, you can determine if you can choose 4 numbers, and they are 0, then output "Yes" or Output "No". Input
Line 1th, 1 number n,n the length of the array (4 <= N <= 1000) 2-n + 1 line: A[i] ( -10^9 <= a[i] <= 10^9)
Output
If you can select 4 numbers so that they are 0, the output is "Yes", otherwise the output "No".
Input Example
5-11-524
Output Example
Yes
Topic Analysis:
The subject and three number of the sum of 0, the same as the algorithm, just here the array B and its own sum is 0, compare whether 22 is equal, you need to compare the subscript, as long as the addition is the two number of subscript corresponding to save up.
AC Code:
<span style= "FONT-SIZE:18PX;" >/** * Sequence may have a number of repetitions * First two numbers are added to a sequence, and then the sequence and the sum of this sequence is equal to 0 * judgment is to pay attention to the comparison whether four numbers are equal, because there may be a number of repetitions, can only be judged by the * position, and record two number of position subscript to compare, * DeMerit equals 0 and not equal yes, otherwise no */#include <iostream> #include <algorithm> #include <cmath> #define MAX 1001using namespace Std;int a[max];struct node{int i,j; int sum;} s[max* (MAX-1)];int CMP1 (node A,node b) {if (a.sum<=b.sum) return 1; return 0;} int main () {int n; while (cin>>n) {int k=0; for (int i=0;i<n;i++) {cin>>a[i]; for (int j=0;j<i;j++) {///a two-digit and s[k].i=i; S[k].j=j; S[K++].SUM=A[I]+A[J]; }} sort (S,S+K,CMP1); int i=0,j=k-1,ok=1; while (I<=J) {if (s[i].sum+s[j].sum==0) {if (s[i].i!=s[j].i&&s[i].i!=s[j].j&&s[i ].J!=S[J].I&&S[I].J!=S[J].J) {//four subscript any unequal ok=0; Break } i++; j--; } else if (s[i].sum+s[j].sum<0) {i++; } else if (s[i].sum+s[j].sum>0) {j--; }} if (OK) cout<< "No" <<endl; else cout<< "Yes" <<endl; } return 0;} </span>
51nod 1090 3 numbers and 0 & 51nod 1267 4 numbers and 0 (Mark two points)