1.ACM模式

ACM模式

一、Java之ACM注意点

  1. 类名称必须采用public class Main方式命名

  2. 在有些OJ系统上,即便是输出的末尾多了一个“ ”,程序可能会输出错误,所以在我看来好多OJ系统做的是非常之垃圾

  3. 有些OJ上的题目会直接将OI上的题目拷贝过来,所以即便是题目中有输入和输出文件,可能也不需要,因为在OJ系统中一般是采用标准输入输出,不需要文件

  4. 在有多行数据输入的情况下,一般这样处理,

static Scanner in = new Scanner(System.in);
while(in.hasNextInt())
或者是
while(in.hasNext())

  1. 有关System.nanoTime() 函数的使用,该函数用来返回最准确的可用系统计时器的当前值,以毫微秒为单位。

    long startTime = System.nanoTime();
    // ... the code being measured ...
    long estimatedTime = System.nanoTime() - startTime;

采用has xxxx的话,后面也要用next xxxx。比如前面用hasNextLine,那么后面要用 nextLine 来处理输入。

import java.io.*;
import java.math.*;
import java.util.*;
import java.text.*;

二、Java之输入输出处理

由于ACM竞赛题目的输入数据和输出数据一般有多组(不定),并且格式多种多样,所以,如何处理题目的输入输出是对大家的一项最基本的要求。这也是困扰初学者的一大问题。

格式1:Scanner sc = new Scanner (new BufferedInputStream(System.in));

格式2:Scanner sc = new Scanner (System.in);

在读入数据量大的情况下,格式1的速度会快些。

读一个整数: int n = sc.nextInt(); 相当于 scanf("%d", &n); 或 cin >> n; 

读一个字符串:String s = sc.next(); 相当于 scanf("%s", s); 或 cin >> s; 

读一个浮点数:double t = sc.nextDouble(); 相当于 scanf("%lf", &t); 或 cin >> t; 

读一整行: String s = sc.nextLine(); 相当于 gets(s); 或 cin.getline(...); 

判断是否有下一个输入可以用sc.hasNext()或sc.hasNextInt()或sc.hasNextDouble()或sc.hasNextLine()

例1:读入整数

Input 输入数据有多组,每组占一行,由一个整数组成。
Sample Input
56
67
100
123
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
	Scanner sc =new Scanner(System.in);
	while(sc.hasNext()){ //判断是否结束
		int score = sc.nextInt();//读入整数
		。。。。
		}
	}
}

例2:读入实数

输入数据有多组,每组占2行,第一行为一个整数N,指示第二行包含N个实数。


Sample Input
4
56.9 67.7 90.5 12.8
5
56.9 67.7 90.5 12.8
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
	Scanner sc =new Scanner(System.in);
	while(sc.hasNext()){
		int n = sc.nextInt();
		for(int i=0;i<n;i++){
			double a = sc.nextDouble();
			。。。。。。
		}
	}
}
}

例3:读入字符串【杭电2017 字符串统计】

输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。

Sample Input
2
asdfasdf123123asdfasdf
asdf111111111asdfasdfasdf
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for(int i=0;i<n;i++){
            String str = sc.next();
            ......
        }
    }
}

import java.util.Scanner;

public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = Integer.parseInt(sc.nextLine());
            for(int i=0;i<n;i++){
                String str = sc.nextLine();
                ......
        }
    }
}

例3:读入字符串【杭电2005 第几天?】

给定一个日期,输出这个日期是该年的第几天。

Input 输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成
1985/1/20
2006/3/12
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] dd = {0,31,28,31,30,31,30,31,31,30,31,30,31};
        while(sc.hasNext()){
            int days = 0;
            String str = sc.nextLine();
            String[] date = str.split("/");
            int y = Integer.parseInt(date[0]);
            int m = Integer.parseInt(date[1]);
            int d = Integer.parseInt(date[2]);
            if((y%400 == 0 || (y%4 == 0 && y%100 !=0)) && m>2) days ++;
            days += d;
            for(int i=0;i<m;i++){
                days += dd[i];
            }
            System.out.println(days);
        }
    }
}
1. 多组空格分割的两个整数 (无行数,结束字符限制)

题目描述

输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据包括多组。
输出a+b的结果

输入示例

1 5
10 20
12

输出示例

6
30
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) { 
            int a = in.nextInt();
            int b = in.nextInt();
            System.out.println(a + b);
        }
    }
}
2. 第一行组数接空格分割的两个正整数 (行数限制)

题目描述

输入第一行包括一个数据组数t(1 <= t <= 100)
接下来每行包括两个正整数a,b(1 <= a, b <= 10^9)
输出a+b的结果

输入示例

2
1 5
10 20
123

输出示例

6
30
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);
        }
    }
}

