package com.huawei.TianJiSaiMa;
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
/**
* 广义田忌赛马:每匹马都有一个能力指数,齐威王先选马,田忌后选,马的能力相同,则齐威王胜。
* 输入:马匹数量 \n 齐威王的马匹能力值 \n 田忌的马匹能力值(能力值之间都是以空格分开)
* eg:输入:3 \n 1 2 3 \n 1 2 3 输出:2
* @author patrick
*
*/
public class TianJiSaiMa {
public static void main(String args[]) {
List<Integer> lTian = new ArrayList<Integer>();
List<Integer> lQi = new ArrayList<Integer>();
Scanner in = new Scanner(System.in);
System.out.println("参加赛马的马匹数量:");
int n = Integer.parseInt(in.nextLine().trim());
// 先得到齐威王的田忌的能力值
System.out.println("齐威王的马匹能力值:");
String qi = in.nextLine().trim();
// 然后得到田忌的田忌的能力值
System.out.println("田忌的马匹能力值:");
String tian = in.nextLine().trim();
if (!tian.matches("[\\d+ ]*") || !qi.matches("[\\d+ ]*")) {
System.err.println("input error !");
return;
}
String[] tAblity = tian.split(" ");
String[] qAblity = qi.split(" ");
if (tAblity.length != n || qAblity.length != n) {
System.err.println("the number of horses wrong !");
return;
}
for (int i = 0; i < n; i++) {
lTian.add(Integer.parseInt(tAblity[i]));
lQi.add(Integer.parseInt(qAblity[i]));
}
Collections.sort(lTian);
Collections.sort(lQi);
int i = 0, j = 0, x = n - 1, y = n - 1, cnt = 0;
boolean bLast = true;
while (bLast) {
// 是否是最后一匹马
if (x == i)
bLast = false;
if (lTian.get(x) > lQi.get(y)) {// 如果田忌当前最好的马可以胜齐王最好的马,那么比一场
x--;
y--;
cnt += 1;
} else if (lTian.get(i) > lQi.get(j)) {// 如果田忌当前最差的马可以胜齐王最差的马,那么比一场
i++;
j++;
cnt += 1;
} else {// 否则,让田忌最差的马和齐王最好的好比一场
i++;
y--;
}
}
System.out.println("田忌可以赢得赛马的场数为 " + cnt + " 场。");
lTian.clear();
lQi.clear();
}
}
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
/**
* 广义田忌赛马:每匹马都有一个能力指数,齐威王先选马,田忌后选,马的能力相同,则齐威王胜。
* 输入:马匹数量 \n 齐威王的马匹能力值 \n 田忌的马匹能力值(能力值之间都是以空格分开)
* eg:输入:3 \n 1 2 3 \n 1 2 3 输出:2
* @author patrick
*
*/
public class TianJiSaiMa {
public static void main(String args[]) {
List<Integer> lTian = new ArrayList<Integer>();
List<Integer> lQi = new ArrayList<Integer>();
Scanner in = new Scanner(System.in);
System.out.println("参加赛马的马匹数量:");
int n = Integer.parseInt(in.nextLine().trim());
// 先得到齐威王的田忌的能力值
System.out.println("齐威王的马匹能力值:");
String qi = in.nextLine().trim();
// 然后得到田忌的田忌的能力值
System.out.println("田忌的马匹能力值:");
String tian = in.nextLine().trim();
if (!tian.matches("[\\d+ ]*") || !qi.matches("[\\d+ ]*")) {
System.err.println("input error !");
return;
}
String[] tAblity = tian.split(" ");
String[] qAblity = qi.split(" ");
if (tAblity.length != n || qAblity.length != n) {
System.err.println("the number of horses wrong !");
return;
}
for (int i = 0; i < n; i++) {
lTian.add(Integer.parseInt(tAblity[i]));
lQi.add(Integer.parseInt(qAblity[i]));
}
Collections.sort(lTian);
Collections.sort(lQi);
int i = 0, j = 0, x = n - 1, y = n - 1, cnt = 0;
boolean bLast = true;
while (bLast) {
// 是否是最后一匹马
if (x == i)
bLast = false;
if (lTian.get(x) > lQi.get(y)) {// 如果田忌当前最好的马可以胜齐王最好的马,那么比一场
x--;
y--;
cnt += 1;
} else if (lTian.get(i) > lQi.get(j)) {// 如果田忌当前最差的马可以胜齐王最差的马,那么比一场
i++;
j++;
cnt += 1;
} else {// 否则,让田忌最差的马和齐王最好的好比一场
i++;
y--;
}
}
System.out.println("田忌可以赢得赛马的场数为 " + cnt + " 场。");
lTian.clear();
lQi.clear();
}
}