Classroom test - to find water Wang

I, entitled

  There is a big "Water King", he not only liked posts, but also respond to other ID issued by each post. The number of post "Water King" more than half of the total number of posts. If you have all the current posts on the forum (including replies) list, which is also the post author ID table, you can quickly find water this legendary king do?

Second, the design ideas

  "Water King" most afraid delete posts, because he's more than half of the total number of posts, then we can turn left from second on a comparison with the same ID, a different ID eliminated.

 
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 ( "water Wang" + 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 ( "water Wang" + C);} 
    
} 
    }



Screenshot experiment:

 

 

 

Guess you like

Origin www.cnblogs.com/ljm-zsy/p/10963701.html