9月8号华为机试,做题时发现和平时在leetcode上刷题的模式完全不一样,结果一道都没调试成功,第一题二叉树遍历放leetcode上应该是直接秒的。写出一道就能过啊,真的哭了 T T
简单总结一下C++ ACM模式中常用的输入输出语句,避免以后再出现这种情况。
cin>>x
读取数字或字符串到x变量。会跳过数字/字符串前的space/tab/enter,直到遇到space/tab/enter为止(这次遇到的不会跳过,留给下一次cin处理)。
getline()
需要 #include <string>
读入一行,最后的enter也会读取,但是会在读入的字符串中舍弃enter字符
getline(istream is, string str, 结束符) ,结束符默认是换行符 ‘\n’
如果先cin再getline则需要注意cin留下来的enter字符,可以用cin.get()来取出该enter字符。比如输出每一行数据的总和:
#include<iostream>
using namespace std;
int main() {
int sum = 0;
int temp;
while (cin >> temp) {
sum += temp;
if (cin.get(<