C程序找零钱

2023年 8月 29日 16.2k 0

C程序找零钱

在这个问题中,我们给定一个值n,我们想要找零n卢比,并且我们有n个硬币,每个硬币的面值从1到m不等。我们需要返回能够组成这个总和的方式的总数。

例子

Input : N = 6 ; coins = {1,2,4}.
Output : 6
Explanation : The total combination that make the sum of 6
is :
{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
int coins( int S[], int m, int n ) {
int i, j, x, y;
int table[n+1][m];
for (i=0; i= 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程序找零钱的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论