Created
October 5, 2012 18:34
-
-
Save Higgcz/3841572 to your computer and use it in GitHub Desktop.
Number of combination for coins changing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
long change(const int sum, const int coins[], const int size) { | |
if ( sum == 0 ) return 0; | |
if ( coins == NULL || size == 0 ) return 0; | |
long * out = new long[sum + 1]; | |
out[0] = 1; | |
for ( int i = 0; i < size; i++ ) { | |
for ( int j = coins[i]; j <= sum; j++ ) { | |
out[j] += out[j - coins[i]]; | |
} | |
} | |
return out[sum]; | |
} | |
int main(int argc, const char * argv[]) | |
{ | |
int coins[] = {50, 25, 10, 5, 1}; | |
int size = sizeof(coins) / sizeof(*coins); | |
int in = 0; | |
while ( std::cin >> in ) { | |
std::cout << change(in, coins, size) << "\n"; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package coinChange; | |
import java.util.Scanner; | |
/** | |
* | |
* @author vojtechmicka | |
*/ | |
public class CoinChange { | |
public static long change(int sum, int[] coins) { | |
if ( sum == 0 ) return 0; | |
if ( coins == null || coins.length == 0 ) return 0; | |
long out[] = new long[sum + 1]; | |
out[0] = 1; | |
for ( int i = 0; i < coins.length; i++ ) { | |
for ( int j = coins[i]; j <= sum; j++ ) { | |
out[j] += out[j - coins[i]]; | |
} | |
} | |
return out[sum]; | |
} | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
Scanner s = new Scanner(System.in); | |
int[] coins = {50, 25, 10, 5, 1}; | |
while ( s.hasNextInt() ) { | |
System.out.println(change(s.nextInt(), coins)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment