uknowz 2016-04-15 12:04 采纳率: 0%
浏览 1386

微软4月笔试题第二题,为什么本地运行没错,提交是RE,实在想不出来,求救!!

 import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    int allowS = 0;//rules allow数组大小
    int denyS = 0;
    ArrayList<String> allow = new ArrayList<>();//用来存放动态变化的rules,整个类都要使用,则定义为实例变量
    ArrayList<String> deny = new ArrayList<>();

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int N = input.nextInt();
        int M = input.nextInt();
        Main webser = new Main();
        if(1<=N && M<=100000){
            webser.rules(N);
            String[] output = webser.request(M);
            for(int j=0;j<output.length;j++){
                System.out.println(output[j]);
            }
            //sca.close();//写完之后就要close,否则容易忘记
        }
        input.close();

    }
    /*
     * @param n为rules个数,此方法用来封装rules
     */
    public void rules(int n){

        @SuppressWarnings("resource")
        Scanner sca = new Scanner(System.in);
        for(int i=0;i<n;i++){
            String s = sca.nextLine();
            String[] rule = s.split(" ");//返回字符串数组
            if(rule[0].equals("allow")){
                allow.add(rule[1]);
                allowS++;
            }else if(rule[0].equals("deny")){
                deny.add(rule[1]);
                denyS++;
            }
        }
        //sca.close();如果把这个流关闭了,再调用request方法中的输入流就不行了
    }
    public String[] request(int m){
        //开始匹配,最优的应该是用正则表达式匹配,稍后优化
        String[] output1 = new String[m];//没问题
        String[] requ = new String[m];//创建请求数组
        @SuppressWarnings("resource")
        Scanner sca = new Scanner(System.in);
        for(int i=0;i<m;i++){       
            requ[i] = sca.nextLine();//输入请求ip数组
        }
        //开始遍历每一次request是否符合命中rules
        circle:
        for(int j=0;j<m;j++){
            for(int k=0;k<allow.size();k++){
                if(requ[j].equals(allow.get(k))){
                    output1[j] = "YES";//return "YES";
                    continue circle;
                }else if(allow.get(k).contains("/")){
                    String requBinary =IPtoInt.ipToint(requ[j]); 
                    String[] allowip = allow.get(k).split("/");
                    int mask =Integer.parseInt(allowip[1]);//取位数
                    String allowstr = IPtoInt.ipToint(allowip[0]);
                    //String allowMask = allowstr.substring(0,mask);//变成字符串后,取前几位
                    if(requBinary.substring(0, mask).equals(allowstr.substring(0,mask))){
                        output1[j] = "YES";//return "YES";
                        continue circle;

                    }
                }
            }
            for(int k=0;k<deny.size();k++){
                if(requ[j].equals(deny.get(k))){
                    output1[j] = "NO";//return "NO";
                    continue circle;
                }else if(deny.get(k).contains("/")){
                    String requBinary = IPtoInt.ipToint(requ[j]);
                    String[] denyip = deny.get(k).split("/");
                    int mask = Integer.parseInt(denyip[1]);
                    String denystr = IPtoInt.ipToint(denyip[0]);
                    String denyMask = denystr.substring(0,mask);
                    if((requBinary.substring(0,mask)).equals(denyMask)){//必须对比前mask位
                        output1[j] = "NO";//return "NO";
                        continue circle;
                    }
                }
            }
            if(output1[j].length()==0)
                output1[j] = "YES";

        }       
        //sca.close();
        return output1;//返回字符串数组      
    }
}
class IPtoInt {
    // 重写了方法,将ip地址转换为二进制。
    public static String ipToint(String strIP) {
        String[] ip = new String[4];
        String zero = "00000000";//用来补位的
        String[] ipstr = strIP.split("\\.");
        // 将每个.之间的字符串转换成整型
        ip[0] = Integer.toBinaryString(Integer.parseInt(ipstr[0]));
        ip[1] = Integer.toBinaryString(Integer.parseInt(ipstr[1]));
        ip[2] = Integer.toBinaryString(Integer.parseInt(ipstr[2]));
        ip[3] = Integer.toBinaryString(Integer.parseInt(ipstr[3]));
        for(int i=0;i<4;i++){
            if(ip[i].length()<8){
                ip[i] =zero.substring(0,8-ip[i].length()) + ip[i];//如果每一段ip不足八位,那么用字符串加法高位补0,不用循环,一次性补0,因为ip.length()一直会变  
            }
        }
        return ip[0]+ip[1]+ip[2]+ip[3];
    }
 } 


  • 写回答

1条回答 默认 最新

  • devmiao 2016-04-15 15:44
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