This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner scn = new Scanner(System.in); | |
while(scn.hasNext()){ | |
String arr[]=scn.nextLine().split(" "); | |
int i=0; | |
for(i=0;i<arr.length;i++){ | |
int count=0; | |
for(int j=0;j<arr.length;j++){ | |
if(arr[i].equals(arr[j])) | |
count++; | |
} | |
if(arr.length/2<count){ | |
System.out.println(arr[i]); | |
break; | |
} | |
} | |
if(i==arr.length) | |
System.out.println("NO"); | |
} | |
} | |
/*題目:[C_AR42-易] 過半元素 | |
作者:1010 | |
時間:西元 2017 年 2 月 */ | |
} |
這題由於不知有多少數列故用字串切割去比對過半數