>  기사  >  백엔드 개발  >  변화를 찾는 C 프로그램

변화를 찾는 C 프로그램

PHPz
PHPz앞으로
2023-08-29 08:37:06878검색

변화를 찾는 C 프로그램

이 문제에는 n이라는 값이 주어졌고, n 루피를 바꾸고 싶고, n개의 동전이 있고, 각 동전의 액면가는 1에서 m까지입니다. 우리는 이 합이 형성될 수 있는 총 방법 수를 반환해야 합니다.

Example

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

의 중국어 번역은

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;
}

Output

The total number of combinations of coins that sum up to 4 is 4
입니다.

위 내용은 변화를 찾는 C 프로그램의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제