-
Notifications
You must be signed in to change notification settings - Fork 2
/
MoneySplit.cpp
138 lines (130 loc) · 2.78 KB
/
MoneySplit.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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
//大钱(整型)和小钱(向量数组)
struct SMoney
{
int big_money = -1;
vector<int> small_money;
};
//接受最后的结果
string split_result;
//商和余数,商好像是quotient,原谅我的词汇量#-#
typedef struct
{
int result;
int remainer;
}SDivision;
//函数原型
SMoney MoneyRead();
SDivision GetRemainer(int d1, int d2);
void MoneySplit(int bm, vector<int> sm, int len);
int main()
{
SMoney s;
s = MoneyRead();
if (s.big_money == -1)
{
return -1;
}
MoneySplit(s.big_money, s.small_money, s.small_money.size());
cout << split_result << endl;
return 0;
}
SMoney MoneyRead()
{
SMoney m;
//获得大钱的面值
string big_money;
cout << "请输入大钱的面值:";
cin >> big_money;
int i = 0;
while (i < big_money.length())
{
if (isdigit(big_money[i]));
else
{
cout << "输入错误。面值必须是整数";
return m;
}
i++;
}
//获得小钱的面值,小钱种类不能超过五项
vector<string> small_money(1);
int j = 0;
cout << "请输入小钱" << (j + 1) << "的面值:";
cin >> small_money[0];
while (small_money[j] != "#")
{
j++;
if (j >= 5)
{
break;
}
small_money.resize(j+1);
cout << "请输入小钱" << (j + 1) << "的面值:";
cin >> small_money[j];
}
small_money.pop_back();
int k;
for (k = 0; k < small_money.size(); k++)
{
int p = 0;
while (p < small_money[k].length())
{
if (isdigit(small_money[k][p]));
else
{
cout << "输入错误。面值必须是整数";
return m;
}
p++;
}
}
m.small_money.resize(small_money.size());
for (k = 0; k < small_money.size(); k++)
{
(m.small_money)[k] = atoi(small_money[k].c_str());
}
m.big_money = atoi(big_money.c_str());
return m;
}
SDivision GetRemainer(int d1, int d2)
{
int result, remainer;
result = d1 / d2;
remainer = d1 - d2 * result;
SDivision d;
d.result = result;
d.remainer = remainer;
return d;
}
void MoneySplit(int bm, vector<int> sm, int len)
{
SDivision d;
d = GetRemainer(bm, sm[sm.size()-1]);
if (d.remainer == 0)
{
// 我这里都是以字串的形式把结果直接输出到屏幕,C++不如python接口多,vector用着好不习惯,乃们可以改进
split_result += string(" ", 2 * (len - sm.size())) + to_string(sm[sm.size() - 1]) + "*";
split_result += to_string(d.result) + "\n";
return;
}
int i;
for (i = d.result; i >= 0; i--)
{
vector<int> new_sm = vector<int>(sm.begin(), sm.end() - 1);
split_result += string(" ", 2 * (len - sm.size())) + to_string(sm[sm.size() - 1]) + "*";
split_result += to_string(i);
if (new_sm.size() == 0)
{
split_result += " NA\n";
return;
}
split_result += "\n";
MoneySplit(bm - i * sm[sm.size() - 1], new_sm, len);
}
return;
}