课堂练习之“寻找水王”

思路:依次循环数组,如果次数为0,findid赋值为当前循环的值,如果次数为1,判断findid的值是否与当前循环的值相同,相同次数加1,否则减1,循环完留下的findid为“水王”。

package com.pre.one;
//20173522 李秦
public class Damot 
{

    public static void main(String[] args)
    {
        String []ids= { "aa","bb","aa","aa","cc","aa" ,"aa","ee","aa","hh","jj"};
        System.out.println(find(ids));
    }
    public static String find(String [] id)
    {
        if(id == null)
        {
            System.out.println("请输入id列表");
            return null;
        }
        int n=id.length;
        String findid=null;
        int cishu,i;
        for(i=cishu=0;i<n;i++)
        {
            if(cishu==0)
            {
                findid=id[i];
                cishu=1;
            }
            else
            {
                if(findid==id[i]) cishu++;
            
                else cishu--;
                    
            }
        }
        return findid;
    }
}

 

posted on 2019-05-30 17:49  不愧下学  阅读(131)  评论(0编辑  收藏  举报

导航