查找水王程序

设计思路:建立一个存储ID的数组,建立一个查找的函数,在查找的函数中,每次删除两个不一样的ID,最后剩下的就是水王。

package demo;


public class show {
    public static void main(String[] args) {
    String[] ID = {"a","b","a","c","a","b","a","d"};
    System.out.println("水王的id是 "+Search(ID));

}

public static String Search(String[] id)
{
    if(id == null)
    {
        System.out.println("请输入id列表");
        return null;
    }            
   
    String SW= null;
    int t=0,i;
    for(i  = 0;i<id.length;i++){
        if(t == 0)
        {
           SW = id[i];
           t = 1;
        }
        else
        {
            if(SW == id[i])
                t ++;
            else 
                t --;
        }
    }
    return SW;
    
    
}
}

 实验截图:

 

posted @ 2016-05-20 16:47  牙吃多了糖疼  阅读(125)  评论(0编辑  收藏  举报