ACM模式笔记
洛谷,codeforces,Atcode,uoj 找ACM模式题目
参考:
简单版输入输出总结
有链表 有二叉树
java常用数据类型转换的总结
java常见Stream操作(int[] 转换成List需要以下几步 int[]封装成IntStream IntStream转Stream Stream转List)
int[][] intforest={ {1,2,3},{0,0,4},{7,6,5} };
List<List<Integer>> forest=Arrays.stream(intforest)
.map(o -> Arrays.stream(o).boxed().collect(Collectors.toList()))
.collect(Collectors.toList());
类名必须为Main
//阻塞式的
- next()--String,i
- inextInt()--int,
- nextDouble()--double
意思就是如果遇到空格,Tab,Enter,会跳过,直到获得相应类型相应的值!!!!!
//nextLine()
next() 和 nextLine()的区别
接收内容
next()
-
不接收空格,只接收有效字符,不能得到一个带空格的字符串
-
next在没有读到有效字符前会把空格、Tab、Enter自动去掉
nextLine()
-
可以接受空格,能得到带空格的字符串
-
返回的是Enter之前的一切字符
也是以回车符为结束,并且只是以回车符结束,并且会读取回车符。读入整行
数字:
1. 多组空格分割的两个整数 (无行数,组数限制)
//2 5
//13 20
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) { //hasNext()就是判断有没有再输入
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a + b);
}
}
}
2. 知道有多少个空格分开的整数 (无行数,组数限制)
4
1 3 5 6
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
3. 第一行为组数 (行数限制)
2 //组数
1 5
10 20
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
scanner in = new Scanner(System.in);
int num = in.nextInt();
for(int i = 0; i < num; i++) { // 注意 while 处理多个 case
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a + b);
}
}
}
4.第一行组数接第一个个数接空格分开的整数 (行数限制,每行有个数限制)
2 //2组
4 1 2 3 4 //4个元素
5 1 2 3 4 5 //5个元素
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();//先得到2
for (int i = 0; i < num; i++){
int n = in.nextInt();
while(n-->0){//它的意思是循环n次,除了最常用的 for (int i = 0; i < n; ++i) {...} 之外,还可以写 while (n-- > 0) {...} 和 while (--n >= 0) {...}。
//
}
}
}
}
5.不定长度的数组
==// 将字符串根据空格进行分隔 String[] strings = line.trim().split(" ");==
trim()的用处是去掉字符串开头结尾的空白字符串
public static void main(String[] args) throws IOException {
// 创建一个BufferedReader对象
Scanner sc=new Scanner(System.in);
// 读取第一行数据
String line = sc.nextLine();
// 将字符串根据空格进行分隔
String[] strings = line.trim().split(" ");
int[] arr=new int[strings.length];
// 分别将其中的每个数值读出
for(int i=0;i<strings.length;i++) {
arr[i]= Integer.parseInt(strings[i]);
}
}
5.先是一个数字代表数组长度,第二行是以逗号分隔的数组
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
String line = sc.next().toString();
// 将字符串根据空格进行分隔
String[] strings = line.trim().split(",");
int[] arr=new int[strings.length];
// 分别将其中的每个数值读出
for(int i=0;i<strings.length;i++) {
arr[i]= Integer.parseInt(strings[i]);
}
int[] ints = reverse(arr);
for (int i : ints) {
System.out.print(i);
}
}
字符串:
1.第一行个数第二行字符串
5
c d a bb e
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();//数字到字母要换行
while (in.hasNext()) {
String[] s = in.nextLine().split(" ");
for (int i = 0; i < s.length; i++) {
System.out.print(s[i] + " ");
}
}
}
}
2.第一行两个数 第二行字符串
6 4
AbcDEf
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int k=sc.nextInt();
sc.nextLine();
String s=sc.nextLine();
3.第一行是行数,后面是字符串
3
qwe
asd
zxc
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String[] arr = new String[n];
sc.nextLine();
for(int i=0;i<n;i++){
arr[i]=sc.nextLine();
}
for (String s : arr) {
System.out.println(s);
}
}
链表:
链表的输入输出
public static class ListNode {
// 结点的值
int val;
// 下一个结点
ListNode next;
// 节点的构造函数(无参)
public ListNode() {
}
// 节点的构造函数(有一个参数)
public ListNode(int val) {
this.val = val;
}
// 节点的构造函数(有两个参数)
public ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String[] split = sc.nextLine().split(" ");
int[] arr = new int[split.length];
for(int i=0;i<arr.length;i++){
arr[i]= Integer.parseInt(split[i]);
}
//将数组包装成链表
ListNode head=help(arr);
//输出
while (node!=null){
System.out.print(node.val);
if(node.next!=null){
System.out.print("->");
}
node=node.next;
}
}
public static ListNode help(int[] arr){
ListNode dumpy=new ListNode(-1);
ListNode cur=dumpy;
for(int i=0;i<arr.length;i++){
ListNode node = new ListNode(arr[i]);
cur.next=node;
cur=cur.next;
if(i==arr.length-1){
cur.next=null;
}
}
return dumpy.next;
核心代码模式转换成ACM模式
import java.util.*;
import java.lang.*;
public class Main {
public static void main(String[] args) {
//1.数据输入
Scanner in = new Scanner(System.in);
//读数字
int numLen = in.nextInt();
int[] numArr = new int[numLen];
int i = 0;
while(in.hasNextInt() && i < numLen){
numArr[i] = in.nextInt();
i++;
}
//读字符串
int strLen = in.nextInt();
in.nextLine(); //数字到字符串要换行
String[] strArr = new String[strLen];
//或者 strArr[] = in.nextLine().split(" ");
int j = 0;
while(in.hasNextLine() && j < strLen){
strArr[j] = in.nextLine();
j++;
}
//2. 处理
Solution solution = new Solution();
String result = solution.process(numArr, strArr);
//3. 输出
System.out.println(result);
//四舍五入输出小数
String str = String.format("%.2f",3.555);
System.out.println(str);
}
}
//下面类似 LeetCode 的核心代码模式
class Solution {
public String process(int[] nums, String[] strs) {
StringBuilder sb = new StringBuilder();
sb.append(Arrays.toString(nums));
sb.append(" && ");
sb.append(Arrays.toString(strs));
return sb.toString();
}
}
————————————————
版权声明:本文为CSDN博主「多氯环己烷」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/dlhjw1412/article/details/123821153
字节模拟笔试的一题 输入二维数组的
import java.util.*;
import java.lang.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int q= sc.nextInt();
sc.nextLine();
String[] map=new String[n];
for(int i=0;i<n;i++){
map[i]=sc.nextLine();
}
String operation=new String();
operation=sc.next();
int hang=0;
int lie=0;
for(int i=0;i<q;i++){
char step=operation.charAt(i);
if(step=='S'){
if(hang<n-1){
if(map[hang+1].charAt(lie)!='1'){
hang+=1;
}
}
}
if(step=='W'){
if((hang>0)){
if((map[hang-1].charAt(lie)!='1')){
hang-=1;}
}
}
if(step=='D'){
if((lie<m-1)){
if((map[hang].charAt(lie+1)!='1'))
{
lie+=1;}
}}
if(step=='A'){
if((lie>0)){
if(map[hang].charAt(lie-1)!='1'){
lie-=1;}
}
}
}
System.out.println((hang+1)+" "+(lie+1));
}
}
n, m, q = map(int, input().split())//
g = [input() for _ in range(n)]
op = input()
dirs = {
"W": (-1, 0),
"S": (1, 0),
"A": (0, -1),
"D": (0, 1),
}
i = j = 0
for o in op:
di, dj = dirs[o]
ii, jj = i + di, j + dj
if 0 <= ii < n and 0 <= jj < m and g[ii][jj] == "0":
i, j = ii, jj
print(i + 1, j + 1)
回溯算法例子(从核心代码模式转换到ACM模式)
力扣39
5 public class Problem39 {
6
7 public static void main(String[] args) {
8 int[] candidates = new int[]{2,3,6,7};
9 int target = 7;
10 Solution39 solution39 = new Solution39();
11 System.out.println(solution39.combinationSum(candidates, target));
12 }
13 }
14
15 class Solution39 {
16 private List<List<Integer>> res = new ArrayList<>();
17 public List<List<Integer>> combinationSum(int[] candidates, int target) {
18 trackBack(candidates, target, 0, new ArrayList<>());
19 return res;
20 }
21
22 private void trackBack(int[] candidates, int target, int idx, List<Integer> track) {
23 if (target < 0 || idx == candidates.length) {
24 return;
25 }
26 if (target == 0) {
27 res.add(new ArrayList<>(track));
28 return;
29 }
30 // 不选择当前值的分支
31 trackBack(candidates, target, idx + 1, track);
32
33 // 选择当前值的分支
34 int current = candidates[idx];
35 // 根据条件剪枝
36 if (target - current >= 0) {
37 track.add(current);
38 // 值可以重复使用,所以idx不增加,下次选择继续使用
39 trackBack(candidates, target - current, idx, track);
40 track.remove(track.size() - 1);
41 }
42 }
43 }
打印int[]
Arrays.toString(int[]);