个人作业—找水王

news/2024/7/4 15:25:49

题目:三人行设计了一个灌水论坛。信息学院的学生都喜欢在上面交流灌水,传说在论坛上有一个“水王”,他不但喜欢发帖,还会回复其他ID发的每个帖子。坊间风闻该“水王”发帖数目超过了帖子数目的一半。
如果你有一张当前论坛的帖子(包括回帖)列表,其中帖子的作者的ID也在其中,你能快速的找到这个传说中的水王吗?

设计思路:

1、定义一个一维数组a[x],为作者id。

2、再设置一个循环,按照顺序来依次两两比较,如果作者id相同则保留,如果作者id不同则都删除。

3、最后剩余的作者id即为水王id

源程序:

 1 import java.util.Scanner;
 2 public class Select {
 3 
 4     public static void main(String[] args) {
 5         // TODO 自动生成的方法存根
 6         System.out.println("共有多少个帖子");
 7         Scanner input =new Scanner(System.in);
 8         int x=input.nextInt();
 9         String a[]=new String[x];
10         //发帖子情况
11         for(int i=0;i<x;i++)
12         {
13             System.out.println("请输入第"+(i+1)+"个帖子的作者id");
14             Scanner input1 =new Scanner(System.in);
15             a[i]=input1.nextLine();
16         }
17         //找水王
18         String s=a[0];//水王
19         int c=1;//c是水王最后净帖子数,如果c不是正数则不是水王
20         for(int i=0;i<x;i++)
21         {
22             if(!s.equals(a[i]))//相等继续
23             {
24                 c--;
25                 if(c<=0)
26                 {
27                     s=a[i+1];
28                     c=1;
29                     i++;
30                 }
31             }
32             else
33             {
34                 
35                 s=a[i];
36                 c++;
37             }
38         }
39         System.out.println("水王是"+s);
40     }
41 
42 }

 

程序截图:

结果分析:

在过程中需要设定参数来统计水王净帖子数,方便最终找出水王

转载于:https://www.cnblogs.com/muamu/p/5506069.html


http://www.niftyadmin.cn/n/4610385.html

相关文章

1059 Prime Factors (25分)【质因数分解】

1059 Prime Factors (25分) Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N p​1​​​k​1​​​​p​2​​​k​2​​​​⋯p​m​​​k​m​​​​. Input Specification: Each input file contain…

POJO百度百科

POJO(Plain Ordinary Java Object)简单的Java对象&#xff0c;实际就是普通JavaBeans&#xff0c;是为了避免和EJB混淆所创造的简称。 使用POJO名称是为了避免和EJB混淆起来, 而且简称比较直接. 其中有一些属性及其getter setter方法的类,没有业务逻辑&#xff0c;有时可以作为…

找出一个只出现一次的字符

一&#xff0c;问题描述 给定一个字符串&#xff0c;找出一个 其中只出现一次的字符 如"abaccdeff" 只出现一次的字符有 b d e 二&#xff0c;问题分析 ①字符集是个常量 &#xff0c;字符只有那么多。比如ASCII 一共256个&#xff0c;比如 字母表一共只有26个…

ios 开源代码

1、开源代码 http://www.oschina.net/iOS/codingList/365/ios-buttonhttp://www.devdiv.com/iOS_iPhone-iOS6%E6%96%B0%E7%89%B9%E5%BE%81%EF%BC%9A%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99%E5%92%8C%E7%A4%BA%E4%BE%8B%E6%B1%87%E6%80%BB-thread-127965-1-1.htmlhttp://code4app…

Log4J配置文件详解(转载一)

2019独角兽企业重金招聘Python工程师标准>>> 网站要发布了&#xff0c;为了跟踪一些日志&#xff0c;需要用到log4j&#xff0c;于是就研究了一下log4j的配置 先贴自己用的一个配置源文件 log4j.properties log4j.rootLoggerDEBUG, CONSOLE, FILE ## for console …

1063 Set Similarity (25分)【set】

1063 Set Similarity (25分) Given two sets of integers, the similarity of the sets is defined to be N​c​​/N​t​​100%, where N​c​​ is the number of distinct common numbers shared by the two sets, and N​t​​ is the total number of distinct numbers …

PostgreSQL 多国语言支持的实现

1、先了解&#xff1a;GNU gettext 2、以 pg_config 为例&#xff0c;打开 src/bin/pg_config/nls.mk # src/bin/pg_config/nls.mk CATALOG_NAME pg_config AVAIL_LANGUAGES cs de es fr it ja ko nb pl pt_BR ro ru sv ta tr zh_CN zh_TW GETTEXT_FILES pg_config.…

1060 Are They Equal (25分)【string】

1060 Are They Equal (25分) If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.12310​5​​ with simple chopping. Now given the number of significant digits on a machine …