zoukankan      html  css  js  c++  java
  • 寻找水王2

    一,题目说明

      随着论坛的发展,管理员发现水王没有了,但是统计结果表明,有三个发帖很多的ID。据统计他们的发帖数量超过了1/4,设计程序从发帖列表中快速找到他们。

    二,设计思路 

        由于发帖数超过四分之一,所以每次消除四个不同的id,用count来记录判断,最后剩下的就是那三个人。

    三,程序代码

    复制代码
    #include <iostream>
     
    using namespace std;
     
    int candidate[3];
    int count[3] = {0};
     
    int input[100];
    int num = 0;
     
    int main()
    {
        cout<<"please input"<<endl;
        int t;
        while(cin>>t)
        {
            if (t == -1)
                break;
            input[num++] = t;
        }
     
        bool flag = false;
     
        for (int i = 0;i < num;i++)
        {
            flag = false;
            for (int j = 0;j < 3;j++)
            {
                if (count[j] == 0)
                {
                    continue;
                }
                if (candidate[j] == input[i])
                {
                    count[j]++;
                    flag = true;
                }
            }
     
            if (flag == true)
            {
                continue;
            }
     
            for (int j = 0;j < 3;j++)
            {
                if (count[j] == 0)
                {
                    candidate[j] = input[i];
                    count[j]++;
                    flag = true;
                    break;
                }
            }
     
            if (flag == true)
            {
                continue;
            }
     
            for (int j = 0;j < 3;j++)
            {
                count[j]--;
            }
     
        }
     
        cout<<count[0]<<" "<<count[1]<<" "<<count[2]<<endl;
        cout<<candidate[0]<<" "<<candidate[1]<<" "<<candidate[2]<<endl;
    }
    复制代码

    程序截图:

  • 相关阅读:
    angular2
    angular1
    JavaScript随笔1
    鼠标样式
    清除浮动
    css-父标签中的子标签默认位置
    [Leetcode] Decode Ways
    [Java] 利用LinkedHashMap来实现LRU Cache
    LinkedHashMap和HashMap的比较使用(转)
    [Java] java.util.Arrays 中使用的 sort 采用的算法 (转)
  • 原文地址:https://www.cnblogs.com/a1264393659/p/6961938.html
Copyright © 2011-2022 走看看