当前位置:   article > 正文

ACM模式笔试相关Java API熟悉_java acm常用api

java acm常用api

第一题

题目描述

计算a+b

打开以下链接可以查看正确的代码

https://ac.nowcoder.com/acm/contest/5657#question
  • 1

数据范围: 数据组数 1≤t≤100 1 \le t \le 100 \ 1≤t≤100 , 数据大小满足 1≤n≤1000 1 \le n \le 1000 \ 1≤n≤1000

输入描述:

输入包括两个正整数a,b(1 <= a, b <= 1000),输入数据包括多组。
  • 1

输出描述:

输出a+b的结果            
  • 1

输入

1 5
10 20
  • 1
  • 2

输出

6
30
  • 1
  • 2

代码实现

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int a = in.nextInt();
            int b = in.nextInt();
            System.out.println(a + b);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

第二题

输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据有多组, 如果输入为0 0则结束输入

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int a = in.nextInt();
            int b = in.nextInt();
            if (a == 0 && b == 0) {
                break;
            } 
            System.out.println(a + b);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

第三题

输入数据包括多组。
每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100), n为0的时候结束输入。
接下来n个正整数,即需要求和的每个正整数。

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            int sum = 0;
            int count = in.nextInt();
            if (count == 0) {
                break;
            }
            for (int i = 0; i < count; i++) {
                sum += in.nextInt();
            }
            System.out.println(sum);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

第四题

输入数据有多组, 每行表示一组输入数据。

每行不定有n个整数,空格隔开。(1 <= n <= 100)。

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String[] temp = in.nextLine().split(" ");
            int sum = 0;
            for (String str : temp) {
                sum += Integer.parseInt(str);
            }
            System.out.println(sum);
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

第五题

输入有两行,第一行n

第二行是n个字符串,字符串之间用空格隔开
  • 1
  • 2
  • 3

输入

5
c d a bb e
  • 1
  • 2

输出

a bb c d e
  • 1

