源代码:
package classfour; public class test2 { static int num=0; static String t[]= {"001","002","003","001","001","001","001","002","003","004","001","001","001","002","003"}; static public void water() { for(int i=0;i<t.length;i+=2) { if(i<t.length-3) { if((t[0].equals(t[1]))!=true) { if(num>0) { t[0]=t[0]; t[1]=t[i+3]; num--; }else { t[0]=t[i+2]; t[1]=t[i+3]; } }else if((t[0].equals(t[1]))==true) { t[0]=t[0]; t[1]=t[i+3]; num++; i++; } } } System.out.println(t[1]); } public static void main(String[] args) { water(); } }
思路:
由于水王占发帖数的一半以上,所以将水王id数与其他相抵消,最后剩下水王的id,将第一个与第二个比较如果相同,将num++,如果不相同继续比较后两个,分情况判断。