Use twocursors. One at front and the other at the end. Keep track of the sum by movingthe cursors.void find2Number(int a[], int n, int dest) { int *f = a, *e=a+n-1; int sum = *f + *e; while (sum != dest && f < e) { if (sum < dest) sum =*(++f); else sum = *(--e); } if (sum == dest) printf(“%d, %d\n”, *f,*e);}
共 1 个关于本帖的回复 最后回复于 2013-8-16 11:10