1. 程式人生 > >水題 第五站 HDU Ignatius and the Princess IV

水題 第五站 HDU Ignatius and the Princess IV

剛開始看到這個題目覺得簡單得不行,考慮是將數字存入陣列,然後再進行遍歷,可以考慮一下這樣的複雜度是n^2,我覺得思路很清晰就沒敲程式碼,看自己去年提交過就看了下自己當時的程式碼,附當時程式碼

#include<iostream>
#include<stdio.h>
#include<string.h>
#define M 50000
using namespace std;
int tem,b[M];
int main(){
    int n,i,j;
    while(scanf("%d",&n)!=EOF){
        memset
(b,0,sizeof(b)); for(i=0;i<n;i++){ scanf("%d",&tem); b[tem]++; if(b[tem]>=(n+1)/2) j=tem; } printf("%d\n",j); } return 0; }

簡單好多,現在覺得n^2的思路肯定會超時,將陣列的下標和存的數交換一下位置,程式碼一下簡單了好多= =佩服當時的自己。