2016SDAU课程练习一1016
2016-03-21 20:21
183 查看
Problem Q
Problem Description
FJ is surveying his herd to find the most average cow. He wants to know how much milk this 'median' cow gives: half of the cows give as much or more than the median; half give as much or less.
Given an odd number of cows N (1 <= N < 10,000) and their milk output (1..1,000,000), find the median amount of milk given such that at least half the cows give the same amount of milk or more and at least half give the same or less.
Input
* Line 1: A single integer N <br> <br>* Lines 2..N+1: Each line contains a single integer that is the milk output of one cow.
Output
* Line 1: A single integer that is the median milk output.
Sample Input
5
2
4
1
3
5
Sample Output
3
题意:平均值啦
思路:排序取中间的
感想:应该是最简单的吧,但是每次博客写完粘代码就手残刷新了。。刷新。。。刷新。。。。这题有毒。。。
AC代码:
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<numeric>
#include<math.h>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
using namespace std;
int main()
{
int n,i,a,b,s;
vector<int>v;
while(cin>>n)
{
for(i=0;i<n;i++)
{
cin>>a;
v.push_back(a);
}
sort(v.begin(),v.end());
if(n%2!=0)
{
b=(n+1)/2;
s=v[b-1];
}
else
{
b=n/2;
s=(v[b-1]+v[b])/2;
}
cout<<s<<endl;
}
return 0;
}
Problem Description
FJ is surveying his herd to find the most average cow. He wants to know how much milk this 'median' cow gives: half of the cows give as much or more than the median; half give as much or less.
Given an odd number of cows N (1 <= N < 10,000) and their milk output (1..1,000,000), find the median amount of milk given such that at least half the cows give the same amount of milk or more and at least half give the same or less.
Input
* Line 1: A single integer N <br> <br>* Lines 2..N+1: Each line contains a single integer that is the milk output of one cow.
Output
* Line 1: A single integer that is the median milk output.
Sample Input
5
2
4
1
3
5
Sample Output
3
题意:平均值啦
思路:排序取中间的
感想:应该是最简单的吧,但是每次博客写完粘代码就手残刷新了。。刷新。。。刷新。。。。这题有毒。。。
AC代码:
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<numeric>
#include<math.h>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
using namespace std;
int main()
{
int n,i,a,b,s;
vector<int>v;
while(cin>>n)
{
for(i=0;i<n;i++)
{
cin>>a;
v.push_back(a);
}
sort(v.begin(),v.end());
if(n%2!=0)
{
b=(n+1)/2;
s=v[b-1];
}
else
{
b=n/2;
s=(v[b-1]+v[b])/2;
}
cout<<s<<endl;
}
return 0;
}
相关文章推荐
- python中主线程等待子线程完成的实现(join())
- (Leetcode 82) Remove Duplicates from Sorted List II (Java)
- HDOJ 1010 Temper of the Bone
- VSTS写单元测试
- 0321复利计算4.0
- hdu 1671(字典树判断前缀)
- iOS--归档和解档(Archiver)、(UnArchiver)
- 第一范式
- 利用Apache commons pool2构建池应用
- ListView,ScrollView,RecyclerView被HorizontalScrollView嵌套,加载图片
- java socket报文通信(二)报文的封装
- empty() 和 isset() 的区别
- linux idea14 maven 部署 tomcat. 遇到的问题
- zTree实现清空选中的第一个节点的子节点
- 个人软件开发委托书范本
- 软件工程作业1
- flask 使用错误总结
- 高效求解自然数k次幂和的方法
- mogondb安装常识
- eclipse集成Hadoop 开发环境