您的位置:首页 > 其它

【算法】【动态规划】Coin Exchange

2015-08-28 10:42 225 查看

算法描述:

Korean coins consist of 6 levels of 1 won, 5 won, 10 won, 50 won, 100 won, and 500 won. If you make 256 won change, you need 5 coins: two coins of 100 won, one coin of 50 won, one coin of 5 won, and one coin of
1 won.



If you make 8 won change in a country that coin unit consists of three levels of 1 won, 4 won and 6 won, you can form one coin of 6 won & two coins of 1 won, and two coins of 4 won. In the first case, you need total 3 coins, and
in the second case, you will need 2 coins in total. If the target is to make coins by the minimum number of coins, you should choose the second case. When coin units & wanted changes are given, find out the required minimum number to make change. The number
of coins you have is infinite.

Time Limit: 1 seconds for 10 cases. (java 2 seconds) (If your program exceeds this time limit, the answers that have been already printed are ignored and the score becomes 0. So, it may be better to print a wrong answer when
a specific test case might cause your program to exceed the time limit. One guide for the time limit excess would be the size of the input.)

[Input]

Several test cases can be included in the inputs. C, the number of cases is given in the first row of the inputs. After that, the test cases as many as C (1 ≤ C ≤ 10) are given in a row.

Kinds of coins, N are given on the first row per each test case. (1 ≤ N ≤ 10)

Units of each coin, M are given separately with a blank on the second row. (1 ≤ M ≤ W)

Changes to give, W are given on the third row. (1 ≤ W ≤ 64000)

[Output]

For the T-th test case, “case #T” should be printed out in the first line, followed by an integer which is

output the minimum number of coins. If it is not possible to make change, output ‘impossible’.

[I/O Example]

Input


2 ← There are two test cases.

3 ← Case #1

1 4 6

8

6 ← Case #2

1 4 5 7 16 20

4758

Output

Case #1

2

Case #2

240

分析:

动态规划

源码:

/*
You should use the statndard input/output

in order to receive a score properly.

Do not use file input and output

Please be very careful.
*/

#include <iostream>
#include <cstring>

using namespace std;

int N;
int Won;
int Coins[10];

int main(int argc, char** argv)
{
int T, test_case;
/*
The freopen function below opens input.txt file in read only mode, and afterward,
the program will read from input.txt file instead of standard(keyboard) input.
To test your program, you may save input data in input.txt file,
and use freopen function to read from the file when using cin function.
You may remove the comment symbols(//) in the below statement and use it.
Use #include<cstdio> or #include <stdio.h> to use the function in your program.
But before submission, you must remove the freopen function or rewrite comment symbols(//).
*/

freopen("input.txt", "r", stdin);

cin >> T;
for (test_case = 0; test_case < T; test_case++)
{

/////////////////////////////////////////////////////////////////////////////////////////////
/*
Implement your algorithm here.
The answer to the case will be stored in variable Answer.
*/
/////////////////////////////////////////////////////////////////////////////////////////////
int i, j;
Won = 0;
memset(Coins, 0x00, sizeof(Coins));

cin >> N;

for (i = 0; i < N; i++)
{
cin >> Coins[i];
}

cin >> Won;
int *coinNum = new int[Won + 1];

coinNum[0] = 0;

for (i = 1; i <= Won; i++)
{
int minNum = i;
for (j = 0; j < N; j++)
{
if (i >= Coins[j])
{
if (coinNum[i - Coins[j]] + 1 <= minNum)
{
minNum = coinNum[i - Coins[j]] + 1;
}
}
}
coinNum[i] = minNum;
}

// Print the answer to standard output(screen).
cout << "Case #" << test_case + 1 << endl;
cout << coinNum[Won] << endl;
delete[]coinNum;
}

return 0;//Your program should return 0 on normal termination.
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: