View Code of Problem 133

#include<iostream.h>
void QuickSort(int S[],int start,int end)  //快速排序
{
	if(start<end)
	{
		int tmp=S[start],i=start,j=end;
		while(i!=j)
		{
			while(tmp<=S[j]&&i<j)
				j--;
			while(tmp>=S[i]&&i<j)
				i++;
			if(i<j)
			{
				int temp;
				temp=S[i];
				S[i]=S[j];
				S[j]=temp;
			}
		}
		S[start]=S[i];
		S[i]=tmp;
		QuickSort(S,start,i-1);
		QuickSort(S,i+1,end);
	}
 
}
 
bool SearchSumX(int S[],int X,int start,int end)
{
	if(S[start]>=X)
		return false;
	int i=start,j=end;
	while(i<j)
	{
		if((S[i]+S[j]<X)&&i<j)
			i++;
		else if((S[i]+S[j]>X)&&i<j)
			j--;
		else if((S[i]+S[j]==X)&&i<j)
			return true;
	}
	if(i==j)
		return false;
}
 
void main()
{
  
	int S[10]={12,4,9,18,10,1,25,11,6,7};
	
	int X=20;
 
	QuickSort(S,0,sizeof(S)/sizeof(int)-1);
 
 
	if(SearchSumX(S,X,0,sizeof(S)/sizeof(int)-1))
		cout<<"Exist!"<<endl;
	else
		cout<<"No Exist!"<<endl;
	 
}

/*
Main.c:1:9: fatal error: iostream.h: No such file or directory
 #include<iostream.h>
         ^~~~~~~~~~~~
compilation terminated.
*/

Double click to view unformatted code.


Back to problem 133