思路:水王的发帖数超过了总帖数的一半,水王的帖子要比其他所有人的帖子加起来还要多。比较相邻两个发帖ID,若不相同,把两个帖子全部删除;若相删除一个,在这之后再继续比较。到最后剩下的就是水王的id。
package test2;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ShuiWang {
public static void main(String[] args) throws IOException {
find();
}
public static void find() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("sw.txt"));
String s,s1 = null,s2 = null,shuiwang = null;
int i=1;
while((s = br.readLine()) != null) {
s1 = s;
if(s2 == null) {
s2 = br.readLine();
}
System.out.println("------------------------------------------");
System.out.println("第" + i++ + "次比较");
System.out.println("s1:" + s1);
System.out.println("s2:" + s2);
if(s2 != null) {
if(s1.compareTo(s2) != 0)
s2 = null;
else
shuiwang = s2;
}else {
shuiwang = s1;
}
System.out.println("记录id:" + shuiwang);
}
if(shuiwang == null)
shuiwang = s2;
System.out.println(" 水王id为:" + shuiwang);
br.close();
}
}