题目: 代码: #include<stdio.h> #include<stdlib.h> int f[501][501]={0},value[501]={0},book[501]={0},ans[501][501]={0}; int n,k; int max(int a,int b) { if(a>b)return a; return b; } int min(int a,int b) { if(a<b)return a; return b; } void printf_ans(int i,int e) { int j; if(i==1){ printf("%d ",1); return;} for(j=i-1;j<e;j++) if(max(f[i-1][j],value[e]-value[j])==f[i][e]) { if(i-1==1){ printf("%d %d\n%d ",1,j,j+1); return; } printf_ans(i-1,j); printf("%d\n%d ",j,j+1); break; } } int main() { int i,j,m,p,q; scanf("%d %d",&n,&k); for(i=n;i>=1;i--){ scanf("%d",&book[i]);//??????????????????value??1??i?????????? } for(i=1;i<=n;i++)value[i]=value[i-1]+book[i]; for(i=1;i<=k;i++){ f[i][i]=max(f[i-1][i-1],book[i]);//??????f??????????????fi?????????????? } for(i=1;i<=n;i++) f[1][i]=value[i];//???????????????????????? for(i=2;i<=k;i++){//????1?????????????????????????????? for(j=1;j<=n;j++)//????ii?????????????? { //f[i][j]=value[j]-value[1]; for(m=0;m<=j-1;m++) if(f[i][j]==0){ f[i][j]=max(f[i-1][m],value[j]-value[m]); } else { if(f[i][j]>=max(f[i-1][m],value[j]-value[m])){ f[i][j]=max(f[i-1][m],value[j]-value[m]); ans[i][j]=m; } } } } //printf("%d",f[k][n]); //printf_ans(k,n); //printf("%d\n",n); j=n; for(i=k;i>=1;i--){ printf("%d %d\n",n-j+1,n-ans[i][j]); j=ans[i][j]; } /*p=1; q=f[1][k]; while(q<=n){ printf("%d %d\n",p,q-1); p=q; q=f[q][k-1]; k--; } printf("%d %d\n",p,q-1);*/ //system("pause"); return 0; } 后记: 啊,,所以一开始就从后面输入,这样就可以保证前面的人少。在研究一下