2016年7月14日 星期四

ITSA Basic 題目28. 統一發票對獎


http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?a=15933

import java.util.*;

public class Main {

 public static void main(String[] args) {
  Scanner scn = new Scanner(System.in);
  String s1 = scn.next(), s2 = scn.next(), s3 = scn.next(), s4 = scn.next();
  int ans[] = new int[7], n = scn.nextInt(), tot = 0, m[] = { 200000, 40000, 10000, 4000, 1000, 200 };
  while (n-- != 0) {
   String str = scn.next();
   if (s1.equals(str)) {
    ans[0]++;
    tot += 2000000;
    continue;
   }
   for (int i = 0; i &lt= 5; i++) {
    if (str.indexOf(s2.substring(i), i) &gt= 0) {
     ans[i + 1]++;
     tot += m[i];
     break;
    } else if (str.indexOf(s3.substring(i), i) &gt= 0) {
     ans[i + 1]++;
     tot += m[i];
     break;
    } else if (str.indexOf(s4.substring(i), i) &gt= 0) {
     ans[i + 1]++;
     tot += m[i];
     break;
    }
   }
  }
  for (int i = 0; i &lt ans.length; i++) {
   if (i != 0)
    System.out.print(" ");
   System.out.print(ans[i]);
  }
  System.out.printf("\n%d\n", tot);

 }

 /* 
    題目:題目28. 統一發票對獎
    作者:1010
    時間:西元 2016 年 7 月 */
}

這題利用.substring(i)求得子字串來擷取各位數的獎項然後利用.indexOf()來判斷是否中獎 
注意因為兌獎是找尋末*碼所以.indexOf()要設定搜尋開始數字例如找三獎是查詢末六位數那開始的索引值就是2


import java.util.*;

public class Main {

 public static void main(String[] args) {
  String str="654321";
  String s=str.substring(2);//從第index[2]數字開始擷取
  System.out.println(s);
 }

 /* 
    題目:substring()求得子字串
    作者:1010
    時間:西元 2016 年 7 月 */
}

沒有留言:

張貼留言