水王问题

一、题目

  有一个大“水王”,他不仅喜欢发贴,还会回复其他ID发的每个帖子。该“水王”发帖数目超过了帖子总数的一半。如果有当前论坛上所有帖子(包括回帖)的列表,其中帖子作者的ID也在表中,你能快速找出这个传说中的水王吗?

二、设计思想

  “水王”的帖子数量超过总数的一半,那么我们可以从第一个依次与下一个比较,相同的ID留下,不同的ID消除,最后留下的就一定是水王。

         举个例子:1 1 2 3 1 2 1 3 1(一共9个1有五个满足超过一半的条件),1和1相同留下(1,1),与2比较,不同消除(1),与3比较,不同消除(),第五个1成为第一个数与2比较不同余(),第七个1成为第一个数与3比较不同余(),最后剩下的就是1,说明1是水王。

         代码是借鉴的同学的。

 
复制代码
package tiezi;

public class Bean {
int id;
char name;
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public char getName() {
    return name;
}
public void setName(char name) {
    this.name = name;
}
}
复制代码
复制代码
package tiezi;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import tiezi.DBUtil;

public class chaxun {
     public static void main(String[] args) {
        
    //int id=1;
    int q=0;
    int w=0;
    int p=0;
    
    String a="01a";
    String b="02a";
    String c="03a";
    for( int id=1;id<8;id++) {
        String sql = "select * from tiezi where id='"+ id +"'";
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;
        String name[]=new String[200];
        String str=null;
        int i=0;
        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            if (rs.next()) {
                str = rs.getString("name");
                //name[i]=str;
                //i++;
                
                i++;
            
            }
        
            //System.out.println(name[i]);
            
                
            if(str.equals(a)) {
                
                q++;
            }else if(str.equals(b)) {
                
                w++;
            }
            else if(str.equals(c)) {
                
                p++;
            
            
            
          
          //  System.out.println(w);
           // System.out.println(p);
            
            }
              //System.out.println(q);
            
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }
    
        
    
        
        }
    


    int max;
    if(q>w) {
        max=q;
        
    }else if(p>q) {
        max=p;
    }
    else {
        max=w;
    }
    if(q==max) {
    System.out.println("水王为"+c);}
    
}
    }
复制代码
复制代码
package tiezi;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import tiezi.DBUtil;

public class chaxun {
     public static void main(String[] args) {
        
    //int id=1;
    int q=0;
    int w=0;
    int p=0;
    
    String a="01a";
    String b="02a";
    String c="03a";
    for( int id=1;id<8;id++) {
        String sql = "select * from tiezi where id='"+ id +"'";
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;
        String name[]=new String[200];
        String str=null;
        int i=0;
        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            if (rs.next()) {
                str = rs.getString("name");
                //name[i]=str;
                //i++;
                
                i++;
            
            }
        
            //System.out.println(name[i]);
            
                
            if(str.equals(a)) {
                
                q++;
            }else if(str.equals(b)) {
                
                w++;
            }
            else if(str.equals(c)) {
                
                p++;
            
            
            
          
          //  System.out.println(w);
           // System.out.println(p);
            
            }
              //System.out.println(q);
            
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }
    
        
    
        
        }
    


    int max;
    if(q>w) {
        max=q;
        
    }else if(p>q) {
        max=p;
    }
    else {
        max=w;
    }
    if(q==max) {
    System.out.println("水王为"+c);}
    
}
    }



复制代码
posted @ 2019-06-03 17:21  呵呵刀呵呵  阅读(215)  评论(0)    收藏  举报