View Code of Problem 133

//数列S中有n个整数,判断S中是否存在两个数A、B,使之和等于X
#include<stdio.h>
#include<math.h>
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n,x;
		scanf("%d %d",&n,&x);
		int i,j;
		int a[n];
		for(i=0;i<n;i++){
			scanf("%d",&a[i]);
		}
		int temp;
		for(i=1;i<n;i++){
			temp=a[i];
			for(j=i-1;temp<a[j]&&j>=0;j--){
				a[j+1]=a[j];
			}
			a[j+1]=temp;
		}
		int flag=0;
		int low=0;
		int high=n-1;
		while(low<high){
			if(a[low]+a[high]<x) low++;
			else if(a[low]+a[high]>x) high--;
			else{
				flag=1;
				break;
			}
		}
		if(flag==1){
			printf("YES\n");
		}else{
			printf("NO\n");
		}
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 133