zoukankan      html  css  js  c++  java
  • 课堂测试——找水王

    一、题目

      有一个大“水王”,他不仅喜欢发贴,还会回复其他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);}
        
    }
        }



    实验截图:

  • 相关阅读:
    C++ 模板实现败者树,进行多路归并
    CentOS 7 使用 Realtek 8188eu 上网 (解决 Required key not available)
    C++ Concurrency in Action 读书笔记
    Linux操作系统是如何工作的
    大型项目使用Automake/Autoconf完成编译配置
    Socket 用于进程间通信 --- UNIX Domain Socket
    在Linux中实现类似windows中获取配置文 件的函数GetProfileString
    Linux Shell 1
    YAML
    ubuntu虚拟机如何连接到windows上安装的Navicat
  • 原文地址:https://www.cnblogs.com/ljm-zsy/p/10963701.html
Copyright © 2011-2022 走看看