• 个人作业11 找水王


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

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

    package water;
    
    import java.util.Scanner;
    import java.util.*;
    public class king {
        
        //输入
        static Scanner input = new Scanner(System.in);
        //生成固定个数id
        static String id[];
        public static void setId(int num) {
            id = new String[num];
            for(int i = 0;i<num;i++) {
                id[i] = input.next();
            }
        }
        //挑选水王
        public static void select() {
            int count = 0;
            for(int i = 0;i<id.length;i++) {
                for(int j = 0;j<id.length;j++) {
                    if(id[i].equals(id[j])) {
                        count++;
                    }
                }
                if(count >= id.length/2) {
                    System.out.println("水王"+id[i]);
                    break;
                }
                count = 0;
            }
        }
        public static void select1() {
            int count = 1;
            String back = id[0];
            for(int i = 1;i < id.length;i++) {
                if(back.equals(id[i])) {
                    count++;
                    back = id[i];
                }else {
                    count--;
                    if(count<=0) {
                        back = id[i]; //换下一个
                        count = 1;
                    }
                }
            }
            System.out.println("水王"+back);
            
        }
        
        public static void main(String args[]) {
            System.out.println("请输入id个数:");
            int num = 0;
            num = input.nextInt();
            setId(num);//select();
            select1();
        }
    }
  • 相关阅读:
    应急响应中find命令总结
    应急响应排查思路
    硬链接与软链接的区别
    Linux开机启动项总结
    android 開發常用網站
    epoll
    Qualcomm platform, the commonly used parameters of charger and battery in device tree file
    why not ovp protection ?
    Performance tuning
    Using adb over wifi
  • 原文地址:https://www.cnblogs.com/jbwen/p/11071772.html
Copyright © 2020-2023  润新知