zoukankan      html  css  js  c++  java
  • 网易校招机试题(2016.08.02)

      此为,前天网易机试前二道编程题的Java实现,当时时间限制,有的没有理清楚头绪,现补充实现。第三题打土豪分田地还是没有思路,求指教~

    1. 子序列的问题

    输入两个字符串

    abcd

    bd

    输出是否包含

    Yes

     1 package NetEasy;
     2 
     3 import java.util.Scanner;
     4 
     5 public class SubStr {
     6 
     7     public static void main(String[] args) {
     8         Scanner in = new Scanner(System.in);
     9         while(in.hasNext()){
    10             String str = in.next();
    11             in.nextLine();
    12             String subStr = in.nextLine();
    13             if(subStr.length() == 0)
    14                 System.out.println("Yes");
    15             else{
    16                 int start = 0;
    17                 boolean result = false;
    18                 for(int i = 0; i < subStr.length(); i++)
    19                     for(int j = start; j < str.length();j++){
    20                         if(subStr.charAt(i) == str.charAt(j)){
    21                             start = j;
    22                             if(i == subStr.length()-1)
    23                                 result =true;
    24                             break;//这里应该用break而不是continue!!!!!!跳出这一层的循环
    25                         }
    26                             
    27                     }
    28                 if(result)
    29                     System.out.println("Yes");
    30                 else
    31                     System.out.println("No");
    32             }
    33                 
    34             
    35         }
    36 
    37     }
    38 
    39 }

    2. 数字全排列的问题

    输入

    n k

    分别表示有1。。。n个自然数的序列,其中有k个正确的序列

    再输入n个数,0表示模糊了

    输出

    有多少种可能

    例如输入

    5 5

    4 0 0 1 0

    输出

    1

    package NetEasy;
    
    import java.util.*;
    
    public class Permutation {
        private List<String> permutation = new LinkedList<>(); //用来存储所有的全排列
        private int count = 0;//用来对排列总数计数
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            while(in.hasNext()){
                List<Integer> allNum = new LinkedList<>();
                int n = in.nextInt();
                int k = in.nextInt();
                int[] number = new int[n];
                int noZero = 0;//统计明确数字的个数
                for(int i = 0; i<n; i++){
                    number[i] = in.nextInt();
                    if(number[i] != 0)
                        noZero++;
                    allNum.add(i+1);
                }
                Permutation use = new Permutation();//实例化一个Permutation类
                int fits = 0;
                use.listAll(allNum,"");
    //            System.out.println(use.permutation.size());
    //            System.out.println(use.permutation.get(0));
    //            System.out.println(use.permutation.get(1));
    //            System.out.println(use.count);//listAll函数也没有问题
    //            System.out.println(compute("42315"));//compute函数没有问题
                for(int i = 0; i < use.permutation.size(); i++){
                    String temp = use.permutation.get(i);
                    if(compute(temp) == k){
                        int equalNum = 0;
                        for(int j = 0; j < n; j++){
                            if(number[j] == temp.charAt(j)-'0')
                                equalNum++;
                        }
                        if(noZero == equalNum)
                            fits++;
                    }
                    
                }
                System.out.println(fits);
                
            }
    
        }
        
        /**
         * n个数全排列,并存储在permutation列表中
         * @param candidate
         * @param perfix
         */
        private void listAll(List<Integer> candidate, String perfix) {
            if(candidate.isEmpty()){
                this.permutation.add(perfix);
                this.count++;
            }
            else{
                for(int i =0; i<candidate.size(); i++){
                    List<Integer> temp = new LinkedList<>(candidate);
                    String str = perfix + temp.remove(i);
                    listAll(temp, str);
                }
            }
            
        }
        
        
        /**
         * 计算字符正确的序数
         * @param a
         * @return
         */
        private static int compute(String a){
            int number = 0;
            for(int i = 0; i < a.length() - 1; i++){
                for(int j = i + 1; j<a.length(); j++){
                    if(a.charAt(i) < a.charAt(j))
                        number++;
                }
            }
            return number;
        }
    
    }

      方法比较笨拙,希望有更好的解法。

    3. 打土豪,分田地

    没什么思路,跪求指教~

  • 相关阅读:
    Oracle不常用SQL
    C# xml 读xml、写xml、Xpath、Xml to Linq、xml添加节点 xml修改节点
    Oracle常见错误:ORA-06550、ORA-00911、ORA-02085
    IIS设置允许跨域
    npm和yarn 切换为国内镜像(淘宝镜像)
    Wordpress 所有 hook 钩子
    【C#】WPF多线程登录需求中报错 “调用线程无法访问对象,因为另一个线程拥有该对象“
    【C#】 WPF 中WebBrowser拖动来移动窗口,改变窗口位置
    【Java】Hibernate一级缓存测试分析
    javac编译单文件、多文件引入jar包、-cp解决无法加载主类问题
  • 原文地址:https://www.cnblogs.com/focusonepoint/p/5736981.html
Copyright © 2011-2022 走看看