zoukankan      html  css  js  c++  java
  • UVA 10057 A mid-summer night's dream. 仲夏夜之梦 求中位数

    题意:求中位数,以及能成为中位数的数的个数,以及选择不同中位数中间的可能性。

    也就是说当数组个数为奇数时,中位数就只有一个,中间那个以及中位数相等的数都能成为中位数,选择的中位数就只有一种可能;如果为偶数,中位数又两个,同样和那两个相等的数都能成为中位数,在[第一个中位数,第二个中位数]这一区间中拥有的整数个数就是所求的第三个数。

    分类讨论,模拟即可。

    代码:

     /*
     *   Author:        illuz <iilluzen@gmail.com>
     *   Blog:          http://blog.csdn.net/hcbbt
     *   File:          uva10057.cpp
     *   Lauguage:      C/C++
     *   Create Date:   2013-08-25 11:30:04
     *   Descripton:    UVA 10057 A mid-summer night's dream, simulation 
     */
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <iostream>
    #include <list>
    #include <vector>
    #include <map>
    #include <set>
    #include <deque>
    #include <queue>
    #include <stack>
    #include <utility>
    #include <algorithm>
    using namespace std;
    #define rep(i, n) for (int i = 0; i < (n); i++)
    #define repu(i, a, b) for (int i = (a); i < (b); i++)
    #define repf(i, a, b) for (int i = (a); i <= (b); i++)
    #define repd(i, a, b) for (int i = (a); i >= (b); i--)
    #define swap(a, b) {int t = a; a = b; b = t;}
    #define mc(a) memset(a, 0, sizeof(a))
    #define ms(a, i) memset(a, i, sizeof(a))
    #define sqr(x) ((x) * (x))
    #define FI(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
    typedef long long LL;
    typedef unsigned long long ULL;
    
    /****** TEMPLATE ENDS ******/
    
    const int MAXN = 1000000;
    int n, num[MAXN], n1, n2;
    
    int main() {
    	while (scanf("%d", &n) != EOF) {
    		rep(i, n) {
    			scanf("%d", &num[i]);
    		}
    		sort (num, num + n);
    		int k = (n - 1) / 2;
    		n1 = 0; n2 = 1;
    		if (n % 2) {
    			for (int i = k; i >= 0 && num[k] == num[i]; i--)
    				n1++;
    			for (int i = k + 1; i < n && num[k] == num[i]; i++)
    				n1++;
    		}
    		else {
    			for (int i = k; i >= 0 && num[k] == num[i]; i--)
    				n1++;
    			for (int i = k + 1; i < n && num[k + 1] == num[i]; i++)
    				n1++;
    			n2 = num[k + 1] - num[k] + 1;
    		}
    		printf("%d %d %d
    ", num[k], n1, n2);
    	}
    	return 0;
    }


  • 相关阅读:
    ps_cc:制作sprite拼贴图片
    pc端的企业网站(IT修真院test9)详解一个响应式完成的pc端项目
    pc端的企业网站(IT修真院test8)详解1-4
    pc端的企业网站(IT修真院test8)详解1-3
    pc端的企业网站(IT修真院test8)详解1-2
    pc端的企业网站(IT修真院test8)详解1-1
    ps_cc切片
    Sublime Text通过插件编译Sass为CSS及中文编译异常解决
    PostCSS一种更优雅、更简单的书写CSS方式
    Gulp自动添加版本号
  • 原文地址:https://www.cnblogs.com/pangblog/p/3283351.html
Copyright © 2011-2022 走看看