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

    1、题目要求:

      在一个论坛中,有三个用户,他们的发帖量在总帖数中占了1/4 以上,并且在每一个帖子下都有他们的回复,人们管他叫小水王 

      用最简单、快速的方法找到这三个小水王

    2、实现思路:

         本次试验的思路相对上一次试验比较容易想到,虽然有点不太好实现,但是总体来讲还是比较简单的。在本次试验中我们是设置了两个都含有3个元素的数组,shuiwang【3】用来表示要采集的id号,count【3】用来标记三个小水王,当在数组中遇到与shuiwang中相同的id号,count数组中相对应的数值相加1,如果都不同,则都减1,如果有count值减为0,重新赋值,依次寻找,直到得到最终结果。

     1   #include<iostream>
     2   using namespace std;
     3   
     4   int main()
     5   {
     6       int length,i,id;
     7       int shuiwang [3]={0}; 
     8       int count[3] ={0}; //标记三个小水王
     9       int ID[10000];
    10      int water[10000];
    11      cout << "请输入帖子的总个数:";//定义帖子的长度,即数组的个数
    12      cin >> length;
    13      
    14       if (length <=0)
    15       {
    16            if(length==0)
    17             {
    18                 cout<<"没有水王,呜呜呜"<<endl;
    19                 return 0;
    20             }
    21         cout << "输入的帖子数量不正确,请重新输入" << endl;
    22         cin >> length;
    23         while (length < 0)
    24         {
    25             cout << "输入的帖子数量不正确,请重新输入" << endl;
    26             cin >> length;
    27         }
    28       }
    29          
    30     
    31      cout << "请依次输入每个帖子的发布者的ID:" << endl;//输入每个帖子的Id
    32      for (i = 0; i < length; i++)
    33      {
    34          cin >> id;
    35          ID[i] = id;
    36      }
    37     for(int i=0;i<length;i++)//查找三个小水王
    38      { 
    39              if(ID[i]==shuiwang[0])  
    40              {  
    41                   count[0]++;  
    42              }  
    43              else if(ID[i]==shuiwang[1])  
    44              {  
    45                   count[1]++;  
    46              }  
    47              else if(ID[i]==shuiwang[2])  
    48              {  
    49                   count[2]++;  
    50              }  
    51              else if(count[0]==0)  
    52              {  
    53                   count[0]=1;  
    54                 shuiwang[0]=ID[i];  
    55              }  
    56              else if(count[1]==0)  
    57              {  
    58                   count[1]=1;  
    59                  shuiwang[1]=ID[i];  
    60              }  
    61              else if(count[2]==0)  
    62              {  
    63                   count[2]=1;  
    64                   shuiwang[2]=ID[i];  
    65              }  
    66              else  
    67              {  
    68                   count[0]--;  
    69                   count[1]--;  
    70                   count[2]--;  
    71               }  
    72      }
    73      
    74      cout<<"三个小水王的ID分别是:"<<endl;
    75      for(int i=0;i<3;i++)
    76       {
    77           cout<<shuiwang[i]<<" ";
    78       }
    79     cout<<endl;
    80     return 0;
    81  }

    实验结果:

  • 相关阅读:
    P4932 浏览器 题解
    P1627 [CQOI2009]中位数 题解
    P4626 一道水题 II 题解
    P1439 【模板】最长公共子序列 题解
    P2324 [SCOI2005]骑士精神 题解
    P1784 数独 题解
    浅谈 Dancing Links X 算法
    P5905 【模板】Johnson 全源最短路 题解
    线性预处理阶乘,逆元和组合数
    需要支持多种操作的线段树该如何确定运算顺序?
  • 原文地址:https://www.cnblogs.com/apan008/p/5553425.html
Copyright © 2011-2022 走看看