http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=29920 依題意找出重複字元並且大於n/2的元素,因為不知有多少個字元所以存入字串再切割做比對
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String str = scn.nextLine(), s[] = str.split(" ");
int count = 1;
for (int i = 0; i < s.length; i++) {
count = 1;
for (int j = i + 1; j < s.length; j++) {
if (s[i].equals(s[j])) {
count++;
}
}
if (count > s.length / 2) {
System.out.println(s[i]);
break;
}
}
if (count == 1)
System.out.println("n/a");
}
/*
題目:F91
作者:1010
時間:西元 2016 年 7 月 */
}