机试题目练习 - yiyixiaozhi/readingNotes GitHub Wiki

package top.bianxh.test;

import java.util.Scanner;

public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); String[] s = str.split(" "); //正则表达式实用性更强( str.split("\s+")) int length = s[s.length - 1].length(); System.out.println(length); } }

写出一个程序,接受一个由字母和数字组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。 import java.util.Scanner;

public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine().toLowerCase(); String charStr = sc.nextLine(); char[] chars = str.toCharArray(); int result = 0; for (char aChar : chars) { if (aChar == charStr.charAt(0)) { result ++; } } System.out.println(result); } }

明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤1000),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的学生的学号。然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。请你协助明明完成“去重”与“排序”的工作(同一个测试用例里可能会有多组数据,希望大家能正确处理)。

注:测试用例保证输入参数的正确性,答题者无需验证。测试用例不止一组。

当没有新的输入时,说明输入结束。

public class Main {

public static void main(String[] args) throws IOException {
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    String str;
    while ((str = bf.readLine()) != null) {
        int num = Integer.valueOf(str);
        TreeSet<Integer> result = new TreeSet<>();
        for (int i = 0; i < num; i++) {
            result.add(Integer.valueOf(bf.readLine()));
        }
        Iterator iterator = result.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

}

正则提取数字 public class Main { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); TreeSet set = new TreeSet(); String str; while ((str = bf.readLine()) != null) { Pattern pt = Pattern.compile("[0-9]+"); Matcher matcher = pt.matcher(str); int index = 0; while (matcher.find()) { if (index == 0) { } else { set.add(Integer.valueOf(matcher.group(0))); } index++; } Iterator iterator = set.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } } } }

题目描述 •连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组; •长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。 public class Main {

public static void main(String[] args) throws IOException {
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    String str;
    while ((str = bf.readLine()) != null) {
        int num = str.length() / 8;
        for (int index = 0; index < num; index++) {
            System.out.println(str.substring(index * 8, (index + 1) * 8));
        }
        int end = str.length() % 8;
        if (end > 0) {
            String endStr = str.substring(num * 8, str.length());
            for (int i = 0; i < 8; i++) {
                if (i >= str.length() % 8) {
                    endStr += "0";
                }
            }
            System.out.println(endStr);
        }
    }
}

}

写出一个程序,接受一个十六进制的数,输出该数值的十进制表示。 public class Main {

public static void main(String[] args) throws IOException {
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    String str;
    while ((str = bf.readLine()) != null) {
        String str1 = str.replaceAll("0x", "");
        int i = Integer.parseInt(str1, 16);
        System.out.println(i);
    }
}

}

功能:输入一个正整数,按照从小到大的顺序输出它的所有质因子(重复的也要列举)(如180的质因子为2 2 3 3 5 )

最后一个数后面也要有空格

public class Main {

public static void main(String[] args) throws IOException {
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    String str;
    while ((str = bf.readLine()) != null) {
        int num = Integer.valueOf(str);
        for (int index = 2; index < num;) {
            if (num % index == 0) {
                System.out.print(index + " ");
                num = num / index;
                index = 2;
            } else {
                index ++;
            }
        }
        System.out.print(num + " ");
    }
}

}

写出一个程序,接受一个正浮点数值,输出该数值的近似整数值。如果小数点后数值大于等于5,向上取整;小于5,则向下取整。 public class Main {

public static void main(String[] args) throws IOException {
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    String str;
    while ((str = bf.readLine()) != null) {
        String result = new BigDecimal(str).setScale(0, RoundingMode.HALF_UP).toString();
        System.out.println(result);
    }
}

}

数据表记录包含表索引和数值(int范围的整数),请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照key值升序进行输出。

public class Main {

public static void main(String[] args) throws IOException {
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    String str = bf.readLine();
    int num = Integer.valueOf(str);
    int index = 0;
    TreeMap<Long, Long> map = new TreeMap<>();
    while ((str = bf.readLine()) != null) {
        String[] s = str.split(" ");
        Long key = Long.valueOf(s[0]);
        Long value = Long.valueOf(s[1]);
        map.put(key, map.get(key) == null ? value : value + map.get(key));
        index ++;
        if (index >= num) {
            for (Map.Entry<Long, Long> entry : map.entrySet()) {
                System.out.println(entry.getKey() + " " + entry.getValue());
            }
        }
    }
}

}

题目描述 输入一个int型整数,按照从右向左的阅读顺序,返回一个不含重复数字的新的整数。

public class Main {

public static void main(String[] args) throws IOException {
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    String str;
    while ((str = bf.readLine()) != null) {
        LinkedList<Character> characters = new LinkedList<>();
        for (char ch : str.toCharArray()) {
            characters.add(ch);
        }
        Collections.reverse(characters);
        LinkedList<Character> revereAndTrims = new LinkedList<>();
        for (Character character : characters) {
            if (!revereAndTrims.contains(character)) {
                revereAndTrims.add(character);
            }
        }
        StringBuffer result = new StringBuffer();
        for (Character character : revereAndTrims) {
            result.append(character);
        }
        System.out.println(result);
    }
}

}

给定n个字符串,请对n个字符串按照字典序排列。 public class Main { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); int count = Integer.parseInt(bf.readLine()); String[] result = new String[count]; for (int i = 0; i < count; i++) result[i] = bf.readLine(); StringBuilder sb = new StringBuilder(); Arrays.sort(result); for (String w : result) sb.append(w).append('\n'); System.out.println(sb.toString()); } }

#输入一个int型的正整数,计算出该int型数据在内存中存储时1的个数。 char [] numChars = Integer.toBinaryString(num).toCharArray();

购物单: https://www.nowcoder.com/practice/f9c6f980eeec43ef85be20755ddbeaf4?tpId=37&tags=&title=&diffculty=0&judgeStatus=0&rp=1

请解析IP地址和对应的掩码,进行分类识别。要求按照A/B/C/D/E类地址归类,不合法的地址和掩码单独归类。 https://www.nowcoder.com/practice/de538edd6f7e4bc3a5689723a7435682?tpId=37&tags=&title=&diffculty=0&judgeStatus=0&rp=1

⚠️ **GitHub.com Fallback** ⚠️