算法:快速輸入輸出

快速輸入輸出

功能

對於大量數據進行快速輸入輸出。

思路

利用getchar()代替scanf()輸入整數,利用putchar()代替printf()輸出整數。

模板

非負整數輸入

/**
  * @param x: the input number
  * @other: x >= 0;
  */
void FR(int& x) {
  x = 0;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar());
  for (; ch >= '0' && ch <= '9'; ch = getchar()) {
    x = 10*x + ch-'0';
  }
}

非負整數輸出

char f[100]; // the digits of the output number

/**
  * @param x: the output number
  * @other: x >= 0;
  */
void FW(const int& x) {
  int tmp = x;
  int s = 0;
  while (tmp > 0) {
    f[s++] = tmp%10 + '0';
    tmp /= 10;
  }
  while (s > 0) putchar(f[--s]);
}

整數輸入

/**
  * @param x: the input number
  */
void FR(int& x) {
  int sgn;
  char ch = getchar();
  for (; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
  sgn = (ch == '-') ? -1 : 1;
  x = (ch == '-') ? 0 : ch-'0';
  ch = getchar();
  for (; ch >= '0' && ch <= '9'; ch = getchar()) {
    x = 10*x + ch-'0';
  }
  x *= sgn;
}

整數輸出

char f[100]; // the digits of the output number

/**
  * @param x: the output number
  */
void FW(const int& x) {
  int tmp = x;
  if (tmp < 0) {
    tmp = -tmp;
    putchar('-');
  }
  int s = 0;
  while (tmp > 0) {
    f[s++] = tmp%10 + '0';
    tmp /= 10;
  }
  while (s > 0) putchar(f[--s]);

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章