-------------------------------------------------
/**
* 10. (基本選択法)
* 要素数10のint型配列aにデータが入っている。
* この配列要素を昇順に並べ替える。
* (参考:午前中のテキストp.63の基本選択法を使う)
*/
public class Quiz1708 {
public static void main(String[] args) {
int[] a = {50, 30, 90, 40, 100, 10, 20, 60, 80, 70, };
// 表示する
System.out.print("整列前 = ");
print(a);
// 整列する
sort(a);
// 表示する
System.out.print("整列後 = ");
print(a);
}
/**
* 指定された配列を昇順に並べ替える。
* @param a 配列
*/
static void sort(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
// 最大値を持つ添字を探す
int maxIndex = 0; // 最大値を持つ添字
for (int j = 0; j < a.length - i - 1; j++) {
if (a[j] > a[maxIndex]) {
maxIndex = j;
}
}
// 最大値を持つ添字を範囲の最後と交換する
if (a[a.length - i - 1] < a[maxIndex]) {
int w = a[a.length - i - 1];
a[a.length - i - 1] = a[maxIndex];
a[maxIndex] = w;
}
}
}
/**
* 指定された配列aの各要素を並べて表示する。
* @param a 配列
*/
static void print(int[] a) {
System.out.print("[");
for (int i = 0; i < a.length - 1; i++) {
System.out.print(a[i] + ", ");
}
if (a.length > 0) {
System.out.print(a[a.length - 1]);
}
System.out.println("]");
}
}
/* 実行結果
整列前 = [50, 30, 90, 40, 100, 10, 20, 60, 80, 70, ]
整列後 = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, ]
*/
-------------------------------------------------
/**
* 13. (文字列探索 - BM法) char型配列textにデータが入っている。
* この文字配列textの中に、パターン文字列(char型配列pattern)を探索する。
* あった場合は、patternの先頭文字と一致するtextの添字を返す。
* ない場合は、-1を返す。 (参考:午前中のテキストp.67のBM法を使う)
*/
public class Quiz1709 {
public static void main(String[] args) {
// テキスト
String textString = "PQACZXYZRXYZ";
char[] text = textString.toCharArray();
// パターン
String patternString = "XYZ";
char[] pattern = patternString.toCharArray();
// 表示する
System.out.println("テキスト = [" + textString + "]");
System.out.println("パターン = [" + patternString + "]");
// 探索する
int index = indexOf(text, pattern);
// 結果を表示する
System.out.print("index = " + index);
}
/**
* @param text
* @param pattern
* @return
*/
static int indexOf(char[] text, char[] pattern) {
// スキップ表を作る
int[] skip = new int[256];
// デフォルト値を設定する
for (int i = 0; i < skip.length; i++) {
skip[i] = pattern.length;
}
// スキップ数を設定する
for (int i = 0; i < pattern.length - 1; i++) {
skip[pattern[i]] = pattern.length - i - 1;
}
// 探索する
int index = -1;
int i = 0;
search: while (i <= text.length - pattern.length) {
// パターンを後ろから見ていく
int j;
for (j = pattern.length - 1; (j >= 0) && (text[i + j] == pattern[j]); j--) {
}
// すべて一致すれば見つかった
if (j < 0) {
index = i;
break search;
}
// 次の位置までスキップする
int k = skip[text[i + j]] - (pattern.length - j - 1);
if (k > 0) {
i += k;
} else {
i += 1;
}
}
return index;
}
}
/*
実行例
テキスト = [PQACZXYZRXYZ]
パターン = [XYZ]
index = 5
テキスト = [PQACZXYZRXYZ]
パターン = [XYZW]
index = -1
*/
-------------------------------------------------
import java.util.Scanner;
/**
* Quiz0904で、じゃんけんの勝敗表(2次
* 元配列)をつくり、勝負するロジックを簡単
* にする。
* また、指定した手も、数字ではなく、日本語
* 表記(グー、チョキ、パー)で表示すること。
* --------- ------- -------- --------
* 勝敗表 0 グー 1 チョキ 2 パー
* --------- ------- -------- --------
* 0 グー 引分け 勝ち 負け
* 1 チョキ 負け 引分け 勝ち
* 2 パー 勝ち 負け 引分け
* --------- ------- -------- --------
*/
public class Quiz1710 {
public static void main(String[] args) {
String[] HAND = {"グー", "チョキ", "パー"};
String[] JUDGE = {" あなたの負けです。", " 引き分けです。", " あなたの勝ちです。"};
// 標準入力を準備する
Scanner sin = new Scanner(System.in);
// じゃんけんの手を入力する
System.out.print("じゃんけんの手(0:グー、 1:チョキ、 2:パー )を入力してください => ");
int userTe = sin.nextInt(); // 標準入力から手を入力する
int computerTe = (int) (Math.random() * 3); // コンピューターの手
// 勝負する
int judge = judge(userTe, computerTe); // 勝敗結果 -1: 負け、0: 引分け、1: 勝ち
// 結果を表示する
System.out.print("あなた: " + HAND[userTe] + ", コンピューター: " + HAND[computerTe]);
System.out.println(JUDGE[judge + 1]);
}
/**
* 2つのじゃんけんの手の勝負結果を返す。
* @param te1 手1
* @param te2 手2
* @return 手1が負けたとき-1、引分けのとき0、勝ったとき1
*/
static int judge(int te1, int te2) {
int[][] JUDGE = {
{ 0, 1, -1}, // ぐー
{-1, 0, 1}, // ちょき
{ 1, -1, 0}, // ぱー
};
return JUDGE[te1][te2];
}
}
/* 実行結果例
じゃんけんの手(0:グー、 1:チョキ、 2:パー )を入力してください => 0
あなた: グー, コンピューター: グー 引き分けです。
*/
-------------------------------------------------
import java.util.Scanner;
/**
* 23.重さに対する料金を出す。
* 6×2の配列の、1列目に重さ、2列目に料金が
* 入っている。
* なお、重さは、下の表では以下をあらわす。
* たとえば、200gに対しては、190円になる。
* 2000gより重い場合は、一律に2000円とする
*
* ------- ----
* 重さ 料金
* ------- ----
* [0] 100 100
* [1] 200 190
* [2] 400 370
* [3] 600 550
* [4] 1000 900
* [5] 2000 1700
* ------- ----
*/
public class Quiz1711 {
public static void main(String[] args) {
String buffer; // データを入力するバッファー
int WEIGHT_RATE[][] = { // 重量-料金表
{ 100, 100},
{ 200, 190},
{ 400, 370},
{ 600, 550},
{1000, 900},
{2000, 1700},
};
int weight; // 重量
// 標準入力を設定する
Scanner in = new Scanner(System.in);
// データを入力する。
System.out.printf("重量を入力してください。");
buffer = in.nextLine();
weight = Integer.parseInt(buffer);
// 料金を引く
int rate = rate(WEIGHT_RATE, weight);
// 結果を表示する
System.out.println(weight + " g の料金は " + rate + " 円です。");
}
/**
* 料金表と重さを渡して、その重さに対する料金を返す。
* @param rateTable 料金表
* @param weight 重さ
* @return 重さに対する料金。表にない場合は、2000.
*/
private static int rate(int[][] rateTable, int weight) {
for (int i = 0; i < rateTable.length; i++) {
if (weight <= rateTable[i][0]) {
return rateTable[i][1];
}
}
return 2000;
}
}
/* 実行結果例
>java Quiz1711
重量を入力してください。50
50 g の料金は 100 円です。
>java Quiz1711
重量を入力してください。100
100 g の料金は 100 円です。
>java Quiz1711
重量を入力してください。150
150 g の料金は 190 円です。
>java Quiz1711
重量を入力してください。2000
2000 g の料金は 1700 円です。
>java Quiz1711
重量を入力してください。2001
2001 g の料金は 2000 円です。
*/
-------------------------------------------------