當前位置:
首頁 >
【PAT甲级 找到出现次数过半的数字】1054 The Dominant Color (20 分) C++
發布時間:2024/2/28
56
豆豆
生活随笔
收集整理的這篇文章主要介紹了
【PAT甲级 找到出现次数过半的数字】1054 The Dominant Color (20 分) C++
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目
思路
- 用數組模擬維護一個棧,棧內存儲的數據有兩個屬性:數字、數量。
- 讀取數字,如果數字已存在,相應位置上的數量+1
- 如果數字不存在,新建這個數字,size++,相應位置上的數量設置為1
題解 C++
#include<iostream> #include<string> #include<algorithm> #define NUM 480000 using namespace std; int main() {int t1, t2;cin >> t1 >> t2;int total = t1 * t2;int arr[NUM] = { 0 };int count[NUM] = { 0 };int size = 0;//模擬一個棧//存數據int temp;for (int i = 0; i < total; i++) {bool find = false;cin >> temp;for (int j = 0; j < size; j++) {//找相同if (arr[j] == temp) {//有相同find = true;count[j]++;break;}}//沒相同if (!find) {arr[size] = temp;count[size]++;size++;}}//找過半for (int i = 0; i < total; i++) {if (count[i] > total / 2) {cout << arr[i];}}system("pause"); } 超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的【PAT甲级 找到出现次数过半的数字】1054 The Dominant Color (20 分) C++的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【PAT甲级 删除字符串中的指定字符】1
- 下一篇: 【PAT甲级 一个字符数组是否被另一个包