把总数amount拆分以标准差最小的标准,平均拆分成count个整数
public function tt_add(){
$res = $this->arr_avg(,);
echo array_sum($res);
echo '----' . count($res);
dump($res);
exit;
}
/***
* @param $amount '总数'
* @param $count '需要拆分数据个数'
* @return array|mixed '把总数amount拆分已标准差最小的标准,平均拆分成count个整数'
*/
public function arr_avg($amount, $count)
{
$avg_num = $amount / $count;
$ceil_num = ceil($amount / $count);
if ($avg_num == $ceil_num) {
$avg_res = array_fill(, $count, (int)$ceil_num);
} else {
if ($amount < $count) {
$avg_res_1 = array_fill(, $amount, (int)$ceil_num); // 这里只会填充1
$avg_res_2 = array_fill($amount - , $count - $amount, );
$avg_res = dealed_array_merge($avg_res_1, $avg_res_2);
} else {
$floor_num = floor($amount / $count);
$avg_res = array_fill(, $count, (int)$floor_num);
$left_num = $amount - array_sum($avg_res);
if ( <= $left_num) {
$left_avg_res = $this->arr_avg($left_num, $count);
foreach ($left_avg_res as $k => $v){
if ($v == ){
unset($left_avg_res[$k]);
}
}
unset($v);
$left_avg_res = array_values($left_avg_res);
$avg_res = $this->array_add($avg_res,$left_avg_res);
}
}
}
return $avg_res;
}
/***
* @param $a
* @param $b
* @return mixed '2个一维数组相加,键名相同相加,不同的均保留'
*/
public function array_add($a,$b)
{
//根据键名获取两个数组的交集
$arr = array_intersect_key($a, $b);
//遍历第二个数组,如果键名不存在与第一个数组,将数组元素增加到第一个数组
foreach ($b as $key => $value) {
if (!array_key_exists($key, $a)) {
$a[$key] = $value;
}
}
//计算键名相同的数组元素的和,并且替换原数组中相同键名所对应的元素值
foreach ($arr as $key => $value) {
$a[$key] = $a[$key] + $b[$key];
}
//返回相加后的数组
return $a;
}
例:amount:20 count:14
20----8
<pre>array(8) {
[0] => int(3)
[1] => int(3)
[2] => int(3)
[3] => int(3)
[4] => int(2)
[5] => int(2)
[6] => int(2)
[7] => int(2)
}
</pre>
例:amount: 20, count : 20
20----20
<pre>array(20) {
[0] => int(1)
[1] => int(1)
[2] => int(1)
[3] => int(1)
[4] => int(1)
[5] => int(1)
[6] => int(1)
[7] => int(1)
[8] => int(1)
[9] => int(1)
[10] => int(1)
[11] => int(1)
[12] => int(1)
[13] => int(1)
[14] => int(1)
[15] => int(1)
[16] => int(1)
[17] => int(1)
[18] => int(1)
[19] => int(1)
}
</pre>
例: amount: 20, count: 22
20----22
<pre>array(22) {
[0] => int(1)
[1] => int(1)
[2] => int(1)
[3] => int(1)
[4] => int(1)
[5] => int(1)
[6] => int(1)
[7] => int(1)
[8] => int(1)
[9] => int(1)
[10] => int(1)
[11] => int(1)
[12] => int(1)
[13] => int(1)
[14] => int(1)
[15] => int(1)
[16] => int(1)
[17] => int(1)
[18] => int(1)
[19] => int(1)
[20] => int(0)
[21] => int(0)
}
</pre>
把总数amount拆分以标准差最小的标准,平均拆分成count个整数的更多相关文章
- 5.数字拆分成4段,怎样使得4段的乘积最小【dp】
题目是:给出一个数字(10,000-100,000,000),把这个数字拆分成4段,怎样使得4段的乘积最小.比如12345拆分成1*2*3*45=270, 10000=1*00*0*0=0. 解题分析 ...
- ch1_5_1统计最大最小元素的平均比较次数
public class ch1_5_1统计最大最小元素的平均比较次数 { public static void main(String[] args) { // TODO Auto-generate ...
- 运筹学之"最大最大决策标准"和"最大最小决策标准"
一.最大最大决策标准的解题思路就是:先比较出所有行的最大值,在最大值中选出最大值,最后这个最大是那行的就选哪个方案 二.最大最小决策标准的解题思路就是:先比较出所有行的最小值,在最小值中选出最大值,最 ...
- hdu4289 最小割最大流 (拆点最大流)
最小割最大流定理:(参考刘汝佳p369)增广路算法结束时,令已标号结点(a[u]>0的结点)集合为S,其他结点集合为T=V-S,则(S,T)是图的s-t最小割. Problem Descript ...
- BZOJ-1877 晨跑 最小费用最大流+拆点
其实我是不想做这种水题的QWQ,没办法,剧情需要 1877: [SDOI2009]晨跑 Time Limit: 4 Sec Memory Limit: 64 MB Submit: 1704 Solve ...
- BZOJ-2324 营救皮卡丘 最小费用可行流+拆下界+Floyd预处理
准备一周多的期末,各种爆炸,回来后状态下滑巨快...调了一晚上+80%下午 2324: [ZJOI2011]营救皮卡丘 Time Limit: 10 Sec Memory Limit: 256 MB ...
- BZOJ-1070 修车 最小费用最大流+拆点+略坑建图
1070: [SCOI2007]修车 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 3624 Solved: 1452 [Submit][Status] ...
- UVa 1658 - Admiral(最小费用最大流 + 拆点)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- hdu 2098 分拆素数和(一个偶数拆分成两个不同素数和 拆法数量)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2098 分拆素数和 Time Limit: 1000/1000 MS (Java/Others) ...
随机推荐
- 学习Spring-Data-Jpa(四)---Naming命名策略,源码跟踪
1.首先在Entity实体中,命名方式有两种: 一种是显示命名,即通过@Table的name属性指定对应的数据库表名称,@Column的name属性指定实体字段对应数据库字段的名称. 另一种是隐式命名 ...
- [TypeScript ] Using the Null Coalescing operator with TypeScript 3.7
The postshows you how to use the null coalescing operator (??) instead of logical or (||) to set def ...
- JS数组去重整理合集
1.利用splice var arr = [1,2,3,4,5,6,7,8,9,9,8,7,6,5,4,3,2,1]; function repeat(arr){ for(var i = 0;i< ...
- Golang 内存管理
- 021_Python3 OS 文件/目录方法
os 模块提供了非常丰富的方法用来处理文件和目录.常用的方法如下表所示: 序号 方法及描述 1 os.access(path, mode) 检验权限模式 2 os.chdir(path) 改变当前 ...
- Codeforces 1172F Nauuo and Bug [线段树]
Codeforces 思路 定义\(f_{l,r}(x)\)表示数\(x\)从\(l\)进去\(r\)出来的时候会变成什么样子.容易发现这个函数是个分段函数,每一段都是斜率为1的一次函数,并且段数就是 ...
- CSP-S2019 快乐爆0
hhh 我爆0了 快乐 大家都比我强 hh 常规操作 本来就是个憨憨 回去复习文化课了 唉 干啥啥不行
- os 模块常用方法
os.remove()删除文件 os.rename()重命名文件 os.walk()生成目录树下的所有文件名 os.chdir()改变目录 os.mkdir/makedirs创建目录/多层目录 os. ...
- エンジニア死滅シタ世界之高層ビル [MISSION LEVEL: B]-Python3
n = input() pre="" next_str = "" new_str = "" for i in range(int(n)): ...
- ubuntu之路——day2
一:sougou输入法安装 详情参考:https://blog.csdn.net/xin17863935225/article/details/82285177 注意切换成fcitx架构 因为linu ...