嚯,世间竟有如此之奇女子,也是跪了# Apple - 家有苹果
l*y
1 楼
Given n types of coin denominations of values, makes change for an amount of
money C with as few coins as possible. 找零钱是O(Cn);
今天看到一个print_all_combination 的 implementation, very straightforward,
那位大侠给分析一下复杂度, 这个应该不低于O(Cn)
int cent = 100; // how to make change for 1 dollar with unlimited
// quarter, dime, nickel and cent
int comb = 0; // how many combinations;
for (int i = 0; i <= cent/ 25; i++) {
int cent_q = cent - i * 25;
for (int j = 0; j <= cent_q / 10; j++) {
int cent_d = cent_q - j * 10;
for (int k = 0; k <= cent_d / 5; k++) {
int cent_n = cent_d - k * 5;
cout << i << '\t' << j << '\t' << k << '\t' << cent_n << endl;
comb++;
}
}
}
money C with as few coins as possible. 找零钱是O(Cn);
今天看到一个print_all_combination 的 implementation, very straightforward,
那位大侠给分析一下复杂度, 这个应该不低于O(Cn)
int cent = 100; // how to make change for 1 dollar with unlimited
// quarter, dime, nickel and cent
int comb = 0; // how many combinations;
for (int i = 0; i <= cent/ 25; i++) {
int cent_q = cent - i * 25;
for (int j = 0; j <= cent_q / 10; j++) {
int cent_d = cent_q - j * 10;
for (int k = 0; k <= cent_d / 5; k++) {
int cent_n = cent_d - k * 5;
cout << i << '\t' << j << '\t' << k << '\t' << cent_n << endl;
comb++;
}
}
}