在这个问题中,我们给定一个值n,我们想要找零n卢比,并且我们有n个硬币,每个硬币的面值从1到m不等。我们需要返回能够组成这个总和的方式的总数。
例子input : n = 6 ; coins = {1,2,4}.output : 6explanation : the total combination that make the sum of 6is :{1,1,1,1,1,1} ; {1,1,1,1,2}; {1,1,2,2}; {1,1,4}; {2,2,2} ; {2,4}.
example的中文翻译为:示例#include <stdio.h>int coins( int s[], int m, int n ) { int i, j, x, y; int table[n+1][m]; for (i=0; i<m; i++) table[0][i] = 1; for (i = 1; i < n+1; i++) { for (j = 0; j < m; j++) { x = (i-s[j] >= 0)? table[i - s[j]][j]: 0; y = (j >= 1)? table[i][j-1]: 0; table[i][j] = x + y; } } return table[n][m-1];}int main() { int arr[] = {1, 2, 3}; int m = sizeof(arr)/sizeof(arr[0]); int n = 4; printf("the total number of combinations of coins that sum up to %d",n); printf(" is %d ", coins(arr, m, n)); return 0;}
输出the total number of combinations of coins that sum up to 4 is 4
以上就是c程序找零钱的详细内容。