输入输出外挂总结
程序员文章站
2024-03-17 16:35:22
...
适用于正整数
inline void scan_d(int &ret)
{
char c;
ret = 0;
while ((c = getchar()) < '0' || c > '9');
while (c >= '0' && c <= '9')
{
ret = ret * 10 + (c - '0'), c = getchar();
}
}
适用于正负整数
inline bool scan_d(int &ret)
{
char c;
int sgn;
if (c = getchar(), c == EOF)
{
return 0; //EOF
}
while (c != '-' && (c < '0' || c > '9'))
{
c = getchar();
}
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9')
{
ret = ret * 10 + (c - '0');
}
ret *= sgn;
return 1;
}
inline void print_d(int x)
{
if (x > 9)
{
print_d(x / 10);
}
putchar(x % 10 + '0');
}
仅适合纯数字输入输出
int Scan()
{ // 输入外挂
int res = 0, flag = 0;
char ch;
if ((ch = getchar()) == '-')
{
flag = 1;
}
else if(ch >= '0' && ch <= '9')
{
res = ch - '0';
}
while ((ch = getchar()) >= '0' && ch <= '9')
{
res = res * 10 + (ch - '0');
}
return flag ? -res : res;
}
void Out(int a)
{ // 输出外挂
if (a < 0)
{
putchar('-');
a = -a;
}
if (a >= 10)
{
Out(a / 10);
}
putchar(a % 10 + '0');
}
int main()
{
int T, n;
scanf ("%d", &T);
while (T--)
{
n = Scan();
Out(n);
printf("\n");
}
return 0;
}
适用于正负数(int,long long,float,double)
bool scan_d(T &ret)
{
char c;
int sgn;
T bit = 0.1;
if (c=getchar(), c==EOF)
{
return 0;
}
while (c! = '-' && c != '.' && (c < '0' || c > '9'))
{
c = getchar();
}
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9')
{
ret = ret * 10 + (c - '0');
}
if (c == ' ' || c == '\n')
{
ret *= sgn;
return 1;
}
while (c = getchar(), c >= '0' && c <= '9')
{
ret += (c - '0') * bit, bit /= 10;
}
ret *= sgn;
return 1;
}
inline void print_d(int x)
{
if (x > 9)
{
print_d(x / 10);
}
putchar(x % 10 + '0');
}
其他
上面那个输入正负整数的有点难用。
再附一个:
void in(int &m)
{
char ch;
int flag = 0;
while ((ch = getchar()) < '0' || ch > '9')
{
if (ch == '-')
{
flag = 1;
}
}
for (m = 0; ch >= '0' && ch <= '9'; ch = getchar())
{
m = m * 10 + ch - '0';
}
if (flag)
{
m *= -1;
}
}
上一篇: 每日十个面试题--(java基础篇)-(11-20)
下一篇: 水仙花数