课堂测试——找水王

一、题目

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

二、设计思想

  “水王”最不怕删帖,因为他的帖子数量超过总数的一半,那么我们可以从第二个依次与上一个比较,相同的ID留下,不同的ID消除。

 
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-02 18:04  生活依旧  阅读(131)  评论(0编辑  收藏  举报