该程序包含多组测试数据,每一组测试数据的第一行为N,代表该组测试数据包含的数据个数,1<=N<=10000. 接着N行为N个数据的输入,N=0时结束输入
输出中位数,每一组测试数据输出一行
4 10 30 20 40 3 40 30 50 4 1 2 3 4 0
25 40 2
//借助快排思路,找到中位数就立刻返回,无需全部排序~ #include<iostream> (720)#include<vector> using namespace std; int partition(vector<int> &arr, int left, int right) { int mid_val = arr[left]; while(left < right) { while(left < right && arr[right] >= mid_val) --right; arr[left] = arr[right]; while(left < right && arr[left] <= mid_val) ++left; arr[right] = arr[left]; } arr[left] = mid_val; return left; } void qS(vector<int> &arr, int left, int right, int mid_index, int mid_p1_index, bool &m1, bool &m2) { if(m1 && m2) return; if(left < right) { int mid=partition(arr, left, right); if(mid_index == mid) m1=true; else if(mid_p1_index == mid) m2=true; qS(arr, left, mid-1, mid_index, mid_p1_index, m1, m2); qS(arr, mid+1, right, mid_index, mid_p1_index, m1, m2); } } int main() { int n; while(cin>>n) { vector<int> arr(n, 0); for(int i=0; i<n; ++i) cin>>arr[i]; int mid_index=(n-1)/2; bool m1=false, m2=false; qS(arr, 0, n-1, mid_index, mid_index+1, m1, m2); if(n&1) cout<<arr[mid_index]<<endl; else cout<<(arr[mid_index]+arr[mid_index+1])/2<<endl; } }
#include<stdio.h> int main() { int n,i,j,t,a[10000]; scanf("%d",&n); //1.输入 for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n-1;i++)//2.排序 for(j=0;j<n-1-i;j++) if(a[j]>a[j+1]) {//交换 t=a[j];a[j]=a[j+1];a[j+1]=t; } if(n%2==0)//证明是偶数,取平均值 //3.计算中位数 printf("%d\n",(a[n/2]+a[n/2-1])/2); else printf("%d",a[n/2]); }
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i]=scanner.nextInt(); Arrays.sort(a); int len =a.length; if (len%2==0) System.out.println((a[len/2-1]+a[len/2])/2); else System.out.println(a[(len-1)/2]); } } }
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ int n; while(cin>>n && n!=0){ vector<int> vec(n); for(auto& a:vec) cin>>a; if(n%2==0){ int a = n/2; int b = a-1; nth_element(vec.begin(),vec.begin()+b,vec.end()); int el1 = vec[b]; nth_element(vec.begin()+b,vec.begin()+a,vec.end()); int el2 = vec[a]; cout<<(el1+el2)/2<<endl; }else{ int a= n/2; nth_element(vec.begin(),vec.begin()+a,vec.end()); int el2 = vec[a]; cout<<el2<<endl; } } return 0; }不需要排序全部,用快速排序的划分思想。
import java.util.*; public class Main{ // 利用快排的思路寻找一个数组中第target大的数 private static int getTarget(int[] data, int target){ int mid=data[0], l=0, r=data.length-1; while(l < r){ while(l < r && data[r] > mid) r--; if(l < r) data[l++] = data[r]; while(l < r && data[l] < mid) l++; if(l < r) data[r--] = data[l]; } data[l] = mid; if(l+1 == target) return data[l]; else if(l+1 < target) return getTarget(Arrays.copyOfRange(data, l+1, data.length), target-(l+1)); else return getTarget(Arrays.copyOfRange(data, 0, l), target); } public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNextInt()){ int n = sc.nextInt(); if(n == 0) break; int[] data = new int[n]; for(int i=0; i<n; i++) data[i] = sc.nextInt(); // 寻找一个数组中最中间的两个数 int mid1 = (data.length+1)/2; int mid2 = (data.length+2)/2; System.out.println((getTarget(data, mid1)+getTarget(data, mid2))/2); } sc.close(); } }
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextInt()) { int n = sc.nextInt(); if(n == 0) break; int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = sc.nextInt(); Arrays.sort(arr); if ((n & 1) == 1) System.out.println(arr[(n - 1) / 2]); else System.out.println((arr[n / 2] + arr[n / 2 - 1] ) / 2); } sc.close(); } }
#include <stdio.h> #include <algorithm> #define N 10000 using namespace std; int main() { int buf[N]; int n; while(scanf("%d", &n)!=EOF) { if(n<=0) break; for(int i=0; i<n; i++) { scanf("%d", &buf[i]); } sort(buf, buf+n); int mid=(n-1)/2; if(n%2==1) printf("%d\n", buf[mid]); else printf("%d\n", (buf[mid]+buf[mid+1])/2); } return 0; }
#include<iostream> #include<vector> #include<algorithm> using namespace std; int main(){ vector<int> num; int n;cin>>n; for(int i=0;i<n;++i){ int number;cin>>number; num.push_back(number); } sort(num.begin(), num.end()); if(num.size()%2!=0){ cout<<num[num.size()/2]; } else{ cout<<(num[num.size()/2]+num[num.size()/2-1])/2; } return 0; }
#include<algorithm> #include<iostream> using namespace std; int main(){ int n; while(cin>>n){ if(n==0) break; int* a=new int[n]; for(int i=0;i<n;i++) cin>>a[i]; sort(a,a+n); if(n%2!=0) cout<<a[n/2]<<endl; else cout<<(a[n/2]+a[n/2-1])/2<<endl; delete []a; } }
def bubble(a): for i in range(len(a) - 1): for j in range(len(a) - 1 - i): if a[j] > a[j+1]: t = a[j] a[j] = a[j+1] a[j+1] = t return a def mid(a): a = bubble(a) # print(a) if len(a) % 2 == 0: i1 = int(len(a) / 2) i2 = int(len(a) / 2) - 1 m = (a[i1] + a[i2]) / 2 else: i = int(len(a) / 2) m = a[i] return int(m) while True: try: arr = [] n = int(input()) for _ in range(n): a = int(input()) arr.append(a) res = mid(arr) print(res) except: break
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n; while (cin >> n) { if (n == 0) { break; } vector<int>arr(n); for (auto& i : arr) { cin >> i; } sort(arr.begin(), arr.end()); cout << (n % 2 == 1 ? arr[(n - 1) / 2] : (arr[(n - 1) / 2] + arr[(n + 1) / 2]) / 2) << endl; } return 0; }
#include <cstdio> #include <algorithm> using namespace std; int main(){ int n; while(scanf("%d",&n) != EOF){ if(n==0) break; int arr[n]; for(int i = 0; i < n; ++i){ scanf("%d",&arr[i]); } //1.升序排序 sort(arr,arr+n); //2.找中位数 if(n%2 == 1){ //n为奇数 printf("%d\n",arr[n/2]);//arr[n/2]为中位数 }else{ //n为偶数 printf("%d\n",(arr[n/2]+arr[n/2-1])/2); } } return 0; }
#include <iostream> #include <algorithm> using namespace std; int main() { int n,a[10020]; int res; while(cin>>n&&n!=0){ for(int i=0;i<n;i++){ cin>>a[i]; } sort(a,a+n);//排序 if(n%2==0){//n为偶数,则取中间两个数的平均数 res=(a[n/2]+a[n/2-1])/2; } else{//n为奇数,则取中间的数 res=a[n/2]; } printf("%d\n",res); } }