Project Euler task 31
In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:
1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).
It is possible to make £2 in the following way:
1£1 + 150p + 220p + 15p + 12p + 31p
How many different ways can £2 be made using any number of coins?
----------------------------------------------------------------------------------
#include < iostream >
using namespace std;
const unsigned int ciNumber = 200;
// coins: 100, 50, 20, 10, 5, 2, 1
int main() {
int iVariants = 0;
for (int i = 0; i <= ciNumber / 100; i++) {
int I = i * 100;
for (int j = 0; j <= ciNumber / 50; j++) {
int J = j * 50;
if (I + J > 200)
break;
for (int k = 0; k <= ciNumber / 20; k++) {
int K = k * 20;
if (I + J + K > 200)
break;
for (int l = 0; l <= ciNumber / 10; l++) {
int L = l * 10;
if (I + J + K + L > 200)
break;
for (int m = 0; m <= ciNumber / 5; m++) {
int M = m * 5;
if (I + J + K + L + M > 200)
break;
for (int n = 0; n <= ciNumber / 2; n++) {
int N = n * 2;
if (I + J + K + L + M + N > 200)
break;
iVariants++;
}
}
}
}
}
}
// one variant is added - a separate coin of 2 pounds itself )
cout << "Variants: " << iVariants + 1 << endl;
return 0;
}
Comments