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

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

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

    package water;

    import java.util.Scanner;

    public class Water {

     public static void main(String[] args) {

            int[] a = null;//存储

                    Scanner sc = new Scanner(System.in);

            System.out.println("帖子总个数:");

            int sum = sc.nextInt();

            a = new int[sum];

            System.out.println("输入每个帖子的作者:");

            for(int i = 0;i < sum;i++)

            {

                a[i] = sc.nextInt();

            }

            sc.close();

            int n = 0;

            int nid = -1;

            int b=1;

            for(int i = 0;i < a.length - 1;i++)

            {

                if(n == 0)

                {

                    if(a[i] == a[i + 1])

                    {

                        n = 2;

                        nid = a[i];

                        b++;

                    }

                    else

                    {

                        i++;

                        if((i + 1) == a.length - 1)

                        {

                            nid = a[a.length - 1];

                        }

                    }

                }

                else

                {

                    if(nid == a[i])

                    {

                        n++;

                        b++;

                    }

                    else

                    {

                        n--;

                    }

                }

            }

            if (b>sum/2)

            {

            System.out.println("水王的ID : " + nid);

            }

            else 

            {

             System.out.println("无水王" );

            }

        }

    }

  • 相关阅读:
    android view生命周期
    ViewPager 滑动页(四)
    android 中如何获取camera当前状态
    Android LayoutInflater原理分析,带你一步步深入了解View(一)
    仿Twitter登陆移动背景效果
    Android应用性能优化之使用SQLiteStatement优化SQLite操作
    GreenDao官方文档翻译(下)
    高级IO
    linux信号
    LINUX进程
  • 原文地址:https://www.cnblogs.com/love-nan/p/11106787.html
Copyright © 2011-2022 走看看