纯底层代码
2025-10-06 17:46:37
发布于:广东
0阅读
0回复
0点赞
纯底层,无bits/stdc++.h,无iostream,纯底层实现(基于Linux)
// a+b.cpp 严格单整数输出,无换行
extern "C" {
long read(int, void *, unsigned long);
long write(int, const void *, unsigned long);
void exit(int);
}
typedef unsigned int u32;
/* 把 u32 v 变 decimal 字符串,返回长度 */
int u32toa(char *buf, u32 v) {
char tmp[16];
int i = 0, j = 0;
do { tmp[i++] = char('0' + v % 10); } while (v /= 10);
while (i--) buf[j++] = tmp[i];
return j;
}
int main() {
char ibuf[32], obuf[16];
long n = read(0, ibuf, sizeof ibuf);
if (n <= 0) exit(0);
/* 过滤空白 */
const char *p = ibuf, *q;
while (p < ibuf + n && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) ++p;
q = p;
while (q < ibuf + n && *q >= '0' && *q <= '9') ++q;
u32 a = 0, b = 0;
for (const char *t = p; t < q; ++t) a = a * 10 + (*t - '0');
p = q;
while (p < ibuf + n && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) ++p;
q = p;
while (q < ibuf + n && *q >= '0' && *q <= '9') ++q;
for (const char *t = p; t < q; ++t) b = b * 10 + (*t - '0');
u32 c = a + b;
int len = u32toa(obuf, c);
write(1, obuf, len); // 不写 '\n'
return 0;
}
这里空空如也

有帮助,赞一个