3. 空行分割的两个正整数,0 0 结束 (结束符限制)

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

输入示例

1 5
10 20
0 0
123

输出示例

6
30
import java.util.Scanner;
public class Main {
    public static void mai3n(String[] args) {
        Scanner in = new Scanner(System.in);
        
        while (in.hasNext()) { 
            int a = in.nextInt();
            int b = in.nextInt();
            if(a ==0 && b == 0) break;
            System.out.println(a + b);
        }
    }
}

4. 每行第一个为个数后带空格分割整数,0结束 (结束符限制,每行有个数限制)

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

输入示例

4 1 2 3 4
5 1 2 3 4 5
0
123

输出示例

10
15
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        while (in.hasNext()) { 
            int n = in.nextInt();
            if(n == 0) break;
            int sum = 0;
            for (int i = 0; i < n; i++) {
                sum += in.nextInt();
            }
            System.out.println(sum);
        }
    }
}

5. 第一行组数接第一个个数接空格分开的整数 (行数限制,每行有个数限制)

题目描述

输入的第一行包括一个正整数t(1 <= t <= 100), 表示数据组数。
接下来t行, 每行一组数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。

每组数据输出求和的结果

输入示例

2
4 1 2 3 4
5 1 2 3 4 5
123

输出示例

10
15
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++){
            int n = in.nextInt();
            int sum = 0;
            for(int j = 0; j < n; j++){
                sum += in.nextInt();
            }
            System.out.println(sum);
        }
    }
}

6. 每行第一个为个数后带空格分割整数 (无结束限制,每行有个数限制)

题目描述

输入数据有多组, 每行表示一组输入数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。

每组数据输出求和的结果

输入示例

4 1 2 3 4
5 1 2 3 4 5
12

输出示例

10
15
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) { 
            int n = in.nextInt();
            int sum = 0;
            while (n > 0) {
                sum += in.nextInt();
                n--;
            }
            System.out.println(sum);
        }
    }
}
7. 多组空格分隔的正整数 (无结束限制,每行无个数限制,需要当作字符串处理)

题目描述

输入数据有多组, 每行表示一组输入数据。
每行不定有n个整数,空格隔开。(1 <= n <= 100)。

每组数据输出求和的结果

输入示例

1 2 3
4 5
0 0 0 0 0
123

输出示例

6
9
0
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) { 
            String[] s = in.nextLine().split(" ");
            int sum = 0;
            for (int i = 0; i < s.length; i++) {
                sum += Integer.parseInt(s[i]);
            }
            System.out.println(sum);
        }
    }
}

3、输入为一个链表

这里选用最经典的反转链表题目作为例子。

package learnACM;

import java.util.Scanner;
import java.util.Stack;

public class LinkListInput {
    //题目描述
    //对于一个链表 L: L0→L1→…→Ln-1→Ln,
    //将其翻转成 L0→Ln→L1→Ln-1→L2→Ln-2→…

    //先构建一个节点类,用于链表构建
    static class LinkNode {
        int val;
        LinkNode next;
        public LinkNode(int val){
            this.val = val;
        }
    }

    public static void main(String[] args){
        //输入是一串数字,请将其转换成单链表格式之后,再进行操作
        //输入描述:
        //一串数字,用逗号分隔
        //输入
        //1,2,3,4,5
        Scanner scanner = new Scanner(System.in);
        //以字符串形式作为输入
        String str = scanner.next().toString();
        //通过分隔符将其转为字符串数组
        String[] arr  = str.split(",");
        //初始化一个整数数组
        int[] ints = new int[arr.length];
        //给整数数组赋值
        for(int j = 0; j<ints.length;j++) {
            ints[j] = Integer.parseInt(arr[j]);
        }
        Stack<LinkNode> stack = new Stack<>();
        LinkNode head = new LinkNode(0);
        LinkNode p = head;
        //链表初始化并放入stack中
        for(int i = 0; i < ints.length; i++){
            p.next = new LinkNode(ints[i]);
            p = p.next;
            stack.add(p);
        }
        head = head.next;
        //开始链表转换
        p = head;
        LinkNode q = stack.peek();
        while ((!p.equals(q)) && (!p.next.equals(q))) {
            q = stack.pop();
            q.next = p.next;
            p.next = q;
            p = p.next.next;
            q = stack.peek();
        }
        q.next = null;
        //输出
        //1,5,2,4,3
        //打印
        while (head != null) {
            if(head.next == null){
                System.out.print(head.val);
            }else{
                System.out.print(head.val + ",");
            }
            head = head.next;
        }
    }
}

4、树的输入(主要是构建树的过程)

构建树的过程因题目而异,但是大体的思路可以参考以下代码

posted @ 2022-04-27 10:58  我是个机器人  阅读(281)  评论(0)    收藏  举报