Python3 实现字符读入
Python3 实现字符读入
来源:https://www.luogu.com.cn/discuss/724761
此题卡内存,如果按照 Python 常用的 input().split()
方法会 MLE。
因为 input()
一次读入大量字符串,占用内存极大。
于是打算按照 C++ 的快读逻辑写一个 Python3 的快读。
思路就是,利用 sys.stdin.read
的读入若干个字符功能,实现类似 C++ 的 getchar()
的功能,其余的与 C++ 类似。
from sys import stdin def gc(): return stdin.read(1) def digit(c): if c == '': return False return ord(c) >= ord('0') and ord(c) <= ord('9') def read(): n, f, c = 0, 0, gc() while not digit(c): f, c = f | (c == '-'), gc() while digit(c): n, c = n * 10 + ord(c) - ord('0'), gc() if f: return -n; return n
本文来自博客园,作者:RainPPR,转载请注明原文链接:https://www.cnblogs.com/RainPPR/p/python3-getchar.html
如有侵权请联系我(或 2125773894@qq.com)删除。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步