1元的硬币分成cents 1,2,5 第一个比较好,省一层循环,速度快,第二个三重循环,太慢 #include "stdio.h" #include "stdlib.h" void main() { int sum, five, two, one, line; sum = 0; line=0; for( five=0; five<100/5; five++ ) for( two=0; two<=(100-5*five)/2; two++ ) { one = 100-5*five-2*two; sum++; if(++line>20) { (void)getchar(); line=0;printf("-----/n"); } printf("%d %d %d/n", five, two, one ); } printf("sum=%d/n", sum ); } /* 穷举法求硬币分法 */ #include <stdio.h> main() { int five=0,two=0,one=0,total=0; int i=0,j=0,k=0; for (i=0; i <= 20 ; ++i) { for (j=0;j <= 50; ++j) { for (k=0; k <= 100; ++k) { total = i * 5 + j * 2 + k; if (total == 100) { printf("five-->%d/ntwo-->%d/none-->%d/n",i,j,k); } } /* end of k */ } /* end of j */ } /* end of i */ }