1、第一种情况(带逗号输入,元素个数未知)
输入形式:
输入的数据在nums中保存
1,2,3,4,60,70,5
# include<iostream>
# include<vector>
# include<string>
using namespace std;
int main() {
//输入
string s;
cin >> s;
int start = 0;
int num = 0;
vector<int> nums;
for (int i = 0; i < s.size(); i++) {
if (s[i] == ',') {
string temp = s.substr(start, i - start);
nums.push_back(atoi(temp.c_str()));
num++;
start = i + 1;
}
}
string temp = s.substr(start, s.size() - start + 1);
nums.push_back(atoi(temp.c_str()));
//打印验证
for (int i = 0; i < nums.size(); i++) {
cout << nums[i] << " ";
}
system("pause");
return 0;
}
2、第二种情况(带逗号输入,告诉元素个数)
输入形式:
在nums 数组中保存
8
1,2,30,100,50,2,7,5
输出形式:
1 2 30 100 50 2 7 5
代码:
# include<iostream>
# include<vector>
# include<string>
using namespace std;
int main() {
//输入
int n;
cin >> n;
string s;
cin >> s;
int start = 0;
int num = 0;
vector<int> nums;
for (int i = 0; i < s.size(); i++) {
if (s[i] == ',') {
string temp = s.substr(start, i - start);
nums.push_back(atoi(temp.c_str()));
num++;
start = i + 1;
}
if (num == n - 1) {
string temp = s.substr(start, s.size() - start + 1);
nums.push_back(atoi(temp.c_str()));
break;
}
}
//打印验证
for (int i = 0; i < n; i++) {
cout << nums[i] << " ";
}
system("pause");
return 0;
}
3、第三种情况(区间问题,带逗号输入,二维数组)
非重叠区间的总长度
在nums 二维数组中保存
# include<iostream>
# include<vector>
# include<string>
# include<algorithm>
using namespace std;
static bool cmp(vector<int>& a, vector<int>& b) {
if (a[0] == b[0]) {
return a[1] < b[1];
}
return a[0] < b[0];
}
int main() {
//输入
int n;
cin >> n;
vector<string> str;
for (int i = 0; i < n; i++) {
string mys;
cin >> mys;
str.push_back(mys);
}
vector<vector<int>> nums;
int t = 0;
while (t < n) {
vector<int> q;
string s = str[t];
int start = 0;
int num = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == ',') {
string temp = s.substr(start, i - start);
q.push_back(atoi(temp.c_str()));
num++;
start = i + 1;
}
if (num == 1) {
string temp = s.substr(start, s.size() - start + 1);
q.push_back(atoi(temp.c_str()));
break;
}
}
t++;
nums.push_back(q);
}
//打印输出验证
for (int i = 0; i < nums.size(); i++) {
for (int j = 0; j < nums[0].size(); j++) {
cout << nums[i][j] << " ";
}
cout << endl;
}
//算法实现
sort(nums.begin(), nums.end(), cmp);
int sum = 0;
int l = nums[0][0];
int r = nums[0][1];
for (int i = 1; i < nums.size(); i++) {
int l1 = nums[i][0];
int r1 = nums[i][1];
if (r <= l1) {
sum += (r - l);
l = l1;
r = r1;
}
else if (r > l1 && r1 > r) {
sum += (l1 - l);
l = r;
r = r1;
}
else if (r > l1 && r1 <= r) {
sum += (l1 - l);
l1 = r1;
r = r;
}
}
//算法结果
sum += r - l;
cout << sum << endl;
system("pause");
return 0;
}
4、第四种情况(二叉树的建立,带逗号以及“NULL”)
二叉树的创建与各种遍历,华为,网易的笔试都遇到了
# include<iostream>
# include<vector>
# include<queue>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int val) : val(val), left(NULL), right(NULL) {}
};
TreeNode* buildTree(vector<string> str) {
if (str.size() == 0) {
return 0;
}
queue<TreeNode*> qe;
TreeNode* head = new TreeNode(atoi(str[0].c_str()));
qe.push(head);
for (int i = 1; i < str.size(); i++) {
auto temp = qe.front();
qe.pop();
if (str[i] != "NULL") {
temp->left = new TreeNode(atoi(str[i].c_str()));
qe.push(temp->left);
}
if (i + 1 < str.size() && str[i + 1] != "NULL") {
temp->right = new TreeNode(atoi(str[i + 1].c_str()));
qe.push(temp->right);
}
i++;
}
return head;
}
void midSearch(vector<int>& nums, TreeNode* cur) {
if (!cur) {
return;
}
midSearch(nums, cur->left);
nums.push_back(cur->val);
midSearch(nums, cur->right);
}
void inorSearch(vector<int>& nums, TreeNode* cur) {
if (!cur) {
return;
}
inorSearch(nums, cur->left);
inorSearch(nums, cur->right);
nums.push_back(cur->val);
}
void prioSearch(vector<int>& nums, TreeNode* cur) {
if (!cur) {
return;
}
nums.push_back(cur->val);
prioSearch(nums, cur->left);
prioSearch(nums, cur->right);
}
void chengciSearch(vector<int>& nums, TreeNode* cur) {
queue<TreeNode*> qe;
qe.push(cur);
while (!qe.empty()) {
auto temp = qe.front();
qe.pop();
nums.push_back(temp->val);
if (temp->left) {
qe.push(temp->left);
}
if (temp->right) {
qe.push(temp->right);
}
}
}
int main() {
string mys;
cin >> mys;
vector<string> str;
string temp;
for (auto t : mys) {
if (t == ',') {
str.push_back(temp);
temp.clear();
}
else {
temp.push_back(t);
}
}
str.push_back(temp);
TreeNode* cur = buildTree(str);
vector<int> result;
cout << "先序遍历" << endl;
prioSearch(result, cur);
for (int i = 0; i < result.size(); i++) {
cout << result[i] << " ";
}
cout << endl;
result.clear();
cout << "层序遍历" << endl;
chengciSearch(result, cur);
for (int i = 0; i < result.size(); i++) {
cout << result[i] << " ";
}
cout << endl;
cout << "中序遍历" << endl;
result.clear();
midSearch(result, cur);
for (int i = 0; i < result.size(); i++) {
cout << result[i] << " ";
}
cout << endl;
cout << "后序遍历" << endl;
result.clear();
midSearch(result, cur);
for (int i = 0; i < result.size(); i++) {
cout << result[i] << " ";
}
system("pause");
return 0;
}
验证1:
验证2:
5、第五种情况 输入多组带空格的字符串
输入形式:
N组
N行字符串
N
SADSA SDAD SADASD ASDA
ASDAS SDASD ASDSA ASDAS
.
.
.
DSAD SDAS SDAS ASDA
代码:
# include<iostream>
# include<vector>
# include<string>
using namespace std;
int main() {
//输入
string N;
getline(cin, N);
int n = atoi(N.c_str());
vector<string> str;
while (n--) {
string temp;
getline(cin, temp);
str.push_back(temp);
}
//输出验证
for (int i = 0; i < str.size(); i++) {
cout << str[i] << endl;
}
system("pause");
return 0;
}
2 ASDASDAS ASDASDA ASDAS ASD AS DASD ASD ASAD ASDAD AS 数组输出: ASDASDAS ASDASDA ASDAS ASD AS DASD ASD ASAD ASDAD AS 请按任意键继续. . .
6、第六种情况 单行带空格字符串,转换为二维数组
输入:1,2 3,45 78,56 11,12
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
string s;
getline(cin, s);
int start1 = 0;
vector<string> str;
for (int i = 0; i < s.size(); i++) {
if (s[i] == ' ') {
string temp = s.substr(start1, i - start1);
str.push_back(temp);
start1 = i + 1;
}
}
string temp = s.substr(start1, s.size() - start1 + 1);
str.push_back(temp);
vector<vector<int>> nums;
int t = 0;
while (t < str.size()) {
vector<int> q;
string s = str[t];
int start = 0;
int num = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == ',') {
string temp = s.substr(start, i - start);
q.push_back(atoi(temp.c_str()));
num++;
start = i + 1;
}
if (num == 1) {
string temp = s.substr(start, s.size() - start + 1);
q.push_back(atoi(temp.c_str()));
break;
}
}
t++;
nums.push_back(q);
}
}
结果展示:
12,45 56,34 75,65 128,1
输出
12 45
56 34
75 65
128 1
请按任意键继续. . .
7、第七种情况 带空格输入,不告诉元素个数
#include <iostream>
#include <vector>
using namespace std;
int main() {
//p a, b;
vector<int> nums;
do {
int temp;
scanf_s("%d", &temp);
nums.push_back(temp);
} while (getchar() != '\n');
for (int i = 0; i < nums.size(); i++) {
cout << nums[i] << " ";
}
cout << endl;
system("pause");
return 0;
}