代码实现

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 直接读掉就行
        in.nextLine();
        while (in.hasNextLine()) {
            String[] temp = in.nextLine().split(" ");
            Arrays.sort(temp);
            System.out.println(String.join(" ", temp));
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Arrays.toString()

public class Main {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6};
        String s = Arrays.toString(arr);
        System.out.println(s);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

输出结果:

[1, 2, 3, 4, 5, 6]
  • 1

字符串处理

比较项next( )nextLine( )
说明只能读取到空格之前的字符串可以读取空格的字符串
比如“你好 java”“你好”“你好 java”
使用前判断in.hasNext()in.hasNextLine()

输出格式化相关

  • 输出有两种方式:String str = String.format()或者System.out.printf()
  • 向上取整:Math.ceil(1.01),向下取整:Math.floor(1.01)
标志说明示例输出
.后接保留多少位小数(四舍五入)(“%.2f”,3.555)3.56
+为正数或者负数添加符号(“%+d”,15)+15
左对齐(“%-5d”,15)|15 |
0数字前面补0(“%04d”, 99)0099

建立二叉树

核心要点:

  • 一般来说是输入一个数组,然后通过数组来构建一颗二叉树。
  • 二叉树如何存储在数组里面,那么存储方式是规定的,所以可以唯一的确定一棵二叉树。
  • 如果父亲节点的下标是i,那么父亲节点的左孩子节点下标是2 * i + 1,右孩子节点下标是2 * i + 2
import java.util.*;

public class Main {
    // 节点类
    static class TreeNode {
        // 节点值
        int val;
        // 左节点
        TreeNode left;
        // 右节点
        TreeNode right;

        // 节点的构造函数(默认左右节点都为null)
        public TreeNode(int x) {
            this.val = x;
            this.left = null;
            this.right = null;
        }

        // 根据数组构建二叉树
        public TreeNode constructBinaryTree(final int[] arr) {
            // 构建和原数组相同的树节点列表
            List<TreeNode> treeNodeList = arr.length > 0 ? new ArrayList<>(arr.length) : null;
            TreeNode root = null;
            // 把输入数值数组,先转化为二叉树节点列表
            for (int i = 0; i < arr.length; i++) {
                TreeNode node = null;
                if (arr[i] != -1) { // 用-1表示null
                    node = new TreeNode(arr[i]);
                }
                treeNodeList.add(node);
                if (i == 0) {
                    root = node;
                }
            }
            // 遍历一遍,根据规则左右孩子赋值就可以了
            // 注意这里 结束规则是 i * 2 + 1 < arr.length, 避免空指针
            // 为什么结束规则不能是i * 2 + 2 < arr.length呢?
            // 如果i * 2 + 2 < arr.length是结束条件
            // 那么i * 2 + 1这个符合条件的节点就被忽略掉了
            // 例如[2, 7, 9, -1, 1, 9, 6, -1, -1, 10]这样的一个二叉树,最后的10就会被忽略掉
            for (int i = 0; i * 2 + 1 < arr.length; i++) {
                TreeNode node = treeNodeList.get(i);
                if (node != null) {
                    // 线性存储转链式存储关键逻辑
                    node.left = treeNodeList.get(2 * i + 1);
                    // 再次判断,不忽略任何一个节点
                    if (i * 2 + 2 < arr.length) {
                        node.right = treeNodeList.get(2 * i + 2);
                    }
                }
            }
            return root;
        }
    }

    public static void main(String[] args) {

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

常见的字符串处理函数

连接concat()

public class StringTest {
    public static void main(String[] args) {
        String str1 = "you";
        String str2 = "welcome";
        System.out.println(str1.concat(str2));
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

结果如下:

youwelcome
  • 1

提取substring()

public class StringTest {
    public static void main(String[] args) {
        String str="we are students and he is a techer";
        System.out.println(str.substring(2, 10));
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

结果如下:

 are stu
  • 1

字符串的一些检索查找字符的函数

public class StringTest {
    public static void main(String[] args) {
        String str = "我们一起数到6吧!";
        System.out.println(str.indexOf("一"));
        // Tests if this string starts with the specified prefix.
        System.out.println(str.startsWith("我"));
        // Tests if this string ends with the specified suffix.
        System.out.println(str.endsWith("!"));
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

结果:

2
true
true
  • 1
  • 2
  • 3
  • str.indexOf(‘,’),表示在整个str字符串中检索!
  • int n3 = str.indexOf(‘,’,n1);表示从n1开始检索!

把字符串转换为数字

public class StringTest {
    public static void main(String[] args) {
        String s = "123.456 ";
        double d = Double.parseDouble(s);
        float f = Float.parseFloat(s);
        System.out.println(d);
        System.out.println(f);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

转义字符

String []s = str.split("\\.");
  • 1

注意这里要对小数点转义,否则得不到正确答案。

StringBuffer

StringBuffer支持以下几种操作函数:append()、insert()、replace()、delete()、reserve()等等。

  • public StringBuffer append(String s):将指定的字符串追加到此字符序列。
  • StringBuffer insert(int index,String str):在index前插入字符串。
  • replace(int start, int end, String str):使用给定 String 中的字符替换此序列的子字符串中的字符。
  • public delete(int start, int end):移除此序列的子字符串中的字符。(从start开始,包含start,到end结束,不包含end)
  • public StringBuffer reverse():将此字符序列用其反转形式取代。

Arrays方法

public class StringTest {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 2, 3);
        System.out.println(list);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
public class StringTest {
    public static void main(String[] args) {
        Integer[] data = {1, 2, 3, 4};
        Arrays.fill(data, 9);
        System.out.println(Arrays.toString(data));
        // [9, 9, 9, 9]
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

用指定元素填充数组,从起始位置到结束位置,取头不取尾 (会替换掉数组中原来的元素)

Integer[] data = {1, 2, 3, 4};
Arrays.fill(data, 0, 2, 9);
System.out.println(Arrays.toString(data)); // [9, 9, 3, 4]
  • 1
  • 2
  • 3
public class StringTest {
    public static void main(String[] args) {
        String[] data = {"1", "2", "3", "2"};
        System.out.println(Arrays.toString(data)); // [1, 4, 3, 2]
        Arrays.sort(data);
        System.out.println(Arrays.toString(data)); // [1, 2, 3, 4]
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
public class StringTest {
    public static void main(String[] args) {
        String[] data = {"1", "2", "3", "2"};
        System.out.println(Arrays.toString(data)); // [1, 4, 3, 2]
        // 实现降序排序,返回-1放左边,1放在右边,0保持不变
        Arrays.sort(data, (str1, str2) -> {
            if (str1.compareTo(str2) > 0) {
                return -1;
            } else {
                return 1;
            }
        });
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
String[] data = {"1", "4", "3", "2"};
System.out.println(Arrays.toString(data)); // [1, 4, 3, 2]
// 对下标[0, 3)的元素进行降序排序,即对1,4,3进行降序排序,2保持不变
Arrays.sort(data, 0, 3, (str1, str2) -> {
	if (str1.compareTo(str2) > 0) {
		return -1;
	} else {
		return 1;
	}
});
System.out.println(Arrays.toString(data)); // [4, 3, 1, 2]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/418984
推荐阅读
相关标签
  

闽ICP备14008679号