Please put in a dight(0<x) 123 -------------------------------------------------------------- 61+62=123 40+41+42=123 18+19+20+21+22+23=123 --------------------------------------------------------------- exist 3 cases.
int main() { int n,i,j,a; printf("Please input a dight(>0)"); scanf("%d",&n); for (i=2;i<=n/2;i++) if ((n*2)%i==0) { if(((2*n/i-i+1)%2==0)&&((2*n/i-i+1>0))) { a=(2*n/i-i+1)/2; printf("%d",a); for (j=1;j<i;j++) printf("+%d",a+j); printf("\n"); } } return 0; } 看看有无问题
我的 感觉是数学问题 #include<stdio.h> void main() { void conl(long a,int n,int *p); int esti(long a); long a; int n = 0,i,j=0; printf("input the number\n"); scanf("%ld",&a); n = esti(a); for(i=2;i<=n;i++) conl(a,i,&j); printf("There are %d cases.\n",j); } int esti(long a) { int i; for(i=1;;i++) if((1+i)*i/2>=a) break; return i; } void conl(long a,int n,int *p) { long b=0; int i; b = (a - n*(n-1)/2)/n; if(b*n+n*(n-1)/2==a) { (*p)++; for(i=0;i<=n-1;i++) printf("%ld+",b+i); printf("\b=%ld\n",a); } }
int main(void) { long left, right; long sum; long GIVEN; scanf("%ld",&GIVEN);
for (sum = 0, left = right = 1; left < GIVEN/2+1; right++) { sum += right; while (sum > GIVEN) { sum -= left; left++; } if (sum == GIVEN) printf("%ld - %ld\n", left, right); } return 0; }