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. 将一个顺序排列的数组添加到集合 ...
随机推荐
- VUE -input输入框字母转大写
示例: 输入自动转--->大写 <input type="text" placeholder="请输入证件号码" maxlength="1 ...
- mapboxgl 纠偏百度地图
缘起 之前分享了mapboxgl 互联网地图纠偏插件,插件当时只集成了高德地图. 文章发布后,有小伙伴在后台留言,希望插件也能支持百度地图. 刚好国庆假期有时间就研究了一下. 插件加载瓦片原理 首先, ...
- 用OpenCV显示视频时遇到问题
刚刚接触OpenCV,运行了书上的例程,程序编译没有问题,在视频显示快要结束时遇到了下面的问题,代码在后面 #include "stdafx.h"#include <open ...
- 洛谷4248 AHOI2013差异 (后缀数组SA+单调栈)
补博客! 首先我们观察题目中给的那个求\(ans\)的方法,其实前两项没什么用处,直接\(for\)一遍就求得了 for (int i=1;i<=n;i++) ans=ans+i*(n-1); ...
- C 可变参数列表 stdarg.h
内容来自<c和指针>,整理后方便个人理解 stdarg.h 菜鸟教程 - <stdarg.h> 类型 va_list 宏 va_start va_arg va_end #inc ...
- 【c++ Prime 学习笔记】第9章 顺序容器
一个容器是特定类型对象的集合 顺序容器中元素的顺序与其加入容器的位置对应 关联容器中元素的顺序由其关联的关键字决定,关联容器分为有序关联容器和无序关联容器 所有容器类共享公有接口,不同容器按不同方式扩 ...
- pycharm中的terminal和Windows命令提示符有什么区别?二者用pip安装的包是不是位于相同位置?
那要看pycharm使用了什么shell,可以在设置->工具->终端里查看shell path.如果使用的是cmd.exe那就没区别.pycharm终端和Windows命令提示符用pip安 ...
- AtCoder Beginner Contest 223
AtCoder Beginner Contest 223 A是纯纯的水题,就不说了 B - String Shifting 思路分析 我真的sb,一开始想了好久是不是和全排列有关,然后读了好几遍题目也 ...
- BUAA_2020_软件工程_软件案例分析作业
项目 内容 这个作业属于那个课程 班级博客 这个作业的要求在哪里 作业要求 我在这个课程的目标是 学习掌握软件工程的相关知识 这个作业在哪个具体方面帮我实现目标 通过对具体软件案例的分析学习软件工程 ...
- Noip模拟37 2021.8.12
T1 数列 真是考场上不是数学的乱推柿子,想定理,是数学的没想出来.. 比较悲伤... 列柿子不用动脑子,就是没有想出来$EXgcd$解不定方程,淦.. 解处一组解后利用比较显然的性质: $x+\fr ...