• 找水王


    问题:

    •三人行设计了一个灌水论坛。信息学院的学生都喜欢在上面交流灌水,传说在论坛上有一个“水王”,他不但喜欢发帖,还会回复其他ID发的每个帖子。坊间风闻该“水王”发帖数目超过了帖子数目的一半。
    •如果你有一张当前论坛的帖子(包括回帖)列表,其中帖子的作者的ID也在其中,你能快速的找到这个传说中的水王吗?
    解题思想
    条件为  在一个数组str中,存在着水王、及其他用户。其中水王超过了50%。
    问题为  找到水王。
    问题简化为:在str数组中,存在着0-9数字,其中一个数字的个数超过了50%,找到这个数字。
    设计思想:1、先构建一个有若干个数字的数组,其中水王数字超过50%。2、将相邻数字匹配,相同则留下,不同则删去。3、遍历一遍即可找到水王数字。
     
     1 #include <iostream>
     2 #include <ctime>
     3 #include <string>
     4 using namespace std;
     5 void main()
     6 {
     7     //构造数组str
     8     srand((unsigned)time(0));
     9     string str = "";
    10     int Numstr = 1000;   //定义数组长度
    11     int num=0;    //记录水王数字个数
    12     int Shuiwang=rand()%10;  //随机定义水王数字
    13     while (Numstr!=0)
    14     {
    15         if(rand()%10 <= 5) //  加水王数字
    16         {
    17             str+=Shuiwang+48;
    18             num++;
    19         }
    20         else             //否则 加水王或者其他数字    
    21         {
    22             str+=rand()%10+48;
    23         }
    24         Numstr--;
    25     }
    26     cout<<"含水王的数组:"<<str<<endl;
    27     cout<<"水王数字的个数:"<<num<<endl;
    28     //相邻数字 匹配  相同则复制到str1中  
    29     string str1 = "";
    30     for(int i=0;i<str.length();i=i+2)
    31     {
    32         if(str[i]==str[i+1])
    33         {
    34             str1+=str[i];
    35         }
    36     }
    37     cout<<str1<<endl;     //此时str1中  绝大部分都是水王数字
    38     //其中任取若干 相同则是水王
    39     //此时取3个
    40     while(1)
    41     {
    42         int a=rand()%str1.length();
    43         int b=rand()%str1.length();
    44         int c=rand()%str1.length();
    45         if(str1[a]==str1[b]&&str1[a]==str1[c])
    46         {
    47             cout<<"水王是:"<<str1[a]<<endl;
    48             break;
    49         }
    50     }
    51 }

    结果截图:

    另外,因为水王个数已经超过了1/2,从中随机找三个,如果三个一样即为水王。原因有下:

    1、用户的个数会是个大数字,且每个用户的发帖个数与1/2的水王发帖个数相差甚多。

    2、论坛的发帖个数会是个大数字。

    3、假设有11个用户,水王占最少1/2,其他发帖平均为最多  1/20。三个都是水王的概率为1/2 * 1/2 * 1/2 为1/8。其他平均概率为 1/20 * 1/20  * 1/20 为1/8000。

    由上即可断定水王。

    代码如下:

     1 //条件为  在一个数组str中,存在着水王、及其他用户。其中水王超过了50%。
     2 //问题为  找到水王。
     3 //问题简化为:在str数组中,存在着0-9数字,其中一个数字的个数超过了50%,找到这个数字。
     4 //设计思想:1、先构建一个有若干个数字的数组,其中水王数字超过50%。2、将相邻数字匹配,相同则留下,不同则删去。3、遍历一遍即可找到水王数字。
     5 #include <iostream>
     6 #include <ctime>
     7 #include <string>
     8 using namespace std;
     9 string STR()
    10 {
    11     //构造数组str
    12     srand((unsigned)time(0));
    13     int f=1;
    14     while(f)
    15     {
    16         string str = "";
    17         int Numstr = 1000;   //定义数组长度
    18         int num=0;    //记录水王数字个数
    19         int Shuiwang=rand()%10;  //随机定义水王数字
    20         while (Numstr!=0)
    21         {
    22             if(rand()%2 == 1) //  加水王数字
    23             {
    24                 str+=Shuiwang+48;
    25                 num++;
    26             }
    27             else             //否则 加水王或者其他数字    
    28             {
    29                 str+=rand()%10+48;
    30             }
    31             Numstr--;
    32         }
    33         if(num>500)
    34         {
    35             cout<<"含水王的数组:"<<str<<endl;
    36             cout<<"水王数字的个数:"<<num<<endl;
    37             return str;
    38         }
    39     }
    40 }
    41 void main()
    42 {
    43     string str=STR();
    44                 //其中任取若干 相同则是水王
    45     //此时取3个
    46     while(1)
    47     {
    48         int a=rand()%str1.length();
    49         int b=rand()%str1.length();
    50         int c=rand()%str1.length();
    51         if(str1[a]==str1[b]&&str1[a]==str1[c])
    52         {
    53             cout<<"水王是:"<<str1[a]<<endl;
    54             break;
    55         }
    56     }*/
    57 
    58     while(1)
    59     {
    60         int a=rand()%str.length();
    61         int b=rand()%str.length();
    62         int c=rand()%str.length();
    63         if(str[a]==str[b]&&str[a]==str[c])
    64         {
    65             cout<<"水王是:"<<str[a]<<"   "<<str[b]<<" "<<str[c]<<endl;
    66             break;
    67         }
    68     }
    69 }

    结果如图:

  • 相关阅读:
    洛谷 P1896 [SCOI2005]互不侵犯(状压DP)
    POJ 3208 Apocalypse Someday(数位DP)
    HDU 3555 Bomb(数位DP)
    HDU 3652 B-number(数位DP)
    蜂鸣器版天空之城
    【洛谷习题】小木棍[数据加强版]
    【NOIP2009】靶形数独
    【洛谷习题】填涂颜色
    【NOIP2003】加分二叉树
    【NOIP2000】单词接龙
  • 原文地址:https://www.cnblogs.com/L-Damon-v/p/5505960.html
Copyright © 2020-2023  润新知