zoukankan      html  css  js  c++  java
  • 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]+"} "); 
        }
    }
  • 相关阅读:
    C#深入浅出 修饰符(二)
    HDU 5785 Interesting
    HDU 5783 Divide the Sequence
    HDU 5781 ATM Mechine
    UVA 714 Copying Books
    uva 1471 Defense Lines
    UVA 11134 Fabled Rooks
    UVA 11572 Unique Snowflakes
    UVA 11093 Just Finish it up
    UVA 10954 Add All
  • 原文地址:https://www.cnblogs.com/zhangyongjian/p/3632414.html
Copyright © 2011-2022 走看看