題目連結
題意:
有一種 Team Game,奇數個成員
給定所有人的年紀
題目要決定一個老大
比他大的人數要與比他小的相同
求老大是誰,輸出他的年紀
解法:
奇數堆,依題意就是要中位數
這題關鍵在 Input 最後提到
測資必然遞增或遞減
都幫你排序過了,把中間抓出來就好
程式(Java):
題意:
有一種 Team Game,奇數個成員
給定所有人的年紀
題目要決定一個老大
比他大的人數要與比他小的相同
求老大是誰,輸出他的年紀
解法:
奇數堆,依題意就是要中位數
這題關鍵在 Input 最後提到
測資必然遞增或遞減
都幫你排序過了,把中間抓出來就好
程式(Java):
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 sc = new Scanner(System.in); | |
int T = sc.nextInt(); | |
for (int t = 1; t <= T; t++) { | |
int N = sc.nextInt(); | |
for (int i = 1; i <= N; i++) { | |
int tmp = sc.nextInt(); | |
if (i == N/2+1) | |
System.out.println("Case " + t + ": " + tmp); | |
} | |
} | |
} | |
} |
留言
張貼留言