日期:2014-05-18 浏览次数:21244 次
#include <stdio.h> #include <stdlib.h> #define SIZE 16 int main() { int arr[SIZE]={ 1,3,5,7,8,9,10,20,21,22,23,24,25,40,41,42 }; //arr=qsort(arr); int arrSub[SIZE-1]; for (int i=0;i<SIZE-1;i++) { arrSub[i]=arr[i+1]-arr[i]; } bool firstElement=true; int subSofar=0; int sub=arrSub[0]; //stackJ stackSub for (int j=1;j<SIZE-1;j++) { int curSub=arrSub[j]; if (curSub==sub) { if(firstElement) { printf("%d,%d,%d,",arr[j-1],arr[j],arr[j+1]); firstElement=false; } else { printf("%d,",arr[j+1]); } } else if(curSub<sub) { subSofar+=curSub; if (subSofar==sub) { printf("%d,",arr[j+1]); subSofar=0; } else if(subSofar>sub) { subSofar=0; firstElement=true; printf("\n"); } } else { if (curSub>=SIZE-1-j) { sub=arrSub[++j]; } else { sub=curSub; //stackJ.push(j); //stackSub.push(arrSub[j+1]); } firstElement=true; subSofar=0; printf("\n"); } // if(j==SIZE-1) // { // j=stackJ.pop(); // sub=stackSub.pop(): //} } system("pause"); return 0; }