/*************************************************************************
* 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]+"} ");
}
}

最新文章

  1. web app性能大讨论
  2. C++混合编程之idlcpp教程Lua篇(8)
  3. android Error occurred during initialization of VM Could not reserve enough space for object heap Could not create the Java virtual machine.
  4. Oracle wrap 测试的一些细节问题
  5. Intent是什么?
  6. AssertValid函数学习
  7. Java IO详解(六)------序列化与反序列化(对象流)
  8. Implement a Linked List
  9. 用python做小说网站
  10. ES6 Generators的异步应用
  11. BZOJ 4517: [Sdoi2016]排列计数 [容斥原理]
  12. 『集群』001 Slithice 服务器集群 概述
  13. axios 或 ajax 请求文件
  14. java zip API实现压缩和解压缩zip包
  15. 设计一款相册APP,代替系统自带的相册功能,列举主要功能
  16. C语言的基本数据类型
  17. SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一)
  18. FPGA构造spi时序——AD7176为例(转)
  19. DES算法实现(C++版)
  20. Bzoj2149拆迁队:cdq分治 凸包

热门文章

  1. Rafy 框架 - 实体支持只更新部分变更的字段
  2. js 面向对象 动态添加标签
  3. NX7.5 使用UF_MODL_create_proj_curves创建投影曲线
  4. NX Open,怎样取到面的环LOOP
  5. scala基础篇 源码中 :_*的作用
  6. 【UE4 C++ 基础知识】&lt;12&gt; 多线程——FRunnable
  7. ORB_SLAM3 -- 配置安装
  8. 如何使用远程工具连接Linux服务器
  9. [对对子队]会议记录4.16(Scrum Meeting7)
  10. 热身训练2 Another Meaning