需求背景
项目需要实现一个任务灰度的功能,让用户指定每次灰度的百分比。这其实就是一个整数根据比例分割的问题。
代码
// 整数分割算法
private static ListintegerAllocationAlgorithm (Integer sum, Listpercent) {
int rest = sum;
ListstepValue = new ArrayList<>();
for (int i = 0; i < percent.size(); i++) {
int value = 0;
if (rest > 0) {
value = (int)Math.floor(sum * percent.get(i) / 100);
if (value == 0) {
value = 1;
}
if ((rest - value) <= 0) {
value = rest;
rest = 0;
} else {
rest = rest - value;
}
if (i == (percent.size() - 1) && rest > 0) {
value = value + rest;
}
}
stepValue.add(value);
}
return stepValue;
}
public static void main(String[] args) {
Listpercent = new ArrayList<>();
percent.add(10);
percent.add(25);
percent.add(25);
percent.add(30);
percent.add(9);
percent.add(1);
Listresult = null;
for (int i = 1; i< 200; i++) {
result = integerAllocationAlgorithm(i, percent);
System.out.println(i+ ":" + result + "," + result.stream().reduce(Integer::sum).orElse(0));
}
}
结果:
1:[1, 0, 0, 0, 0, 0],1
2:[1, 1, 0, 0, 0, 0],2
3:[1, 1, 1, 0, 0, 0],3
4:[1, 1, 1, 1, 0, 0],4
5:[1, 1, 1, 1, 1, 0],5
6:[1, 1, 1, 1, 1, 1],6
7:[1, 1, 1, 2, 1, 1],7
8:[1, 2, 2, 2, 1, 0],8
9:[1, 2, 2, 2, 1, 1],9
10:[1, 2, 2, 3, 1, 1],10
11:[1, 2, 2, 3, 1, 2],11
12:[1, 3, 3, 3, 1, 1],12
13:[1, 3, 3, 3, 1, 2],13
14:[1, 3, 3, 4, 1, 2],14
15:[1, 3, 3, 4, 1, 3],15
16:[1, 4, 4, 4, 1, 2],16
17:[1, 4, 4, 5, 1, 2],17
18:[1, 4, 4, 5, 1, 3],18
19:[1, 4, 4, 5, 1, 4],19
20:[2, 5, 5, 6, 1, 1],20
21:[2, 5, 5, 6, 1, 2],21
22:[2, 5, 5, 6, 1, 3],22
23:[2, 5, 5, 6, 2, 3],23
24:[2, 6, 6, 7, 2, 1],24
25:[2, 6, 6, 7, 2, 2],25
26:[2, 6, 6, 7, 2, 3],26
27:[2, 6, 6, 8, 2, 3],27
28:[2, 7, 7, 8, 2, 2],28
29:[2, 7, 7, 8, 2, 3],29
30:[3, 7, 7, 9, 2, 2],30
31:[3, 7, 7, 9, 2, 3],31
32:[3, 8, 8, 9, 2, 2],32
33:[3, 8, 8, 9, 2, 3],33
34:[3, 8, 8, 10, 3, 2],34
35:[3, 8, 8, 10, 3, 3],35
36:[3, 9, 9, 10, 3, 2],36
37:[3, 9, 9, 11, 3, 2],37
38:[3, 9, 9, 11, 3, 3],38
39:[3, 9, 9, 11, 3, 4],39
40:[4, 10, 10, 12, 3, 1],40
41:[4, 10, 10, 12, 3, 2],41
42:[4, 10, 10, 12, 3, 3],42
43:[4, 10, 10, 12, 3, 4],43
44:[4, 11, 11, 13, 3, 2],44
45:[4, 11, 11, 13, 4, 2],45
46:[4, 11, 11, 13, 4, 3],46
47:[4, 11, 11, 14, 4, 3],47
48:[4, 12, 12, 14, 4, 2],48
49:[4, 12, 12, 14, 4, 3],49
50:[5, 12, 12, 15, 4, 2],50
51:[5, 12, 12, 15, 4, 3],51
52:[5, 13, 13, 15, 4, 2],52
53:[5, 13, 13, 15, 4, 3],53
54:[5, 13, 13, 16, 4, 3],54
55:[5, 13, 13, 16, 4, 4],55
56:[5, 14, 14, 16, 5, 2],56
57:[5, 14, 14, 17, 5, 2],57
58:[5, 14, 14, 17, 5, 3],58
59:[5, 14, 14, 17, 5, 4],59
60:[6, 15, 15, 18, 5, 1],60
61:[6, 15, 15, 18, 5, 2],61
62:[6, 15, 15, 18, 5, 3],62
63:[6, 15, 15, 18, 5, 4],63
64:[6, 16, 16, 19, 5, 2],64
65:[6, 16, 16, 19, 5, 3],65
66:[6, 16, 16, 19, 5, 4],66
67:[6, 16, 16, 20, 6, 3],67
68:[6, 17, 17, 20, 6, 2],68
69:[6, 17, 17, 20, 6, 3],69
70:[7, 17, 17, 21, 6, 2],70
71:[7, 17, 17, 21, 6, 3],71
72:[7, 18, 18, 21, 6, 2],72
73:[7, 18, 18, 21, 6, 3],73
74:[7, 18, 18, 22, 6, 3],74
75:[7, 18, 18, 22, 6, 4],75
76:[7, 19, 19, 22, 6, 3],76
77:[7, 19, 19, 23, 6, 3],77
78:[7, 19, 19, 23, 7, 3],78
79:[7, 19, 19, 23, 7, 4],79
80:[8, 20, 20, 24, 7, 1],80
81:[8, 20, 20, 24, 7, 2],81
82:[8, 20, 20, 24, 7, 3],82
83:[8, 20, 20, 24, 7, 4],83
84:[8, 21, 21, 25, 7, 2],84
85:[8, 21, 21, 25, 7, 3],85
86:[8, 21, 21, 25, 7, 4],86
87:[8, 21, 21, 26, 7, 4],87
88:[8, 22, 22, 26, 7, 3],88
89:[8, 22, 22, 26, 8, 3],89
90:[9, 22, 22, 27, 8, 2],90
91:[9, 22, 22, 27, 8, 3],91
92:[9, 23, 23, 27, 8, 2],92
93:[9, 23, 23, 27, 8, 3],93
94:[9, 23, 23, 28, 8, 3],94
95:[9, 23, 23, 28, 8, 4],95
96:[9, 24, 24, 28, 8, 3],96
97:[9, 24, 24, 29, 8, 3],97
98:[9, 24, 24, 29, 8, 4],98
99:[9, 24, 24, 29, 8, 5],99
100:[10, 25, 25, 30, 9, 1],100
101:[10, 25, 25, 30, 9, 2],101
102:[10, 25, 25, 30, 9, 3],102
103:[10, 25, 25, 30, 9, 4],103
104:[10, 26, 26, 31, 9, 2],104
105:[10, 26, 26, 31, 9, 3],105
106:[10, 26, 26, 31, 9, 4],106
107:[10, 26, 26, 32, 9, 4],107
108:[10, 27, 27, 32, 9, 3],108
109:[10, 27, 27, 32, 9, 4],109
110:[11, 27, 27, 33, 9, 3],110
111:[11, 27, 27, 33, 9, 4],111
112:[11, 28, 28, 33, 10, 2],112
113:[11, 28, 28, 33, 10, 3],113
114:[11, 28, 28, 34, 10, 3],114
115:[11, 28, 28, 34, 10, 4],115
116:[11, 29, 29, 34, 10, 3],116
117:[11, 29, 29, 35, 10, 3],117
118:[11, 29, 29, 35, 10, 4],118
119:[11, 29, 29, 35, 10, 5],119
120:[12, 30, 30, 36, 10, 2],120
121:[12, 30, 30, 36, 10, 3],121
122:[12, 30, 30, 36, 10, 4],122
123:[12, 30, 30, 36, 11, 4],123
124:[12, 31, 31, 37, 11, 2],124
125:[12, 31, 31, 37, 11, 3],125
126:[12, 31, 31, 37, 11, 4],126
127:[12, 31, 31, 38, 11, 4],127
128:[12, 32, 32, 38, 11, 3],128
129:[12, 32, 32, 38, 11, 4],129
130:[13, 32, 32, 39, 11, 3],130
131:[13, 32, 32, 39, 11, 4],131
132:[13, 33, 33, 39, 11, 3],132
133:[13, 33, 33, 39, 11, 4],133
134:[13, 33, 33, 40, 12, 3],134
135:[13, 33, 33, 40, 12, 4],135
136:[13, 34, 34, 40, 12, 3],136
137:[13, 34, 34, 41, 12, 3],137
138:[13, 34, 34, 41, 12, 4],138
139:[13, 34, 34, 41, 12, 5],139
140:[14, 35, 35, 42, 12, 2],140
141:[14, 35, 35, 42, 12, 3],141
142:[14, 35, 35, 42, 12, 4],142
143:[14, 35, 35, 42, 12, 5],143
144:[14, 36, 36, 43, 12, 3],144
145:[14, 36, 36, 43, 13, 3],145
146:[14, 36, 36, 43, 13, 4],146
147:[14, 36, 36, 44, 13, 4],147
148:[14, 37, 37, 44, 13, 3],148
149:[14, 37, 37, 44, 13, 4],149
150:[15, 37, 37, 45, 13, 3],150
151:[15, 37, 37, 45, 13, 4],151
152:[15, 38, 38, 45, 13, 3],152
153:[15, 38, 38, 45, 13, 4],153
154:[15, 38, 38, 46, 13, 4],154
155:[15, 38, 38, 46, 13, 5],155
156:[15, 39, 39, 46, 14, 3],156
157:[15, 39, 39, 47, 14, 3],157
158:[15, 39, 39, 47, 14, 4],158
159:[15, 39, 39, 47, 14, 5],159
160:[16, 40, 40, 48, 14, 2],160
161:[16, 40, 40, 48, 14, 3],161
162:[16, 40, 40, 48, 14, 4],162
163:[16, 40, 40, 48, 14, 5],163
164:[16, 41, 41, 49, 14, 3],164
165:[16, 41, 41, 49, 14, 4],165
166:[16, 41, 41, 49, 14, 5],166
167:[16, 41, 41, 50, 15, 4],167
168:[16, 42, 42, 50, 15, 3],168
169:[16, 42, 42, 50, 15, 4],169
170:[17, 42, 42, 51, 15, 3],170
171:[17, 42, 42, 51, 15, 4],171
172:[17, 43, 43, 51, 15, 3],172
173:[17, 43, 43, 51, 15, 4],173
174:[17, 43, 43, 52, 15, 4],174
175:[17, 43, 43, 52, 15, 5],175
176:[17, 44, 44, 52, 15, 4],176
177:[17, 44, 44, 53, 15, 4],177
178:[17, 44, 44, 53, 16, 4],178
179:[17, 44, 44, 53, 16, 5],179
180:[18, 45, 45, 54, 16, 2],180
181:[18, 45, 45, 54, 16, 3],181
182:[18, 45, 45, 54, 16, 4],182
183:[18, 45, 45, 54, 16, 5],183
184:[18, 46, 46, 55, 16, 3],184
185:[18, 46, 46, 55, 16, 4],185
186:[18, 46, 46, 55, 16, 5],186
187:[18, 46, 46, 56, 16, 5],187
188:[18, 47, 47, 56, 16, 4],188
189:[18, 47, 47, 56, 17, 4],189
190:[19, 47, 47, 57, 17, 3],190
191:[19, 47, 47, 57, 17, 4],191
192:[19, 48, 48, 57, 17, 3],192
193:[19, 48, 48, 57, 17, 4],193
194:[19, 48, 48, 58, 17, 4],194
195:[19, 48, 48, 58, 17, 5],195
196:[19, 49, 49, 58, 17, 4],196
197:[19, 49, 49, 59, 17, 4],197
198:[19, 49, 49, 59, 17, 5],198
199:[19, 49, 49, 59, 17, 6],199