1 #include <stdio.h> 2 3 void show(unsigned int value, unsigned int start, unsigned seq_end) 4 { 5 int i = 0; 6 printf("%d = %d + ", value, start); 7 for(i = 1; i < seq_end; i++) 8 { 9 printf("%d + ", start + i); 10 } 11 printf("%d\n", start + seq_end); 12 } 13 14 /* 15 * 功能:连续正整数和测试 16 * 若存在则输出相应的等式 17 * 18 * 返回:返回存在的组数,不存在则返回0 19 */ 20 int integer_solve(unsigned value) 21 { 22 unsigned int start = 0; 23 unsigned int all_group = 0; 24 if(!(value &(value-1)))//2^n必定不满足 25 { 26 return 0; 27 } 28 for(start = 1; start <= (value/2);start++)//起始值必小于(value/2); 29 { 30 unsigned int count_start = 0; 31 unsigned int count_end = (value+1)/2 - start;//终值必小于(value+1)/2,获得最多测试项数 32 unsigned int sum = 0; 33 for(count_start = 1; count_start <= count_end; count_start++) 34 { 35 sum = (2 * start + count_start) * (count_start + 1)/2; 36 if(sum >= value) 37 break; 38 sum = 0; 39 } 40 if(sum) 41 { 42 if(sum == value) 43 { 44 all_group++; 45 show(value, start, count_start); 46 } 47 } 48 } 49 return all_group; 50 } 51 52 int main(void) 53 { 54 int i = 0; 55 for(i = 0; i <= 100; i++) 56 { 57 unsigned int all_group = 0; 58 all_group = integer_solve(i); 59 if(all_group) 60 { 61 printf("********************************\n"); 62 printf("********************************\n"); 63 } 64 } 65 return 0;