fapai

import java.util.ArrayList;

public class dispatchCard {

 private static ArrayList<String> card = new ArrayList<String>();
 static final int PEOPLE_COUNT = 7;

 /**
  * @param args
  */
 public static void main(String[] args) {
  // generate a sorted list of cards
  newCards();
  // exchange Cards to get a random list of cards
  exchangeCards(50);
  // dispatch cards
  ArrayList<ArrayList<String>> list = dispatch();
  // output every people's cards
  output(list);
 }

 /**
  * a random list of cards
  */
 private static ArrayList<ArrayList<String>> dispatch() {
  ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
  int pageCount = card.size() / PEOPLE_COUNT;
  int residue = card.size() % PEOPLE_COUNT;
  for(int i=0; i < PEOPLE_COUNT;i++){
   ArrayList<String> each = new ArrayList<String>();
   for(int j=0; j < pageCount;j++){
    each.add(card.get(i*pageCount + j));
   }
   if(i < residue){
    each.add(card.get(pageCount * PEOPLE_COUNT + i));
   }
   sort(each);
   list.add(each);
  }
  return list;
 }
 

 /**
  * sort cards
  */
 private static void sort(ArrayList<String> each) {
  for(int i=0; i < each.size();i++){
   for(int j=0; j < each.size() - i - 1;j++){
    if(each.get(j).compareTo(each.get(j + 1)) < 0){
     String tmp = each.get(j);
     each.set(j, each.get(j+1));
     each.set(j + 1, tmp);
    }
   }
  }
 }

 /**
  * a random list of cards
  */
 private static void exchangeCards(int exchageTimes) {
  for (int i = 0; i < exchageTimes; i++) {
   int r1 = (int) (Math.random() * card.size());
   int r2 = (int) (Math.random() * card.size());
   String tmp = card.get(r1);
   card.set(r1, card.get(r2));
   card.set(r2, tmp);
  }
 }

 /**
  * a sorted list of cards
  */
 private static void newCards() {

  // four types
  String[] card1 = { "A", "B", "C", "D" };
  // 13 pages for every type
  String[] card2 = { "03", "04", "05", "06", "07", "08", "09", "10",
    "11", "12", "13", "21", "22"};
  // input fixed value to the list
  for (int i = 0; i <= 3; i++) {
   for (int j = 0; j <= 12; j++) {
    card.add(card1[i] + ":" + card2[j]);
   }
  }
 }

 /**
  * output every people's cards
  */
 private static void output(ArrayList<ArrayList<String>> list) {
  for(ArrayList<String> each : list){
   for(String s : each){
    System.out.print(s);
    System.out.print(",");
   }
   System.out.println();
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值