日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

E - Ignatius and the Princess IV

發(fā)布時間:2024/2/28 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 E - Ignatius and the Princess IV 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

####E - Ignatius and the Princess IV
“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èi)存限制,所以用幾個大數(shù)組儲存數(shù)據(jù)并且進行運算是行不通的(如:a[i]=k) ,因此改變策略:輸入的n個數(shù)中 每個數(shù)都看作是一個位置信息(如a[k]++),通過置零該數(shù)組解決循環(huán)問題
####代碼實現(xiàn):

/* 錯誤代碼: #include<iostream> #include<cstdio> long a[1000000] = {0} ;using namespace std ;int main () {long n , i , d , ; //最好用配套的long while (scanf( "%d" , &n ) != EOF ){for ( i = 0 ; i < n ; i++ ){// 數(shù)組第一個循環(huán)后沒有被清空 導(dǎo)致第二個循環(huán)里的數(shù)組不全為0 cin >> d ;a[d]++ ;if ( a[d] >= (n+1)/2 ){cout << d << endl ;break ; // a[1000000] = {0} ; //在這里遍歷long不好使 }}}return 0 ;} */ //正確代碼: #include<iostream> #include<cstdio> #include<string.h> long a[1000000] = {0}; //在外面定義 在里面賦值 using namespace std ; int main() {long n,i,t,re; //配套long while(scanf("%ld",&n)!=EOF)//在int main 里定義大數(shù)組不好使 { memset(a,0,1000000); //memset 把數(shù)組a 前1000000個值賦值0 如果用在整型數(shù)組里 只能賦值0或-1. for(i=0;i<n;i++){scanf("%ld",&t);a[t]++;if(a[t]>=(n+1)/2){re = t ;} }printf("%ld\n",re);} } /* 教訓(xùn): 1:由于long型數(shù)組太大 無法在int main 里初始化 這就導(dǎo)致了前一個循環(huán)完成后,在后一個循環(huán)里數(shù)組的數(shù)不全為0 , 所以要用memset去初始化 2:如果不用re代替t 則可能出現(xiàn)的情況中 輸入的數(shù)字都相同 那么結(jié)果會提前輸出 所以要建立一個中間變量去儲存該值 */

總結(jié)

以上是生活随笔為你收集整理的E - Ignatius and the Princess IV的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。