shuffle.java
/*************************************************************************
* Compilation: javac Shuffle.java
* Execution: java Shuffle N < california-gov.txt
* Dependencies: StdIn.java
*
* Reads in N lines of text, shuffles them, and print them in random order.
* Uses Knuth's shuffling shuffle.
*
* The file california-gov.txt contains a list of the 135
* candidates in the October 7, 2003 California governor's runoff
* election. The file cards.txt contains a list of 52 playing cards.
*
*
* % java Shuffle 5 < california-gov.txt
* Iris Adam
* Douglas Anderson
* Alex-St. James
* Angelyne
* Brooke Adams
*
* % java Shuffle 5 < cards.txt
* Four of Clubs
* Six of Clubs
* Three of Clubs
* Deuce of Clubs
* Five of Clubs
*
*
*************************************************************************/ public class Shuffle { // swaps array elements i and j
public static void exch(String[] a, int i, int j) {
String swap = a[i];
a[i] = a[j];
a[j] = swap;
} // take as input an array of strings and rearrange them in random order
public static void shuffle(String[] a) {
int N = a.length;
for (int i = 0; i < N; i++) {
int r = i + (int) (Math.random() * (N-i)); // between i and N-1
exch(a, i, r);
}
} // take as input an array of strings and print them out to standard output
public static void show(String[] a) {
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
} public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
String[] a = new String[N]; // read in data
for (int i = 0; i < N; i++) {
a[i] = StdIn.readLine();
} // shuffle array and print permutation
shuffle(a);
show(a); System.out.println(); // do it again
shuffle(a);
show(a); }
}
import java.util.ArrayList;
import java.util.Random; public class J718{
public static void main(String[]args){
int[][]m={{1,2},{3,4},{5,6},{7,8},{9,10}};
/*int N = m.length;
for (int i = 0; i < N; i++) {
int r = i + (int) (Math.random() * (N-i)); // between i and N-1
System.out.print("{"+m[r][0]+","+m[r][1]+"} ");*/
shuffle(m);
}
public static void shuffle(int[][]m){
/*int N = m.length;
for (int i = 0; i < N; i++) {
int r = i + (int) (Math.random() * (N-i)); // between i and N-1
System.out.print("{"+m[r][0]+","+m[r][1]+"} ");*/
int n = m.length;
Random rand = new Random();
int[] p=new int[5];
boolean[] bool = new boolean[n]; int num =0; for (int i = 0; i<5; i++){
do{
//如果产生的数相同继续循环
num = rand.nextInt(n); }while(bool[num]); bool[num] =true; p[i]=num;
}
for(int j=0;j<n;j++)
System.out.println ("{"+m[p[j]][0]+","+m[p[j]][1]+"} ");
}
}
shuffle.java的更多相关文章
- hadoop Shuffle Error OOM错误分析和解决
在执行Reduce Shuffle的过程中,偶尔会遇到Shuffle Error,但是重启任务之后,Shuffle Error会消失,当然这只是在某些特定情况下才会报出来的错误.虽然在每次执行很短的时 ...
- mapreduce.shuffle set in yarn.nodemanager.aux-services is invalid
15/07/01 20:14:41 FATAL containermanager.AuxServices: Failed to initialize mapreduce.shuffle java.la ...
- container error log
learn from error- Error: org.apache.hadoop.mapreduce.task.reduce.Shuffle$ShuffleError: error in shuf ...
- (转)yarn 集群部署,遇到的问题小结
link:http://blog.csdn.net/uniquechao/article/details/26449761 版本信息: hadoop 2.3.0 hive 0.11.0 1. ...
- yarn 集群部署,遇到的问题小结
版本号信息: hadoop 2.3.0 hive 0.11.0 1. Application Master 无法訪问 点击application mater 链接,出现 http 500 错 ...
- Hadoop学习历程(四、运行一个真正的MapReduce程序)
上次的程序只是操作文件系统,本次运行一个真正的MapReduce程序. 运行的是官方提供的例子程序wordcount,这个例子类似其他程序的hello world. 1. 首先确认启动的正常:运行 s ...
- Hadoop 2.x 安装常见问题FAQ(一) NodeManager 无法启动问题解决
一.问题描述 在搭建 Hadoop hadoop-2.4.1 集群的最后一步启动集群,在命令窗口并没有报任何错误,但是Slave 节点的 NodeManager进程始终启动不起来.随后查看了后台启动日 ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- java Collection.shuffle()随机打乱一个顺序数组
如何打乱一个顺序的数组,其实集合的帮助类Collection就有现成的方法可用,而且效率还蛮高的,总比自定义随机数等等方法要好很多.其实乱序就这么简单,步骤如下: 1. 将一个顺序排列的数组添加到集合 ...
随机推荐
- 11.4.3 LVS-TUN
LVS-TUN 用IP隧道技术实现虚拟服务器。这种方式是在集群的节点不在同一个网段时可用的转发机制,是将IP包封装在其他网络流量中的方法。为了安全的考虑,应该使用隧道技术中的VPN,也可使用租用专线。 ...
- TWAIN-v2.4-说明文档翻译(2)技术概览
技术概览 Technical Overview TWAIN架构(TWAIN Architecture) 数据交互的实现在TWAIN中由三类软件元素共同工作完成:应用程序(the application ...
- Eureka使用总结
关于Eureka: 提供基于 REST的服务,在集群中主要用于服务管理.使用该框架,可以将业务组件注册到Eureka容器中,这些组件可进行集群部署,Eureka主要维护这些服务的列表并自动检查他们的状 ...
- 什么是js事件,冒泡机制,事件捕获,默认行为
js事件: javascript使我们能够有能力创建动态页面,事件就是可以被js侦测到的行为,网页中每个元素都可以产生某些触发js函数的事件. 例如我们可以在用户点击某个按钮时产生一个click事件来 ...
- Seata分布式事务失败通知
一.背景 在我们使用Seata作为分布式事务时,有些时候我们的分布式时候并不是每次都可以成功的,而对于这些失败的分布式事务就需要进行通知.这篇文章简单记录一下如何实现通知. 二.功能实现 此处模拟邮件 ...
- Spring Security Resource Server的使用
Spring Security Resource Server的使用 一.背景 二.需求 三.分析 四.资源服务器认证流程 五.实现资源服务器 1.引入jar包 2.资源服务器配置 3.资源 六.测试 ...
- 脚本:bat实现自动转换windows远程端口
问题描述:通过一个脚本可以实现windows远程端口的转换,这个是拷贝过来学习的一个脚本 @echo off color f0 echo 修改远程桌面3389端口(支持Windows 2003 200 ...
- 热身训练2 The All-purpose Zero
The All-purpose Zero 简要题意: 长度为n的数组,每个数字为S[i],$0$是一种很神奇的数字,你想要的,它都可以变! 问这个序列的最长上升子序列长度为多少? 分析: 我们将除了 ...
- 21.10.12 test
题目 **WOJ5110 ** 到 WOJ5113 校内自测没开捆绑,于是输出 -1 和 n! 的程序拿到了高分,我的得分也比期望得分略有提升 T1 problem a \(\color{red}{2 ...
- 求1+2+3...+n 牛客网 剑指Offer
求1+2+3...+n 牛客网 剑指Offer 题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). ...