• 找水王续


    一、题目与要求

         三人行设计了一个灌水论坛。信息学院的学生都喜欢在上面交流灌水,传说在论坛上有一个水王,他不但喜欢发帖,还会回复其他ID发的每个帖子。坊间风闻该水王发帖数目超过了帖子数目的一半。

         如果你有一张当前论坛的帖子(包括回帖)列表,其中帖子的作者的ID也在其中,你能快速的找到这个传说中的水王吗?(参考核心代码)

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

    二、设计思路

      三个发帖最多的人每人发的贴都超过了总帖子的1/4,每次减去四个不同的数,他仨发帖数还满足超过1/4

    三、代码

     1 //找水王续,信1301-1,李青 2016/5/26
     2 
     3 #include "stdafx.h"
     4 #include"iostream"
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     int key1=0;
    10     int key2=0;
    11     int key3=0;
    12     int king1;
    13     int king2;
    14     int king3;
    15     int i=0;
    16     int array[30]={4,1,2,1,2,2,5,1,4,1,3,3,1,3,4,3,1,2,3,4,1,2,3,2,2,3,1,2,3,1};
    17     for(i=0;i<30;i++)
    18     {
    19         if(key1==0)
    20         {
    21             king1=array[i];
    22             cout<<"Now the King1 of Navy is:"<<array[i]<<endl;
    23             key1=1;
    24         }
    25         else
    26         {
    27             if(king1==array[i])
    28             {
    29                 key1++;
    30             }
    31             else
    32             {
    33                 if(key2==0)
    34                 {
    35                     king2=array[i];
    36                     cout<<"Now the King2 of Navy is:"<<array[i]<<endl;
    37                     key2=1;
    38                 }
    39                 else
    40                 {
    41                     if(king2==array[i])
    42                     {
    43                         key2++;
    44                     }
    45                     else
    46                     {
    47                         if(key3==0)
    48                         {
    49                             king3=array[i];
    50                             cout<<"Now the King3 of Navy is:"<<array[i]<<endl;
    51                             key3=1;
    52                         }
    53                         else
    54                         {
    55                             if(king3==array[i])
    56                             {
    57                                 key3++;
    58                             }
    59                             else
    60                             {
    61                                 key1--;
    62                                 key2--;
    63                                 key3--;
    64                             }
    65                         }
    66                     }
    67                 }
    68             }
    69         }
    70     }
    71 
    72     cout<<"第一个水王是"<<king1<<"!"<<endl;
    73     cout<<"第二个水王是:"<<king2<<"!"<<endl;
    74     cout<<"第三个水王是:"<<king3<<"!"<<endl;
    75     cout<<"完成"<<endl;
    76     return 0;
    77 }

    四、截图

    五、心得体会

      这次找水王续只要对上一次的找水王做简单的举一反三即可。

  • 相关阅读:
    python脚本2_输入2个数比较大小后从小到大升序打印
    python脚本1_给一个半径求圆的面积和周长
    配置双机互信
    如何在 CentOS7 中安装 Nodejs
    Git 服务器搭建
    docker安装脚本
    CentOS7下安装Docker-Compose
    Linux 文件锁
    6 系统数据文件和信息
    bash脚本编程之二 字符串测试及for循环
  • 原文地址:https://www.cnblogs.com/liqing1/p/5612064.html
Copyright © 2020-2023  润新知