[关闭]
@lychee123 2017-09-16T16:46:24.000000Z 字数 2186 阅读 1203

输入输出外挂

外挂


  1. #include<bits/stdc++.h>
  2. struct FastIO
  3. {
  4. static const int S = 1310720;
  5. int wpos;
  6. char wbuf[S], endofFile;
  7. FastIO() : wpos(0), endofFile(0) {}
  8. inline int xchar()
  9. {
  10. if(endofFile) exit(0);
  11. static char buf[S];
  12. static int len = 0, pos = 0;
  13. if(pos == len)
  14. pos = 0, len = fread(buf, 1, S, stdin);
  15. if(pos == len)
  16. {
  17. endofFile = 1;
  18. return '\n';
  19. }
  20. return buf[pos ++];
  21. }
  22. inline unsigned long long xuint()//输入正整数
  23. {
  24. int c = xchar();
  25. unsigned long long x = 0;
  26. while(c <= 32)
  27. c = xchar();
  28. for(; '0' <= c && c <= '9'; c = xchar())
  29. x = x * 10 + c - '0';
  30. return x;
  31. }
  32. inline long long xint()//输入整数
  33. {
  34. long long s = 1;
  35. int c = xchar(), x = 0;
  36. while(c <= 32)
  37. c = xchar();
  38. if(c == '-')
  39. s = -1, c = xchar();
  40. for(; '0' <= c && c <= '9'; c = xchar())
  41. x = x * 10 + c - '0';
  42. return x * s;
  43. }
  44. inline void xstring(char *s)//输入字符串
  45. {
  46. int c = xchar();
  47. while(c <= 32)
  48. c = xchar();
  49. for(; c > 32; c = xchar())
  50. * s++ = c;
  51. *s = 0;
  52. }
  53. inline double xdouble()//输入double数据
  54. {
  55. bool sign = 0;
  56. char ch = xchar();
  57. double x = 0;
  58. while(ch <= 32)
  59. ch = xchar();
  60. if(ch == '-')
  61. sign = 1, ch = xchar();
  62. for(; '0' <= ch && ch <= '9'; ch = xchar())
  63. x = x * 10 + ch - '0';
  64. if(ch == '.')
  65. {
  66. double tmp = 1;
  67. ch = xchar();
  68. for(; ch >= '0' && ch <= '9'; ch = xchar())
  69. tmp /= 10.0, x += tmp * (ch - '0');
  70. }
  71. if(sign)
  72. x = -x;
  73. return x;
  74. }
  75. inline void wchar(int x)//输出char型
  76. {
  77. if(wpos == S)
  78. fwrite(wbuf, 1, S, stdout), wpos = 0;
  79. wbuf[wpos ++] = x;
  80. }
  81. inline void wint(long long x)//输出int型
  82. {
  83. if(x < 0)
  84. wchar('-'), x = -x;
  85. char s[24];
  86. int n = 0;
  87. while(x || !n)
  88. s[n ++] = '0' + x % 10, x /= 10;
  89. while(n--)
  90. wchar(s[n]);
  91. }
  92. inline void wstring(const char *s)//输出字符串
  93. {
  94. while(*s)
  95. wchar(*s++);
  96. }
  97. inline void wdouble(double x, int y = 6)//输出double数本身,精度
  98. {
  99. static long long mul[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000LL, 100000000000LL, 1000000000000LL, 10000000000000LL, 100000000000000LL, 1000000000000000LL, 10000000000000000LL, 100000000000000000LL};
  100. if(x < -1e-12)
  101. wchar('-'), x = -x;
  102. x *= mul[y];
  103. long long x1 = (long long) floorl(x);
  104. if(x - floor(x) >= 0.5)
  105. ++x1;
  106. long long x2 = x1 / mul[y], x3 = x1 - x2 * mul[y];
  107. wint(x2);
  108. if(y > 0)
  109. {
  110. wchar('.');
  111. for(size_t i = 1; i < y && x3 * mul[i] < mul[y]; wchar('0'), ++i);
  112. wint(x3);
  113. }
  114. }
  115. ~FastIO()
  116. {
  117. if(wpos)
  118. fwrite(wbuf, 1, wpos, stdout), wpos = 0;
  119. }
  120. } io;

用法

  1. //放在头文件下面
  2. //输入
  3. int x;
  4. x=io.xuint();
  5. char s;
  6. s=io.xchar();
  7. //输出
  8. io.wint(x);
  9. io.wchar(s);
  10. io.wchar('\n');//输出换行
  11. string str[100];
  12. io.xstring(str);//输入字符串
  13. io.wstring(str);//输出字符串
  14. double d = io.xdouble();//输入double
  15. io.wdouble(d, 8);//输出double,保留8位小数

注意:
    输出double的时候,要保证输出的值的整数部分的位数+保留精度的位数小于18位。
    读入到文件结束会马上中断程序运行
    使用此模板后,不能再使用scanf或者cin。但是依然可以使用print和cout。但是如果调用了io的写函数,print和cout也不能再使用了。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注