找水王

题目要求:

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

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

个人分析:没有很好地思路,根据同学的就是找最多的信息,然后进行把短的删除,之后就会有,不过我也很迷茫

源代码:

这些源代码有借鉴,有原创。

package water_King;

    public class Water_King 
    {
        
        public static void main(String[] args) 
        {
            int a[]={3,3,3,5,3,3,2,3,2,3,3,3,0,9,0,8,4};
            int b[]={3,5,3,2,4,3,6,8,3,6,3,2,3,6,2,8,3,6,2,3,4,6,2,2,6};
            int candidate[] = new int[3];
       // Find(a,a.length);
            Find1(b,b.length,candidate);//1/3水王----可推广
            for (int i = 0; i < candidate.length; i++) {
                System.out.println(candidate[i]);
        }
    }
    
//     static int Find(int []ID,int N)
//    {
//            int candidate;
//            int nTimes=0,i;
//            for(i=0;i<N;i++)
//            {
//                    if(candidate==ID[i])
//                    nTimes++;
//                    else if(nTimes==0)
//                    {
//                            candidate=ID[i];
//                            nTimes=1;
//                    }
//                    else
//                            nTimes--;
//            }
//            return candidate;
//    }
     static int[] Find1(int []ID,int N,int candidate[])
    {
            int nTimes[] = new int[3],i;
            nTimes[0]=0;
            nTimes[1]=0;
            nTimes[2]=0;
            candidate[0]=0;
            candidate[1]=0;
            candidate[2]=0;
            for(i=0;i<N;i++)
            {
                    if(candidate[0]==ID[i])
                            nTimes[0]++;
                    else if(candidate[1]==ID[i])
                            nTimes[1]++;
                    else if(candidate[2]==ID[i])
                            nTimes[2]++;
                    else if(nTimes[0]==0)
                    {
                            candidate[0]=ID[i];
                            nTimes[0]=1;
                    }
                    else if(nTimes[1]==0)
                    {
                            candidate[1]=ID[i];
                            nTimes[1]=1;
                    }
                    else if(nTimes[2]==0)
                    {
                            candidate[2]=ID[i];
                            nTimes[2]=1;
                    }
                    else
                    {
                            nTimes[0]--;
                            nTimes[1]--;
                            nTimes[2]--;
                    }
            }
            
            
            
            return candidate;
    }
 
 
    
}

实验截图:

原文地址:https://www.cnblogs.com/kmxbf2292/p/11070293.html