Problem
You have 100 coins which needs to be distributed among 3 beggars A, B, C.
In how many ways you can distribute these 100 coins to all the 3 beggars.
Constraint: A cannot have more than 75 coins, B cannot have more than 50 coins,
C cannot have more than 25 coins. Write complete code covering all the edge cases.
Also suggest test cases.
Solution
/*
============================================================================
Author : James Chen
Email : a.james.chen@gmail.com
Description : Distribute coins
Created Date : 09-07-2013
Last Modified :
============================================================================
*/
#include <iostream>
#include <iomanip>
using namespace std;
int DistributeCoin(int total, int A, int B, int C)
{
int count(0);
for(int i = 25; i <= A; ++i){
for(int j = 0; j <= B && (i + j) <= 100; ++j){
if(100 - i - j <= C){
count ++;
}
}
}
return count;
}
int DistributeCoin1(int total, int A, int B, int C)
{
int count(0);
for(int i = 25; i <= A; ++i){
for(int j = 0; j <= B; ++j){
for(int k = 0; k <=C; ++k){
if(i + j + k == total){
count ++;
}
}
}
}
return count;
}
int main(int argc, char* argv[])
{
cout << DistributeCoin(100, 75, 50, 25) << endl;
cout << DistributeCoin1(100, 75, 3, 25) << endl;
for(int i = 0; i <= 50; ++i)
{
cout << i << "-- " << DistributeCoin1(100, 75, i, 25) << endl;
}
for(int i = 0; i <= 25; ++i)
{
cout << i << "-- " << DistributeCoin(100, 75, 50, i) << endl;
}
return 0;
}
Output
1001
10
0-- 1
1-- 3
2-- 6
3-- 10
4-- 15
5-- 21
6-- 28
7-- 36
8-- 45
9-- 55
10-- 66
11-- 78
12-- 91
13-- 105
14-- 120
15-- 136
16-- 153
17-- 171
18-- 190
19-- 210
20-- 231
21-- 253
22-- 276
23-- 300
24-- 325
25-- 351
26-- 377
27-- 403
28-- 429
29-- 455
30-- 481
31-- 507
32-- 533
33-- 559
34-- 585
35-- 611
36-- 637
37-- 663
38-- 689
39-- 715
40-- 741
41-- 767
42-- 793
43-- 819
44-- 845
45-- 871
46-- 897
47-- 923
48-- 949
49-- 975
50-- 1001
0-- 26
1-- 53
2-- 81
3-- 110
4-- 140
5-- 171
6-- 203
7-- 236
8-- 270
9-- 305
10-- 341
11-- 378
12-- 416
13-- 455
14-- 495
15-- 536
16-- 578
17-- 621
18-- 665
19-- 710
20-- 756
21-- 803
22-- 851
23-- 900
24-- 950
25-- 1001
Press any key to continue . . .