1. 程式人生 > >判斷一個數是否能分解為某個連續正整數序列之和

判斷一個數是否能分解為某個連續正整數序列之和

#include   <stdio.h>
void fun( int  n )
{  int  j, b, c, m, flag=0;
   for (b=1; b<=n/2; b++) { 
      m = n;
      c = b;
      while (m !=0 && m>=c) {
        m = m - c;    c++;
      }
      if ( m==0)
      {  printf("%d=", n);
         for (j=b; j<c-1; j++)   printf( "%d+", j  );
         printf
("%d\n", j); flag=1; } } if(flag==0) printf("不能分解\n"); } main() { int n; printf("輸入 : "); scanf("%d", &n); fun(n); }