-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCoin_Change_Class.cpp
140 lines (135 loc) · 2.61 KB
/
Coin_Change_Class.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//coin change
//unlimited coins
//limited coins
//How_many_ways_to_make?
//Can_make?
class CoinChange_un
{
int _coinCnt;
int _moneyLimit;
vector<int>_Coin;
vector<int>_moneyRange;
public:
CoinChange_un(int n,int k)
{
_coinCnt=n;
_moneyLimit=k;
_moneyRange=vector<int>(k+9,0);
}
CoinChange_un()
{
_coinCnt=0;
_moneyLimit=0;
}
void getCoin(int n)
{
_coinCnt=n;
}
void getMoney(int k)
{
_moneyLimit=k;
_moneyRange=vector<int>(k+9,0);
}
void _coinInput()
{
for(int i=0; i<_coinCnt; i++)
{
int x;
scanf("%d",&x);
_Coin.push_back(x);
}
sort(_Coin.begin(),_Coin.end());
}
void calc()
{
_moneyRange[0]=1;
for(int i=0; i<_Coin.size(); i++)
for(int j=_Coin[i]; j<=_moneyLimit; j++)
_moneyRange[j]+=_moneyRange[j-_Coin[i]];
}
bool CanMake(int k)
{
if(_moneyRange[k])
return 1;
return 0;
}
int Ways_to_make(int k)
{
return _moneyRange[k];
}
};
class CoinChange_lm
{
int _coinCnt;
int _moneyLimit;
vector< pair<int,int> >coins;
//coin value--coin count;
vector<int>Money_limit;
public:
CoinChange_lm()
{
_coinCnt=0;
_moneyLimit=0;
}
CoinChange_lm(int n,int k)
{
_coinCnt=n;
_moneyLimit=k;
Money_limit=vector<int>(k+9,0);
}
void getCoin(int n)
{
_coinCnt=n;
}
void getMoney(int k)
{
_moneyLimit=k;
Money_limit=vector<int>(k+9,0);
}
void _coinInput()
{
int x,y;
for(int i=0; i<_coinCnt; i++)
{
scanf("%d %d",&x,&y);
coins.push_back(make_pair(x,y));
}
sort(coins.begin(),coins.end());
}
void calc()
{
Money_limit[0]=1;
for(int i=0;i<_coinCnt;i++)
for(int j=0;j<coins[i].second;j++)
for(int k=_moneyLimit;k>=coins[i].first;k--)
Money_limit[k]+=Money_limit[k-coins[i].first];
}
int Ways_to_make(int k)
{
return Money_limit[k];
}
bool CanMake(int k)
{
return Money_limit[k]>0?1:0;
}
};
int main()
{
CoinChange_un CcU;
int n,k,q;
scanf("%d %d",&n,&k);
CcU.getCoin(n);
CcU.getMoney(k);
CcU._coinInput();
CcU.calc();
while(1)
{
scanf("%d",&q);
if(q<0)
break;
printf("%d\n",CcU.Ways_to_make(q));
}
}