Title: There are 1, 5, 10, 50, 100, 500 yuan coins each several pieces, now to use these coins to pay a dollar, at least how many coins?
There is at least one payment scheme for the fake question.
Use the greedy algorithm, the first choice of the largest coins, and constantly adjust the number of coins.
Code:
* * * main.cpp * * Created on:2014.7.17 *
author:spike/
/*eclipse CDT, gcc 4.8.1*/
#include <stdio.h>
#include <limits.h>
#include <utility>
#include <queue >
using namespace std;
Class Program {
const int v[6] = {1, 5, Ten, m};
int c[6] = {3, 2, 1, 3, 0, 2};
int A = 620;
Public:
void Solve () {
int ans = 0;
for (int i=5; i>=0; i--) {
int t = min (A/v[i], c[i]);
A-= T*v[i];
Ans + = t;
}
printf ("result =%d\n", ans);
}
;
int main (void)
{program
P;
P.solve ();
return 0;
}
Output:
result = 6
This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
Author: csdn Blog Mystra