//算法分析与设计 王晓东版 算法实现2-2题 #include void findModel_3(int s[],int l,int r) { int ll = 0, rr = 0; //分治求解时中位数两边的边界 int mid = s[(l + r ) / 2]; //查找边界 for(int i = l; i <= r; i++) if(s[i] == mid) { ll = i; break; } for(i = r; i >= l; i--) if(s[i] == mid) { rr = i; break; } //最大重数小于中位数个数时赋值 if(max < rr-ll+1) { max = rr - ll + 1; mode = mid; //return mode,max; } //若分割的字部分长度大于中位数长度,递归求解子问题 if(ll - l > max) findModel_3(s, l, ll-1); if(r - rr > max) findModel_3(s, rr+1, r); } void main() { FILE *fp; int s[100],n; if ((fp=fopen(\"input.txt\ { printf(\"error!\\n\"); return ; } if(fscanf(fp,\"%d\ { printf(\"error!\\n\"); return; } for(int i=1;i<=n;i++) { if(fscanf(fp,\"%d\ { printf(\"error!\\n\"); return; } } fclose(fp); findModel_3(s,1,n); fp=fopen(\"output.txt\ fprintf(fp,\"%d\\n%d\ } 因篇幅问题不能全部显示,请点此查看更多更全内容