笔试都是acm模式,需要自己处理输入输出
首先注意类名都是规定好的,一般是Main类,最终直接new一个对象输出结果
其次有一些常用函数:
为了导入包,需要import java.util.*;
Scanner sc=new Scanner(System.in);//输入 int n=sc.nextInt();//一个整数 String str=sc.next();//一个字符串 String str1=sc.nextLine().split(" ")[0];//获取一行并且分割
然后会经常碰到一些转换,new一些数组或者二维数组
Integer.parseInt(str.split("->")[0]);//字符串转int型 String[][] table=new String[n][2];//二维数组
总的来说,只要是空格分隔的,总能直接next或者nextint
逗号或者其他分割的,需要split
举几个题目的例子:
题1:
Scanner sc=new Scanner(System.in); int n=sc.nextInt(); String[][] table=new String[n][2]; for(int i=0;i<n;i++) { table[i][0]=sc.next(); table[i][1]=sc.next(); } Solution1 solution1=new Solution1();
第二题:
Scanner sc=new Scanner(System.in); String[] timeStr=sc.nextLine().split(","); //存第一行数据 List<Integer> list=new ArrayList<>(); for(int i=0;i<timeStr.length;i++) { list.add(Integer.parseInt(timeStr[i])); } //存第二行数据 String[] relyStr=sc.nextLine().split(","); List<Integer[]> table=new ArrayList<>(); for(String str:relyStr) { Integer[] rely=new Integer[2]; rely[0]=Integer.parseInt(str.split("->")[0]); rely[1]=Integer.parseInt(str.split("->")[1]); table.add(rely); }
第三题:
Scanner sc=new Scanner(System.in); int row=sc.nextInt(); int col=sc.nextInt(); int t=sc.nextInt(); int[][] table=new int[row][col]; for(int i=0;i<row;i++) { for(int j=0;j<col;j++) { table[i][j]=sc.nextInt(); } }