ACM模式代码模板

Java代码实例:

import java.util.*;

class Solution {
    public int add(int a, int b) {
        return a + b;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // 读取到 EOF
        while (sc.hasNext()) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int result = new Solution().add(a, b);
            System.out.println(result);
        }
        sc.close();
    }
}

FastScanner模板:

import java.io.*;
import java.util.*;

public class Main {

    static class FastScanner {
        private final InputStream in = System.in;
        private final byte[] buffer = new byte[1 << 16];
        private int ptr = 0, len = 0;

        private int read() throws IOException {
            if (ptr >= len) {
                len = in.read(buffer);
                ptr = 0;
                if (len <= 0) return -1;
            }
            return buffer[ptr++];
        }

        int nextInt() throws IOException {
            int c;
            do {
                c = read();
            } while (c <= ' ');

            int sign = 1;
            if (c == '-') {
                sign = -1;
                c = read();
            }

            int val = 0;
            while (c > ' ') {
                val = val * 10 + (c - '0');
                c = read();
            }
            return val * sign;
        }

        long nextLong() throws IOException {
            int c;
            do {
                c = read();
            } while (c <= ' ');

            int sign = 1;
            if (c == '-') {
                sign = -1;
                c = read();
            }

            long val = 0;
            while (c > ' ') {
                val = val * 10 + (c - '0');
                c = read();
            }
            return val * sign;
        }
    }

    public static void main(String[] args) throws Exception {
        FastScanner fs = new FastScanner();
        StringBuilder sb = new StringBuilder();

        int T = fs.nextInt();
        while (T-- > 0) {
            int n = fs.nextInt();
            long sum = 0;
            for (int i = 0; i < n; i++) {
                sum += fs.nextLong();
            }
            sb.append(sum).append('\n');
        }

        System.out.print(sb.toString());
    }
}

FastScanner + FastWriter模板

import java.io.*;
import java.util.*;

public class Main {

    // ---------- FastScanner ----------
    static class FastScanner {
        private final InputStream in = System.in;
        private final byte[] buffer = new byte[1 << 16];
        private int ptr = 0, len = 0;

        private int read() throws IOException {
            if (ptr >= len) {
                len = in.read(buffer);
                ptr = 0;
                if (len <= 0) return -1;
            }
            return buffer[ptr++];
        }

        String next() throws IOException {
            StringBuilder sb = new StringBuilder();
            int c;
            do {
                c = read();
            } while (c <= ' ' && c != -1);

            while (c > ' ') {
                sb.append((char) c);
                c = read();
            }
            return sb.toString();
        }

        int nextInt() throws IOException {
            int c;
            do {
                c = read();
            } while (c <= ' ');

            int sign = 1;
            if (c == '-') {
                sign = -1;
                c = read();
            }

            int val = 0;
            while (c > ' ') {
                val = val * 10 + (c - '0');
                c = read();
            }
            return val * sign;
        }

        long nextLong() throws IOException {
            int c;
            do {
                c = read();
            } while (c <= ' ');

            int sign = 1;
            if (c == '-') {
                sign = -1;
                c = read();
            }

            long val = 0;
            while (c > ' ') {
                val = val * 10 + (c - '0');
                c = read();
            }
            return val * sign;
        }

        double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }
    }

    // ---------- FastWriter ----------
    static class FastWriter {
        private final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        void print(Object o) throws IOException {
            bw.write(String.valueOf(o));
        }

        void println(Object o) throws IOException {
            bw.write(String.valueOf(o));
            bw.newLine();
        }

        void println() throws IOException {
            bw.newLine();
        }

        void flush() throws IOException {
            bw.flush();
        }

        void close() throws IOException {
            bw.flush();
            bw.close();
        }
    }

    public static void main(String[] args) throws Exception {
        FastScanner fs = new FastScanner();
        FastWriter out = new FastWriter();

        int T = fs.nextInt();
        while (T-- > 0) {
            int n = fs.nextInt();
            long sum = 0;
            for (int i = 0; i < n; i++) {
                sum += fs.nextLong();
            }
            out.println(sum);
        }

        out.flush();
    }
}

评论