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] =&gt; int(3)
[1] =&gt; int(3)
[2] =&gt; int(3)
[3] =&gt; int(3)
[4] =&gt; int(2)
[5] =&gt; int(2)
[6] =&gt; int(2)
[7] =&gt; int(2)
}
</pre>

例:amount: 20, count : 20

20----20

<pre>array(20) {
[0] =&gt; int(1)
[1] =&gt; int(1)
[2] =&gt; int(1)
[3] =&gt; int(1)
[4] =&gt; int(1)
[5] =&gt; int(1)
[6] =&gt; int(1)
[7] =&gt; int(1)
[8] =&gt; int(1)
[9] =&gt; int(1)
[10] =&gt; int(1)
[11] =&gt; int(1)
[12] =&gt; int(1)
[13] =&gt; int(1)
[14] =&gt; int(1)
[15] =&gt; int(1)
[16] =&gt; int(1)
[17] =&gt; int(1)
[18] =&gt; int(1)
[19] =&gt; int(1)
}
</pre>

例: amount: 20, count: 22

20----22

<pre>array(22) {
[0] =&gt; int(1)
[1] =&gt; int(1)
[2] =&gt; int(1)
[3] =&gt; int(1)
[4] =&gt; int(1)
[5] =&gt; int(1)
[6] =&gt; int(1)
[7] =&gt; int(1)
[8] =&gt; int(1)
[9] =&gt; int(1)
[10] =&gt; int(1)
[11] =&gt; int(1)
[12] =&gt; int(1)
[13] =&gt; int(1)
[14] =&gt; int(1)
[15] =&gt; int(1)
[16] =&gt; int(1)
[17] =&gt; int(1)
[18] =&gt; int(1)
[19] =&gt; int(1)
[20] =&gt; int(0)
[21] =&gt; int(0)
}
</pre>


把总数amount拆分以标准差最小的标准,平均拆分成count个整数的更多相关文章

  1. 5.数字拆分成4段,怎样使得4段的乘积最小【dp】

    题目是:给出一个数字(10,000-100,000,000),把这个数字拆分成4段,怎样使得4段的乘积最小.比如12345拆分成1*2*3*45=270, 10000=1*00*0*0=0. 解题分析 ...

  2. ch1_5_1统计最大最小元素的平均比较次数

    public class ch1_5_1统计最大最小元素的平均比较次数 { public static void main(String[] args) { // TODO Auto-generate ...

  3. 运筹学之"最大最大决策标准"和"最大最小决策标准"

    一.最大最大决策标准的解题思路就是:先比较出所有行的最大值,在最大值中选出最大值,最后这个最大是那行的就选哪个方案 二.最大最小决策标准的解题思路就是:先比较出所有行的最小值,在最小值中选出最大值,最 ...

  4. hdu4289 最小割最大流 (拆点最大流)

    最小割最大流定理:(参考刘汝佳p369)增广路算法结束时,令已标号结点(a[u]>0的结点)集合为S,其他结点集合为T=V-S,则(S,T)是图的s-t最小割. Problem Descript ...

  5. BZOJ-1877 晨跑 最小费用最大流+拆点

    其实我是不想做这种水题的QWQ,没办法,剧情需要 1877: [SDOI2009]晨跑 Time Limit: 4 Sec Memory Limit: 64 MB Submit: 1704 Solve ...

  6. BZOJ-2324 营救皮卡丘 最小费用可行流+拆下界+Floyd预处理

    准备一周多的期末,各种爆炸,回来后状态下滑巨快...调了一晚上+80%下午 2324: [ZJOI2011]营救皮卡丘 Time Limit: 10 Sec Memory Limit: 256 MB ...

  7. BZOJ-1070 修车 最小费用最大流+拆点+略坑建图

    1070: [SCOI2007]修车 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 3624 Solved: 1452 [Submit][Status] ...

  8. UVa 1658 - Admiral(最小费用最大流 + 拆点)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. hdu 2098 分拆素数和(一个偶数拆分成两个不同素数和 拆法数量)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2098 分拆素数和 Time Limit: 1000/1000 MS (Java/Others)     ...

随机推荐

  1. CString、char*l类型转换

    环境:VS2010 1.CString to char* 第一种方法: 需头文件:atlconv.h CString   host_string; //CString to char* USES_CO ...

  2. luogu 3466 对顶堆

    显然答案是将一段区间全部转化成了其中位数这样的话,需要维护一个数据结构支持查询当前所有数中位数对顶堆 用两个堆将 < 中位数的数放入大根堆将 > 中位数的数放入小根堆这样就会存在删除操作 ...

  3. CodefChef September Challenge 2019 题解

    传送门 \(CHEFK1\) 首先连出一个环和所有的自环,剩下的每次按\(n\)个一连就可以了 //quming #include<bits/stdc++.h> #define R reg ...

  4. webbench 源代码

    WebBench源码:https://github.com/EZLippi/WebBench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问 ...

  5. 实体类(VO,DO,DTO,PO)的划分《转载---》

    转载自:https://blog.csdn.net/u010722643/article/details/61201899 经常会接触到VO,DO,DTO的概念,本文从领域建模中的实体划分和项目中的实 ...

  6. 2.json解析

    static String jsonStr = "{\"sites\":[{\"name\":\"gold\",\"ur ...

  7. STP生成树算法

    生成树算法第一:决定谁是“根网桥”对比各个网桥ID,先对比ID中的优先级,优先级相同的时候对比网桥MAC地址,对比依据是谁的值最小,谁是“根网桥” 第二:决定哪些是“根端口”窍门——每个非根网桥上都有 ...

  8. arts打卡13周

    算法: 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 12. 113. 214. 12115. 1112211 被读作  "one 1" ...

  9. 给定一个二叉搜索树(BST),找到树中第 K 小的节点

    问题:给定一个二叉搜索树(BST),找到树中第 K 小的节点. 出题人:阿里巴巴出题专家:文景/阿里云 CDN 资深技术专家. 考察点: 1. 基础数据结构的理解和编码能力 2.  递归使用 参考答案 ...

  10. 解决url传递过程中加号变空格的问题<转>

    url传递过程中加号变空格在接收url参数的过程中,会发现如果参数中存在‘+’号,接收后会变成空格. 如11+22接收后变成11 22.要解决这个问题,需要将加号替换为%2B进行传递. 如11%2B2 ...