
黄毅然的JAVA学习(五)
发布日期:2021-05-06 15:14:59
浏览次数:21
分类:精选文章
本文共 1503 字,大约阅读时间需要 5 分钟。
螺钉螺帽问题
假设有N个螺丝和N个螺丝帽混在一堆,需要将它们快速配对。一个螺丝只会匹配一个螺丝帽,一个螺丝帽也只会匹配一个螺丝。可以试着把一个螺丝和一个螺丝帽拧在一起,看看哪一个大了,但不能直接比较两个螺丝或者螺丝帽的大小。给出一个解决此问题的有效方法。
该问题可以使用改进型的快速排序办法。
//黄毅然已经两个月没有更新CSDN了,这个排序弄得我挺头疼的,在此感谢大佬@zy的指导,/舔一舔/ //import java.util.Random;public class screw { public static void printArray(int[] num) { for(int i : num) { System.out.printf("%d\t", i); } System.out.println('\n'); } public static void exchange(int []num,int i,int j) { int tmp=num[i]; num[i]=num[j]; num[j]=tmp; } public static int partition(int []nut,int []screw,int s,int t) { int i=s,j=t; int x=s,y=t; int tmp=nut[i]; while(itmp) j--; while(i tmp) y--; while(s 0 ; i++){ int num = createRandom(index); int temp = arr[num]; arr[num] = arr[index]; arr[index] = temp; index--; } } public static int createRandom(int end){ return (new Random().nextInt(end)); } public static void main(String []args) { int []screw=new int[10]; int []nut=new int[10]; for(int i=0;i<10;i++) { Random rand=new Random(); screw[i]=rand.nextInt(100); nut[i]=screw[i]; } flushArr(nut); printArray(nut); printArray(screw); quicksort(nut,screw,0,9); printArray(screw); printArray(nut); }}
由于生成的是随机数,结果不会完全一致
将会类似下面的 72 79 78 30 64 14 47 9 1 347 3 30 1 14 72 9 78 79 64
1 3 9 14 30 47 64 72 78 79
1 3 9 14 30 47 64 72 78 79
发表评论
最新留言
很好
[***.229.124.182]2025年03月24日 17时05分57秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Kali Day01 --- arpspoof命令进行断网攻击(ARP欺骗)
2019-03-05
echart关系图平分节点删除时自动平衡问题
2019-03-05
【Coursera】Internet History 读书笔记
2019-03-05
《ODAY安全:软件漏洞分析技术》学习心得-----shellcode的一点小小的思考
2019-03-05
Decision tree(决策树)算法初探
2019-03-05
《Unity3D/2D游戏开发从0到1(第二版本)》 书稿完结总结
2019-03-05
sctf_2019_easy_heap
2019-03-06
AT 杂题泛做
2019-03-06
StringBuilder拼接字符串,“,”在前还是在后问题
2019-03-06
给asterisk1.8.7添加menuselct选项
2019-03-06
组合模式
2019-03-06
PyQt5之音乐播放器
2019-03-06
css居中方法与双飞翼布局
2019-03-06
Redis进阶实践之十八 使用管道模式提高Redis查询的速度
2019-03-06
SQL注入
2019-03-06
XCTF-upload1
2019-03-06