zoukankan      html  css  js  c++  java
  • 软件工程个人作业06

    一、程序题目:

    三人行设计了一个灌水论坛。信息学院的学生都喜欢在上面交流灌水,传说在论坛上有一个“水王”,他不但喜欢发帖,还会回复其他ID发的每个帖子。坊间风闻该“水王”发帖数目超过了帖子数目的一半。随着论坛的发展,管理员发现水王没有了,但是统计结果表明,有三个发帖很多的ID。据统计他们的发帖数量超过了1/4,如果你有一张当前论坛的帖子(包括回帖)列表,其中帖子的作者的ID也在其中,你能从发帖列表中快速找到他们吗?

    二、设计思想

    从上一个寻找水王的思路来看,只需将计数器改为三个,利用两个一维数组分别表示水王和计数器,在一次循环作者ID过程中,三个临时水王分别与当前ID比较,得出三个水王。

    三、代码实现

     

    import java.util.InputMismatchException;
    import java.util.Scanner;
    
    public class Finding_2 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
        
            for(;;)                             //按照用户需求无限循环
            {
                int judge=0;
                Scanner in=new Scanner(System.in);
                int n; 
                System.out.println("请输入帖子的个数:");
                n=in.nextInt();
             
                System.out.println("请输入ID(序号为正整数):");
                int ID[]=new int [n];
                try                              //捕捉输入错误
                {
                    for(int i=0;i<n;i++)
                    {
                        ID[i]=in.nextInt();
                    }
                 }
                catch(InputMismatchException e)
                {
                    System.out.println("输入不合法!请输入整数!");
                    judge=1;
                }
                int shuiwang[]=new int [3];
                int temp[]=new int[3];                    //三个计数器,三个水王            
                temp[0]=temp[1]=temp[2]=0;
                shuiwang[0]=shuiwang[1]=shuiwang[2]=-1;   //初始化计数器和三个水王ID             
                for(int i=0;i<n;i++)
                {                                     
                    if(temp[0]==0)                        //如果temp为0,则水王换值为当前ID,计数器各加1
                    {
                        temp[0]++;
                        shuiwang[0]=ID[i];
                    }
                    else if(temp[1]==0)
                    {
                        temp[1]++;
                        shuiwang[1]=ID[i];
                    }
                    else if(temp[2]==0)
                    {
                        temp[2]++;
                        shuiwang[2]=ID[i];
                    }
                    else if(ID[i]==shuiwang[0])        //当此时某水王与下一个ID相同时,计数器temp+1
                    {
                        temp[0]++;
                    }
                    else if(ID[i]==shuiwang[1])
                    {
                        temp[1]++;
                    }
                    else if(ID[i]==shuiwang[2])
                    {
                        temp[2]++;
                    }
                    else                              //如果当前水王ID与当前ID都不同,则三个计数器都减一,直到temp为0
                    {
                        temp[0]--;
                        temp[1]--;
                        temp[2]--;
                    }
                 }
                if(judge!=1)                          //如果没有发生输入错误,则找水王
                {
                    System.out.println("水王是:");
                    for(int i=0;i<3;i++)
                        System.out.println(shuiwang[i]);
                }
                System.out.println("继续寻找水王请按任意键,退出请按q:");
                String s=in.next();
                if(s.equals("q"))
                    System.exit(0);
                else
                    continue;    
                in.close();
            }
        }
    }

    四、实现截图

    五、个人总结

    在前一个程序上的提高,要一步一步分析,锻炼编程能力。

  • 相关阅读:
    java相关
    cmd批处理命令及powershell
    火狐浏览器调试模式
    windows 配置java环境变量
    Acwing-279-自然数拆分(背包)
    Acwing-278-数字组合(背包)
    Acwing-277-饼干(DP)
    Acwing-274-移动服务(DP)
    Acwing-275-传纸条(DP)
    Acwing-121-赶牛入圈(二分, 二维前缀和,离散化)
  • 原文地址:https://www.cnblogs.com/love528/p/5535741.html
Copyright © 2011-2022 走看看