View Code of Problem 3686

#include<iostream> 
#include<algorithm>
using namespace std;
//二刷 
int main(){
	int T;
	scanf("%d",&T);
	while(T--) {
		int n, x;   
		scanf("%d %d", &n, &x); 
		int p[n]; //价钱 
		for(int i=0; i<n; i++){
			scanf("%d", &p[i]);
		}
		
		sort(p, p+n); //从小到大排序 
		
		int flag = 0;
		int low = 0, high = n-1;
		while(low < high) {  
			if(p[low] + p[high] == x){
				printf("YES\n");
				flag = 1;
				break;
			}
			else if(p[low] + p[high] < x){
				low++;
			}
			else high--;
		}//查找的循环结束 
	}
  
	if(flag==0){
	    printf("NO\n");
		}
	return 0;
}

/*
Main.cc: In function 'int main()':
Main.cc:18:7: warning: variable 'flag' set but not used [-Wunused-but-set-variable]
   int flag = 0;
       ^~~~
Main.cc:33:5: error: 'flag' was not declared in this scope
  if(flag==0){
     ^~~~
Main.cc:33:5: note: suggested alternative: 'float'
  if(flag==0){
     ^~~~
     float
*/

Double click to view unformatted code.


Back to problem 3686