/*************************************************************************
* 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的更多相关文章

  1. hadoop Shuffle Error OOM错误分析和解决

    在执行Reduce Shuffle的过程中,偶尔会遇到Shuffle Error,但是重启任务之后,Shuffle Error会消失,当然这只是在某些特定情况下才会报出来的错误.虽然在每次执行很短的时 ...

  2. 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 ...

  3. container error log

    learn from error- Error: org.apache.hadoop.mapreduce.task.reduce.Shuffle$ShuffleError: error in shuf ...

  4. (转)yarn 集群部署,遇到的问题小结

    link:http://blog.csdn.net/uniquechao/article/details/26449761   版本信息: hadoop 2.3.0  hive 0.11.0   1. ...

  5. yarn 集群部署,遇到的问题小结

    版本号信息: hadoop 2.3.0  hive 0.11.0 1. Application Master 无法訪问     点击application mater 链接,出现 http 500 错 ...

  6. Hadoop学习历程(四、运行一个真正的MapReduce程序)

    上次的程序只是操作文件系统,本次运行一个真正的MapReduce程序. 运行的是官方提供的例子程序wordcount,这个例子类似其他程序的hello world. 1. 首先确认启动的正常:运行 s ...

  7. Hadoop 2.x 安装常见问题FAQ(一) NodeManager 无法启动问题解决

    一.问题描述 在搭建 Hadoop hadoop-2.4.1 集群的最后一步启动集群,在命令窗口并没有报任何错误,但是Slave 节点的 NodeManager进程始终启动不起来.随后查看了后台启动日 ...

  8. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  9. java Collection.shuffle()随机打乱一个顺序数组

    如何打乱一个顺序的数组,其实集合的帮助类Collection就有现成的方法可用,而且效率还蛮高的,总比自定义随机数等等方法要好很多.其实乱序就这么简单,步骤如下: 1. 将一个顺序排列的数组添加到集合 ...

随机推荐

  1. bzoj4552排序(线段树,二分)

    题目大意 给定一个长度为n的序列,有m个操作,操作包括两种: \(0\ l\ r\)区间[l,r]的数字升序排序 \(1\ l\ r\)区间[l,r]的数字降序排序 最后询问在q位置上的数是多少? 其 ...

  2. AutoCAD云产品平台ForgeViewer格式离线部署思路分析

    背景 在上一篇博文中CAD图DWG解析WebGIS可视化技术分析总结提到,利用AutoCAD的自有云产品 Autodesk Forge,能在浏览器中渲染 3D 和 2D 模型数据,实现DWG图形的We ...

  3. FastAPI 学习之路(二)

    之前的文章已经介绍了如何安装,以及简单的使用,这篇文章呢,我们就不去分享如何安装对应的包了. 我们如何去编写呢,其实很简单,按照下面的步骤,一个简单的基于fastapi的接口就编写完毕. 首先:创建一 ...

  4. 《手把手教你》系列技巧篇(三十二)-java+ selenium自动化测试-select 下拉框(详解教程)

    1.简介 在实际自动化测试过程中,我们也避免不了会遇到下拉选择的测试,因此宏哥在这里直接分享和介绍一下,希望小伙伴或者童鞋们在以后工作中遇到可以有所帮助. 2.select 下拉框 2.1Select ...

  5. C 标准库函数手册摘要

    <stdlib.h> int abs( int value ); long int labs( long int value ); 返回参数的绝对值 int rand( void ); v ...

  6. NX Open,怎样取到面的环LOOP

    在封装的ufun .NET库里面,对UF_MODL_ask_face_loops这个函数并没有封装,导致我们很多不便,那我们在.NET下怎样才能使用这个函数呢??当然是手动处理一下 Public Fu ...

  7. Java中的函数式编程(三)lambda表达式

    写在前面 lambda表达式是一个匿名函数.在Java 8中,它和函数式接口一起,共同构建了函数式编程的框架.   lambda表达式乍看像是匿名内部类的一种语法糖,但实际上,它们是两种本质不同的事物 ...

  8. aritest发送测试报告到邮件

    #!/usr/bin/env python # -*- coding=utf-8 -*- __CreateAt__ = '2020/4/19-17:34' import shutil from air ...

  9. .net 5.0 ref文件夹的作用

    ref目录里的dll是一个名为参考组件的东西,微软MSDN给的解释是 参考组件是一种特殊类型的程序集,仅包含表示库的公共API面所需的最小元数据数量.它们包括用于在构建工具中引用程序集时重要的所有成员 ...

  10. Python课程笔记(一)

    由于新冠状病毒的爆发,不得不在家里上网课,开课已经两个礼拜了,今天上完Python课后,准备整理一下最近学习Python的笔记. 人生苦短,我用Python 一.Hello World 初学一门新的语 ...