本帖最后由 jason680 于 2017-12-18 20:39 编辑
回复 1# 会飞的_海豚
$ ./get_coin 50
( 0, 0, 50) ( 0, 1, 45) ( 0, 2, 40) ( 0, 3, 35) ( 0, 4, 30) count=5
( 0, 5, 25) ( 0, 6, 20) ( 0, 7, 15) ( 0, 8, 10) ( 0, 9, 5) count=10
( 0, 10, 0) ( 1, 0, 40) ( 1, 1, 35) ( 1, 2, 30) ( 1, 3, 25) count=15
( 1, 4, 20) ( 1, 5, 15) ( 1, 6, 10) ( 1, 7, 5) ( 1, 8, 0) count=20
( 2, 0, 30) ( 2, 1, 25) ( 2, 2, 20) ( 2, 3, 15) ( 2, 4, 10) count=25
( 2, 5, 5) ( 2, 6, 0) ( 3, 0, 20) ( 3, 1, 15) ( 3, 2, 10) count=30
( 3, 3, 5) ( 3, 4, 0) ( 4, 0, 10) ( 4, 1, 5) ( 4, 2, 0) count=35
( 5, 0, 0)
Total count=36
$ ./get_coin 33
( 0, 0, 33) ( 0, 1, 28) ( 0, 2, 23) ( 0, 3, 18) ( 0, 4, 13) count=5
( 0, 5, 8) ( 0, 6, 3) ( 1, 0, 23) ( 1, 1, 18) ( 1, 2, 13) count=10
( 1, 3, 8) ( 1, 4, 3) ( 2, 0, 13) ( 2, 1, 8) ( 2, 2, 3) count=15
( 3, 0, 3)
Total count=16
$ cat get_coin.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int total = 10;
int count = 0;
void partition(int v10,int v5,int v1) {
int value = v10*10+v5*5+v1;
if(value == total){
printf("(%3d,%3d,%3d) ",v10,v5,v1);
++count;
if(count%5==0)
printf("count=%d\n", count);
return;
}
if(value < total) {
if(value<= total)
partition(v10,v5,v1+total-value);
if(value+ 5 <= total && v1==0)
partition(v10,v5+1,v1);
if(value+10 <=total && v5==0 && v1==0)
partition(v10+1,v5,v1);
}
}
int main(int argc, char**argv) {
if(argc == 2){
if(atoi(argv[1])>0)
total = atoi(argv[1]);
}
partition(0,0,0);
printf("\nTotal count=%d\n",count);
return 0;
}
|