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

      程序设计思想:由于水王发的帖子占到了全部帖子的一半以上,所以水王的帖子比其他人的帖子和要多,所以我设计的思路就是,从头开始遍历,从第一个人的ID开始,进行比对,如果相同就对数量进行增加,如果不同,就对数量减一,如果为零就重新选择水王,由于水王的帖子最多且大于一半,所以最后剩下的一定就是水王。

      源程序代码:

     1 package 灌水为王;
     2 
     3 public class shuiwang {
     4 
     5     public static void main(String[] args) {
     6         // TODO Auto-generated method stub
     7 
     8         int n=9;
     9         int b[]=new int[n+1];
    10         int a[]={0,4,3,4,5,4,2,4,3,4,4};
    11         int i;
    12         int shuiwang=0;
    13         int x=0;
    14         for(i=0;i<11;i++)
    15         {
    16             if(x==0)
    17             {
    18                 shuiwang=a[i];
    19                 x=1;
    20             }
    21             else
    22             {
    23                 if(shuiwang==a[i])
    24                 {
    25                     ++x;
    26                 }
    27                 else
    28                 {
    29                     --x;
    30                 }
    31             }
    32         }
    33                 System.out.println("水王是"+shuiwang);
    34             
    35         }
    36     }

      实现截图:

     

      个人总结:整体思路是老师讲的,我只是对思路程序化,并不能说明什么,我还是需要多多努力才能成为一名合格的工程师。

  • 相关阅读:
    cf D. Vessels
    cf C. Hamburgers
    zoj 3758 Singles' Day
    zoj 3777 Problem Arrangement
    zoj 3778 Talented Chef
    hdu 5087 Revenge of LIS II
    zoj 3785 What day is that day?
    zoj 3787 Access System
    判断给定图是否存在合法拓扑排序
    树-堆结构练习——合并果子之哈夫曼树
  • 原文地址:https://www.cnblogs.com/wangfengbin/p/6729589.html
Copyright © 2011-2022 走看看