分治算法寻找硬币
来源:互联网 发布:ps软件电脑版 编辑:程序博客网 时间:2024/06/07 15:26
1,寻找假硬币
2,步骤:1 首先为每个硬币编号,然后可以将所有的硬币等分为两份,放在天平的两端;
2 因为假硬币分量比较轻,因此天平较轻的一端一定包含假硬币
3 再将较轻的一侧中的硬币等分为两部分,重述上方的做法。,
3,伪代码如下:
public static int FalseCoin(int coin[],int low,int high){
int i,sum1,sum2,sum3;
int re = 0;
sum1=sum2=sum3=0;
if(low+1==high){
if(coin[low]<coin[high]){
re = low+1;
return re;
}
else{
re = high+1;
return re;
}
}
if((high-low+1)%2 == 0){
for(i=low;i<=low+(high-low)/2;i++){
sum1 = sum1+coin[i];
}
for(i=low+(high-low)/2+1;i<=high;i++){
sum2=sum2+coin[i];
}
if(sum1<sum2){
re = FalseCoin(coin, low, low+(high-low)/2);
return re;
}
else if(sum1>sum2){
re = FalseCoin(coin, low+(high-low)/2, high);
return re;
}
else{
}
}
else{
for(i=low;i<=low+(high-low)/2-1;i++){
sum1 = sum1+coin[i];
}
for(i = low+(high-low)/2+1;i<=high;i++){
sum2=sum2+coin[i];
}
sum3 = coin[low+(high-low)/2];
if(sum1>sum2){
re = FalseCoin(coin, low+(high-low)/2+1, high);
return re;
}
else if(sum1<sum2){
re = FalseCoin(coin, low, low+(high-low)/2-1);
return re;
}
else{
}
if(sum1+sum3==sum2+sum3){
re = low+(high-low)/2+1;
return re;
}
}
return 0;
}
- 分治算法寻找硬币
- 分治算法——假硬币寻找,硬币找零,众数问题
- 趣味算法-寻找假硬币
- 算法--分治法寻找中值
- 分治算法--寻找第k大数
- 分治算法经典例题 寻找假币
- 分治算法寻找第K小数
- 分治算法思想解决找假硬币的问题
- bzoj1411 硬币游戏 分治
- 寻找数组中最大值和最小值—分治算法
- 算法笔记04--分治法之寻找最大最小元素
- 分治算法寻找假银币问题(六)
- 分治解决假硬币问题
- 从硬币找零问题:看分治/动态规划/贪心算法的区别与联系
- 分治算法
- 分治算法
- 分治算法?
- 【算法】分治
- 在树莓派上 Arch 的 更新操作
- 一张图学会VIM
- iOS 枚举的巧用
- 56. spring boot中使用@Async实现异步调用
- iOS开发中深入理解CADisplayLink和NSTimer
- 分治算法寻找硬币
- ps的第二天
- mysql 查看二进制日志
- Java操作MongoDB
- 【如何快速的开发一个完整的iOS直播app】(原理篇)
- 57. Spring 自定义properties升级篇
- WPF中的资源(一) - 静态资源和动态资源
- 合并表记录
- 10009---Solr 6.0 学习(一)环境搭建