1 #include < conio.h > 2 #include < stdlib.h > 3 #include < stdio.h > 4 #include < dos.h > 5 6 7 #define NUM 5 8 #define LOWER 1 9 #define UPPER 50 10 #define MATCH_TOTAL_NUM 100 11 #define OUTPUT_FILENAME "result.txt" 12 13 void CountNext( int * num, int length, int total); 14 15 int g_num[NUM]; 16 static int g_totalWays = 0 ; 17 FILE * g_fp; 18 19 void CountNext( int * num, int length, int total) 20 { 21 if(total < 0)22 { 23 return;24 }25 for(int i = LOWER; i <= UPPER; i++)26 { 27 if(length != NUM - 1) 28 { 29 if(g_num[length+1] >= i)30 continue;31 }32 33 g_num[length] = i; 34 35 if(length == 0) 36 { 37 if(total != i)38 { 39 if(i == UPPER)40 { 41 return;42 }43 continue; 44 }45 else46 { 47 g_totalWays++; 48 for(int idx = 0; idx < NUM; idx++)49 { 50 fprintf(g_fp, "%d ", g_num[idx]);51 }52 fputs("\n", g_fp); 53 }54 return;55 }56 57 CountNext(num, length - 1, total - i);58 }59} 60 61 int main() 62 { 63 unlink(OUTPUT_FILENAME);64 g_fp = fopen(OUTPUT_FILENAME, "at");65 CountNext(g_num, NUM - 1, MATCH_TOTAL_NUM); 66 fclose(g_fp);67 68 printf("Total ways = %d\n", g_totalWays);69 getch(); 70 return 0;71} 72