关于输出换行的问题!
查看原帖
关于输出换行的问题!
370808
themysteryboy楼主2023/8/6 17:39

先贴上代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <vector>

void TLE(int &n) {
  int t = 1;
  for (int i = n; i >= 0; i--) {
    for (int j = 1; j <= i; j++, t++) {
      printf("%02d", t);
    }
    printf("\n"); // 注意这一行
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
  
  int n;
  std::cin >> n;

  TLE(n);

  return 0;
}

代码中注释的地方我试着改成std::cout << '\n';结果就是全部一行打印一通然后换很多次行,特别奇怪;然后我把这行改成std::cout << std::endl;的话就多输出一行,然后就全WA,最后改成了上面的AC代码的写法才全对

不明白这几种输出方式的区别在哪里?特别是std::cout << '\n';为什么会出现那种问题

2023/8/6 17:39
加载中...