#include <stdio.h>
void print_equal(int x[], int nx, int pre, int suf) { for(int i=0; i<pre-1; i++) printf("%d+",x[i]); printf("%d=",x[pre-1]);
for(i=nx-1; i>suf+1; i--) printf("%d+",x[i]); printf("%d\n",x[suf+1]); }
int main() { int x[] = {1,3,6,2,1,4,5,2,1}; int nx = sizeof(x)/sizeof(int); int pre_sum = 0, suf_sum = 0;
int pre = 0,suf = nx-1;
while(pre<=nx-1 && suf>=0) { if(pre_sum < suf_sum) { pre_sum = pre_sum + x[pre]; pre++; } else if(pre_sum > suf_sum) { suf_sum = suf_sum + x[suf]; suf--; } else { if(pre_sum != 0) { print_equal(x,nx,pre,suf); } pre_sum = pre_sum + x[pre]; suf_sum = suf_sum + x[suf]; pre++; suf--; } } print_equal(x,nx,nx,-1); //最后pre值为nx,suf值为-1
return 0; }
|