跳到正文

题解

题解:P10815 【模板】快速读入

Maisie586_约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月1日
篇幅
约 2 分钟
获赞
5
题目
P10815

版本

  1. v1当前

解法

  1. 快读:在 fastRead 函数里,逐字符读入。

  2. 位移操作:在 fastRead 函数里,使用位移代替普通乘法,进一步加速。

  3. 循环累加:循环调用 fastRead 函数输入,并累加到 totalSumtotalSum 中。

细节

  • 记得开 long long。
  • 使用 getchar_unlocked 函数进行快读。
  • 在主函数中,首先读取整数个数 numnum,然后循环读取每个整数并累加。
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// 快读
inline ll fastRead() {
    ll x = 0, y = 1;
    char ch = getchar_unlocked();
    while (!(ch >= '0' && ch <= '9')) {
        if (ch == '-') y = -1;
        ch = getchar_unlocked();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 3) + (x << 1) + (ch - '0');
        ch = getchar_unlocked();
    }
    return x * y;
}

int num;
ll totalSum = 0;

int main() {
    scanf("%d", &num);
    for (int i = 0; i < num; ++i) {
        totalSum += fastRead();
    }
    printf("%lld\n", totalSum);
    return 0;
}
原文