ACM 模式通用代码模板

C++ 模板

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
//-------------------
#define fi first
#define se second
//-------------------
typedef long long LL;
typedef pair<int,int> PII;
//-------------------
const LL mod = 998244353;
const int N = 2e5+10;
//-------------------

//主要逻辑写在solve中
void solve()
{

    return;
}

void test()
{

}

signed main()
{
    //如果是大规模数据就开发下面的三行代码,注意开放以下限制后不能将cin和scanf混用!!
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //test()是方便测试一些不确定的代码使用的
    // test();
    int t=1;
    //多组数据处理
    cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}

Java 模板

java 复制代码
import java.io.*;
import java.util.*;

/**
 * 大厂笔试 Java 通用模板(ACM 模式)
 * 核心:使用 BufferedReader + StringTokenizer 实现快速读取,PrintWriter 实现快速输出。
 * 这种写法远比 Scanner 快 10 倍以上,有效避免在大规模数据下出现 TLE(超时)。
 */
public class Main {
    // 1. 定义快速输入输出组件
    // BufferedReader 比 Scanner 效率更高,因为它有更大的缓冲区
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    // StringTokenizer 用于将读入的一行字符串按空格拆分成一个个单词/数字
    static StringTokenizer st;
    // PrintWriter 带有缓冲区,可以一次性输出,比 System.out.println 快得多
    static PrintWriter out = new PrintWriter(System.out);


    // 2. 读取下一个字符串的方法
    static String next() throws IOException {
        // 如果当前行没有更多单词,就读取下一行
        while (st == null || !st.hasMoreElements()) {
            String line = br.readLine();
            if (line == null) return null; // 读到文件末尾 (EOF)
            st = new StringTokenizer(line);
        }
        return st.nextToken();
    }

    // 3. 读取下一个整数的方法
    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    // 4. 读取下一个长整数的方法 (处理 1e18 以上的数据)
    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    // 5. 核心解题逻辑区域
    public static void solve() throws IOException {
        /**
         * 在这里编写你的算法逻辑
         * 例如:
         * int n = nextInt();
         * int[] arr = new int[n];
         * for(int i=0; i<n; i++) arr[i] = nextInt();
         *
         * 注意:必须使用 out.println 而不是 System.out.println() 才能享受性能提升
         */
    }

    // 6. 程序主入口
    public static void main(String[] args) throws IOException {
        // 默认处理 1 组数据
        int t = 1;

        /**
         * 场景 A:题目第一行给出 T 组数据 (如 T=2)
         * 取消下面两行的注释即可:
         */
        // String line = br.readLine();
        // if (line != null) t = Integer.parseInt(line.trim());

        // 循环处理每组数据
        while (t-- > 0) {
            solve();
        }

        // 7. 非常重要:刷新并关闭输出流
        // 如果不写 out.flush(), 缓冲区里的内容可能不会被打印到屏幕上,导致判题失败
        out.flush();
        out.close();
    }
}

Python 模板

python 复制代码
import sys

# 1. 提高递归深度限制
# Python 默认递归深度仅为 1000,处理 DFS(深度优先搜索)或树形 DP 时极易爆栈
# 设置为 200,000 可以应对大多数笔试题目的数据范围
sys.setrecursionlimit(200000)

# 2. 启用"快速"模式
# 内置的 input() 函数在读取大量数据时非常慢(因为它会处理提示符并去除末尾换行符)
# 使用 sys.stdin.readline 替代 input,读取速度能提升数倍
# 注意:使用 readline 读入的字符串末尾会带有换行符 \n,通常需要配合 .strip() 使用
input = sys.stdin.readline


def solve():
    """
    核心解题逻辑区域
    """

    # 常用读取方式示例:
    # s = input().strip()          # 读取一行字符串,并去掉末尾换行符
    # n = int(input())             # 读取一个整数
    # a, b = map(int, input().split()) # 读取一行中的两个整数
    # nums = list(map(int, input().split())) # 读取一行中的所有整数并转为列表

    # 在这里编写你的代码逻辑
    pass


def main():
    """
    程序主入口,负责处理多组数据
    """

    # 尝试读取第一行内容(通常是测试数据组数 T,或者第一组数据的开头)
    line = sys.stdin.readline()
    if not line:
        return

    # 默认处理 1 组数据
    t = 1

    # 场景 A:题目第一行给出测试组数 T(如 T=2)
    # 取消下面一行的注释即可:
    # t = int(line.strip())

    # 场景 B:题目没有给出 T,需要一直读到 EOF(文件末尾)
    # 此时逻辑需要调整为 while 循环,或在 for 循环中根据输入判断

    for _ in range(t):
        solve()


# 标准的 Python 启动写法
if __name__ == '__main__':
    main()
相关推荐
IT界的老黄牛5 小时前
从 MQ 积压追到事件总线:诊断 4K 线程吃光 7G 内存的实战
java·运维·rocketmq
我命由我123455 小时前
C++ - 面向对象 - 析构函数
android·c语言·开发语言·c++·visualstudio·visual studio·android runtime
小旭95275 小时前
商品详情实现与缓存问题(穿透、击穿、雪崩)解决方案
java·数据库·spring boot·后端·缓存
ComputerInBook5 小时前
Euclid 几何变换——仿射(affine)变换
算法·仿射变换·几何变换
hnxaoli5 小时前
统信小程序(十三)循环键鼠操作程序
python·小程序
AI视觉网奇6 小时前
blender底部对齐
开发语言·python·blender
lunzi_08266 小时前
【学习笔记】《Python编程 从入门到实践》第2章:变量命名规则、字符串操作与数值类型详解
笔记·python·学习
E_ICEBLUE6 小时前
Python 教程:快速复制 Excel 工作表
python·excel
苦逼的猿宝6 小时前
基于springboot的课程作业管理系统(源码+论文)
java·毕业设计·springboot·计算机毕业设计