NC24836. [USACO 2009 Mar B]The Perfect Cow
描述
输入描述
* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains N space-separated integers that are the N beauty indices for row i of the cow square
输出描述
* Line 1: A single integer that is the index of the most perfect cow as described in the task.
示例1
输入:
5 1 5 3 9 5 2 5 3 8 1 6 3 5 9 2 8 8 3 3 2 5 4 4 4 4
输出:
4
说明:
N=5, so there are 25 cows. They line up in a 5 x 5 square as shown.C++14(g++5.4) 解法, 执行用时: 5ms, 内存消耗: 496K, 提交时间: 2019-09-15 21:56:42
#include<bits/stdc++.h> #define MAX_INT ((unsigned)(-1)>>1) #define MIN_INT (~MAX_INT) #define pi 3.1415926535898 using namespace std; int n; int a[105][105]; int b[105]; int main(void) { cin>>n; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ cin>>a[i][j]; } sort(a[i],a[i]+n); b[i]=a[i][n/2]; } sort(b,b+n); cout<<b[n/2]; return 0; }
C++11(clang++ 3.9) 解法, 执行用时: 6ms, 内存消耗: 488K, 提交时间: 2019-09-07 10:06:00
#include<bits/stdc++.h> using namespace std; int a[2001]; int n,ans[2001]; int main() { cin>>n; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { cin>>a[j]; } sort(a+1,a+n+1); ans[i]=a[(n/2)+1]; } sort(ans+1,ans+n+1); cout<<ans[(n/2)+1]; }