The 11th Zhejiang University Programming Contest - J/ Judge Internal Error

返回目录

 

题目大意:输入一串数,输出出现次数最多的那个数,若有多个数符合则输出最大的那个 。

 

题目类型: 基础模拟题

 

题目分析: 把一组数升序排序,然后从头到尾扫描更新出现最多次数的数。

 

代码:

 

#include<cstdio> #include<cstring> #include <algorithm> //sort using namespace std; #define MAXN 1010 int a[MAXN]; int n; int f() { int ans, sum1=1, sum2=1; sort(a,a+n); //升序 int i; for(i=1; i<n; i++) { if(a[i] == a[i-1]) sum2++; else { sum1 = sum1>sum2? sum1: (ans = a[i-1], sum2); sum2=1; } } sum1 = sum1>sum2? sum1: (ans = a[i-1], sum2); return ans; } int main() { int t; scanf("%d", &t); while(t--) { scanf("%d", &n); for(int i=0; i<n; i++) scanf("%d", &a[i]); printf("%d/n", f()); } }

你可能感兴趣的:(The 11th Zhejiang University Programming Contest - J/ Judge Internal Error)