輸入輸出外掛總結
阿新 • • 發佈:2018-12-08
適用於正整數
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;
}
}