Lab5-Lottery


5530468921  นายรณกร วุทธานันท์ (COM22000) (7/13/2012 (12:42:41 PM))

MiniQuiz + TestScript
JLab>javac Lottery.java
JLab>
JLab>java Selftest 

JLab> testLottery : ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok  (10.0/10.0)
JLab> : -----------------------------
JLab> : ?????? 10.0 ????? (???? 10.0)
JLab> : -----------------------------
JLab> :<POINT>10.0</POINT> (<TOTAL>10.0</TOTAL> )
JLab>

ได้ 10 คะแนน
Source Code
import java.util.Scanner;
import java.io.*;

public class Lottery {
  public static void main(String[] args) throws IOException {
    Scanner in = new Scanner(new File("lottery.txt"));

int[] lot = new int[100];
    while (in.hasNext()) {
      String a = in.nextLine();
      String b = a.substring(a.length() - 2, a.length());
      int c = Integer.parseInt(b);
      lot[c]++;
      }
      in.close();

    System.out.print("เลขท้ายสองตัวที่ไม่เคยออกเลยคือ:");
    // แสดงรายการของเลขที่ไม่เคยออก (แสดงต่อ ๆ กันไปบนบรรทัดเดียวกัน แต่ละตัวคั่นด้วยช่องว่าง เช่น 12 41 32)
    // ถ้าไม่มีเลขที่ไม่เคยออก ก็ไม่ต้องแสดงอะไร


    for (int i = 0; i < 100; i++) {
      if (lot[i] == 0) {
        System.out.print(i + " ");
        }
      }
    System.out.println();
    System.out.print("เลขท้ายสองตัวที่ออกบ่อยสุดคือ:");
    // แสดงรายการของเลขที่ออกบ่อยสุด (แสดงต่อ ๆ กันไปบนบรรทัดเดียวกัน แต่ละตัวคั่นด้วยช่องว่าง เช่น 69 18)
    // เลขที่ออกบ่อยสุด อาจมีมากกว่าหนึ่งตัว


    int max = 0;
    for (int j = 0; j < 100; j++) {
      if (lot[j] > max) {
        max = lot[j];
        }
      }
    for (int k = 0; k < 100; k++) {
      if (lot[k] == max) {
        System.out.print(k + " ");
        }
      }
    System.out.println();
  }
}
SM=0, CM=2, ST=2, KY=16, TR=60:00
ERR = 60:00 ;