zoukankan      html  css  js  c++  java
  • HDU 1029 Ignatius and the Princess IV

    Ignatius and the Princess IV

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K (Java/Others)
    Total Submission(s): 18818    Accepted Submission(s): 7603


    Problem Description
    "OK, you are not too bad, em... But you can never pass the next test." feng5166 says.

    "I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers." feng5166 says.

    "But what is the characteristic of the special integer?" Ignatius asks.

    "The integer will appear at least (N+1)/2 times. If you can't find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha....." feng5166 says.

    Can you find the special integer for Ignatius?
     
    Input
    The input contains several test cases. Each test case contains two lines. The first line consists of an odd integer N(1<=N<=999999) which indicate the number of the integers feng5166 will tell our hero. The second line contains the N integers. The input is terminated by the end of file.
     
    Output
    For each test case, you have to output only one line which contains the special number you have found.
     
    Sample Input
    5 1 3 2 3 3 11 1 1 1 1 1 5 5 5 5 5 5 7 1 1 1 1 1 1 1
     
    Sample Output
    3 5 1
     

    这个问题比较简单,很多的前辈已经写过了,我最近做了一下,也提提我的拙见;

    首先题目的大意是:给你一个项数为n的数列t,这个数列中有一个数出现的次数最多,并且出现的次数大于n/2;

    我的思路是先排序,然后取t[n/2],t[n/2]就是这个数;

    代码一:

    sort排序的:561MS;

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<iomanip>
     5 #include<cctype>
     6 #include<string>
     7 #include<cmath>
     8 #include<cstdio>
     9 #include<cstdlib>
    10 #define LL long long
    11 #define PF(x) ((x)*(x))
    12 #define LF(x) ((x)*PF(x))
    13 
    14 using namespace std;
    15 const int INF=1<<31-1;
    16 const int max9=1e9;
    17 const int max6=1e6;
    18 const int max3=1e3;
    19 
    20 int gcd(int a,int b)
    21 {
    22     return b==0?a:gcd(b,a%b);
    23 }
    24 int t[max6+5];
    25 int main()
    26 {
    27     int n;
    28     while(cin >> n)
    29     {
    30         for(int i=0;i<n;i++) cin >> t[i];
    31         sort(t,t+n);
    32         cout << t[n/2] << endl;
    33     }
    34     return 0;
    35 }
    View Code

    代码二:

    qsort排序的:390MS

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<iomanip>
     5 #include<cctype>
     6 #include<string>
     7 #include<cmath>
     8 #include<cstdio>
     9 #include<cstdlib>
    10 #define LL long long
    11 #define PF(x) ((x)*(x))
    12 #define LF(x) ((x)*PF(x))
    13 
    14 using namespace std;
    15 const int INF=1<<31-1;
    16 const int max9=1e9;
    17 const int max6=1e6;
    18 const int max3=1e3;
    19 
    20 int gcd(int a,int b)
    21 {
    22     return b==0?a:gcd(b,a%b);
    23 }
    24 int t[max6+5];
    25 int comp(const void *a,const void *b)
    26 {
    27     return *(int *)a-*(int *)b;
    28 }
    29 int main()
    30 {
    31     int n;
    32     while(cin >> n)
    33     {
    34         for(int i=0;i<n;i++) cin >> t[i];
    35         qsort(t,n,sizeof(t[0]),comp);
    36         cout << t[n/2] << endl;
    37     }
    38     return 0;
    39 }
    View Code
  • 相关阅读:
    纯css切换左侧菜单
    HDU——T 1556 Color the ball
    CODEVS——T 1404 字符串匹配
    HDU——T 1506 Largest Rectangle in a Histogram|| POJ——T 2559 Largest Rectangle in a Histogram
    BZOJ——T 1113: [Poi2008]海报PLA
    POJ——T 2796 Feel Good
    November 27th 2016 Week 48th Sunday
    November 26th 2016 Week 48th Saturday
    November 25th 2016 Week 48th Friday
    November 24th 2016 Week 48th Thursday
  • 原文地址:https://www.cnblogs.com/I-love-HLD/p/4298348.html
Copyright © 2011-2022 走看看