zoukankan      html  css  js  c++  java
  • 九度oj 1004

    题目1004:Median            

    时间限制:1 秒

    内存限制:32 兆

    特殊判题:

    提交:17536

    解决:4860

    题目描述:                       

        Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1={11, 12, 13, 14} is 12, and the median of S2={9, 10, 15, 16, 17} is 15. The median of two sequences is defined to be the median of the non-decreasing sequence which contains all the elements of both sequences. For example, the median of S1 and S2 is 13.     Given two increasing sequences of integers, you are asked to find their median.

    输入:                       

        Each input file may contain more than one test case.     Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (≤1000000) is the size of that sequence. Then N integers follow, separated by a space.     It is guaranteed that all the integers are in the range of long int.

    输出:                       

        For each test case you should output the median of the two given sequences in a line.

    样例输入:                       
    4 11 12 13 14
    5 9 10 15 16 17
    样例输出:                       
    13

    此题要求输出两个递增数列并到一起后的中间数,代码如下
     1 #include <stdio.h>
     2 #define MAX 1000002
     3 long int A[MAX];
     4 long int B[MAX];
     5 int main() {
     6     int M,N;
     7     while(scanf("%d",&M) != EOF) {
     8       for(int i = 0; i < M; i++) {
     9         scanf("%ld",&A[i]);
    10       }
    11       scanf("%d",&N);
    12       for(int i = 0; i < N; i++) {
    13         scanf("%ld",&B[i]);
    14       }
    15       int middle = (M+N-1)/2;
    16       int a = 0, b =0, i = 0;
    17       int flag = 0;
    18       while(i <= middle) {
    19         if(A[a] < B[b] && a < M && b < N) {
    20           a++;
    21           i++;
    22           flag = 0;
    23           //printf("%d
    ",A[a]);
    24           continue;
    25         }
    26         else if(A[a] > B[b] && a < M && b < N) {
    27           b++;
    28           i++;
    29           flag = 1;
    30           //printf("%d
    ",B[b]);
    31           continue;
    32         }
    33         else if(a < M && b < N){
    34           a++;
    35           b++;
    36           i++;
    37           i++;
    38           flag = 0;
    39           //printf("%d
    ",A[a]);
    40           continue;
    41         }
    42         else if(a < M && b >= N) {
    43           a++;
    44           i++;
    45           flag = 0;
    46           //printf("%d
    ",A[a]);
    47           continue;
    48         }
    49         else if(a >= M && b < N) {
    50           b++;
    51           i++;
    52           flag = 1;
    53           //printf("%d
    ",B[b]);
    54           continue;
    55         }
    56       }
    57       if(flag == 0) {
    58         printf("%ld
    ", A[a-1]);
    59       }
    60       else {
    61         printf("%ld
    ", B[b-1]);
    62       }
    63     }
    64     return 0;
    65 }
  • 相关阅读:
    Drawable和Bitmap的区别
    Android中的Drawable资源
    了解Objective-C中NSAutoreleasePool使用方法
    Object-C 内存管理及对象
    事件类型: 错误 事件来源: Service Control Manager 事件种类: 无 事件 ID: 7000
    HTML xmlns
    asp.net(C#)清除全部Session与单个Session
    Html学习笔记---html5表单元素
    jquery学习笔记---jquery事件($.event.special )
    C#学习笔记---Dispose(),Finalize(),SuppressFinalize
  • 原文地址:https://www.cnblogs.com/jasonJie/p/5651424.html
Copyright © 2011-2022 走看看