2016騰訊模擬試題程式設計第二道
阿新 • • 發佈:2018-12-29
</pre><pre code_snippet_id="1632794" snippet_file_name="blog_20160402_1_924315" name="code" class="cpp">#include<iostream> #include<cmath> #include<vector> using namespace std; class Gift { public: int getValue(vector<int> gifts, int n) { // write code here //先氣泡排序 int len = n; int tmp; for(int i=0;i<len;i++){ for(int j=0;j<len-i-1;j++){ if(gifts[j]>gifts[j+1]){ tmp = gifts[j]; gifts[j] =gifts[j+1]; gifts[j+1] = tmp; } } } int Maxnum = 0; int compa = gifts[len/2]; for(int i=0;i<len;i++){ if(gifts[i] == compa){ Maxnum++; } } if(Maxnum>len/2){ return compa; } return 0; //for(int i=0;i<len;i++){ // cout<<gifts[i]<<" "; //} } }; int main(){ vector<int> gifts; int e; int len=0; Gift g; cin>>e; while(e>0){ //如果大於0,則存入陣列,長度+1; len++; gifts.push_back(e); cin>>e; } cout<<len<<endl; int max = g.getValue(gifts,len); cout<<max<<endl; return 0; }