c++笔试准备
一、 输入输出
1.多组数据【while】
cin
#include <iostream>
using namespace std;int main() {int a, b;while (cin >> a >> b) { // 当读取到EOF时,循环自动终止cout << a + b << endl;}return 0;
}
getline
#include <iostream>
#include <string>int main() {std::string line;while (std::getline(std::cin, line)) { // 当读取到 EOF 时,循环自动终止std::cout << "读取到的行是: " << line << std::endl;}return 0;
}
- 指定组数字
第一行n代表输入了多少数字
#include <iostream>
using namespace std;int main() {int n;cin >> n; // 先读取数据组数for (int i = 0; i < n; ++i) {int a, b;cin >> a >> b;cout << a + b << endl;}return 0;
}
- 使用自定义分隔符:
#include <iostream>
#include <string>int main() {std::string token;std::cout << "请输入一串以逗号分隔的数据: ";while (std::getline(std::cin, token, ',')) { // 使用逗号作为分隔符std::cout << "解析出的令牌是: " << token << std::endl;}return 0;
}
注意:
cin遇到空格会停止,getline可以适用于自定义分隔符和含空格的情形。
二、 递归
三、排序
四、字符串操作
五、数据结构
链表
struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(nullptr) {}
}
;
六、算法
技巧:
-
指数运算的性质
用于优化x的n次方时间复杂度问题 -
vector的扩容机制
-
双头指针