本文共 1324 字,大约阅读时间需要 4 分钟。
温故知新,基础复习(一个有序从大到小不重复的数列,任意给出一个sum值,求出数列中所有满足和为sum的数对)
#include#include void PrintSumNumbers(int Arra[],int ASize,int Sum){ //O(1) if (ASize<2) { printf("The size of the Arra is invalid.\n"); return; } if(Sum>=0) { if(Arra[ASize-1]>=Sum || Arra[0]<=0) { printf("The minimum value is bigger than the sum or the maximum value is equal or lesser than 0. \n"); return; } } if (Sum<0) { if (Arra[0]<=Sum || Arra[ASize-1]>=0) { printf("The maximum value is smaller than the sum or the minimum value is equal or greater than 0. \n"); return; } } int LeftIndex=0,RightIndex=ASize-1; int MinTempValue = Arra[ASize-1]; int MaxTempValue = Arra[0]; if (Sum>=0&&MinTempValue<0) { if (MaxTempValue>=0) { MinTempValue = Sum-MaxTempValue; for (int i = ASize-1; i != 0; i--) { if (Arra[i]>=MinTempValue) { RightIndex=i; break; } } } } else { MaxTempValue = Sum-Arra[ASize-1]; for(int i=0;i Sum) { LeftIndex++; } else { RightIndex--; } }}int main(){ int Arra[] = {2147483647,2147483646,9,8,7,6,5,4,3,2,1,0,-1,-2,-3,-2147483646,-2147483647,-2147483647-1}; PrintSumNumbers(Arra,sizeof(Arra)/sizeof(Arra[0]),2147483647);}
转载地址:http://flrfa.baihongyu.com/