//快速寻找满足条件的2个数 int icy[]={1,2,3,4,5,6,7,8,9,10}; int i=0; int j=9; int key=11; while(i<j) { if(icy[i]+icy[j]==key) { std::cout<<icy[i]<<" "<<icy[j]<<" "; i++; j--; std::cout<<endl; } else if(icy[i]+icy[j]<key) { i++; } else if(icy[i]+icy[j]>key) { j--; } }
//计算差为 key i=0; j=1; while(j<=9&&i<j) { if(icy[j]-icy[i]==key) { std::cout<<icy[i]<<" "<<icy[j]<<" "; i++; j++; std::cout<<endl; } else if(icy[j]-icy[i]>key) { i++; } else { j++; } }