Problem Description
There are n numbers A1,A2....An{A}_{1},{A}_{2}....{A}_{n}A?1??,A?2??....A?n??,your task is to check whether there exists there different positive integers i, j, k (1≤i,j,k≤n1\leq i , j , k \leq n1≤i,j,k≤n) such that Ai−Aj=Ak{A}_{i}-{A}_{j}={A}_{k}A?i??−A?j??=A?k??
Input
There are multiple test cases, no more than 1000 cases. First line of each case contains a single integer n.(3≤n≤100)(3\leq n\leq 100)(3≤n≤100). Next line contains n integers A1,A2....An{A}_{1},{A}_{2}....{A}_{n}A?1??,A?2??....A?n??.(0≤Ai≤1000)(0\leq {A}_{i}\leq 1000)(0≤A?i??≤1000)
Output
For each case output "YES" in a single line if you find such i, j, k, otherwise output "NO".
Sample Input
3 3 1 2 3 1 0 2 4 1 1 0 2
Sample Output
YES NO YES 大水题,快排一下两个循环搞一下就好 23333 用goto嘲讽了,我的锅
#include<iostream> #include<stdio.h> #include<algorithm> #include<string.h> using namespace std; int donser(const void *a,const void *b) { return *(int *)a-*(int *)b; } int main() { freopen("in.txt","r",stdin); int i,j,k,m,d[2000]; while(~scanf("%d",&m)) { for(i=1;i<=m;i++) { scanf("%d",&d[i]); } qsort(d+1,m,sizeof(int),donser); if(m<3){cout<<"NO\n";goto n;} for(i=1;i<=m-2;i++) { for(k=i+1;k<=m-1;k++) { for(j=k+1;j<=m;j++) { if(d[i]+d[k]==d[j]){cout<<"YES\n";goto n;} } } } cout<<"NO\n"; n: m=0; for(i=1;i<=m;i++) { d[i]=0; } } return 0; }
问题描述
给n个数A1,A2....An{A}_{1},{A}_{2}....{A}_{n}A?1??,A?2??....A?n??,从中选3个位置不同的数A,B和C,问是否有一种情况满足A-B=C.
输入描述
输入有多组数据,不超过1000组. 每组数据第一行包含一个整数n,随后一行n个整数A1,A2....An{A}_{1},{A}_{2}....{A}_{n}A?1??,A?2??....A?n??.(3≤n≤1003\leq n\leq 1003≤n≤100,0≤Ai≤10000\leq {A}_{i}\leq 10000≤A?i??≤1000)
输出描述
对于每组数据如果符合条件输出"YES",否则输出"NO".
输入样例
3 3 1 2 3 1 0 2 4 1 1 0 2
输出样例
YES NO YES
时间: 2024-10-05 19:11:59