课评: 转出学生归档(王子墨/张立坤/黄俊博)+ 周四班级总结 + 新生章梓皓档案 + C++示例代码
This commit is contained in:
36
C++代码/GESP考级/三级/202609分割字符串解法2.cpp
Normal file
36
C++代码/GESP考级/三级/202609分割字符串解法2.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <bits/stdc++.h>
|
||||
#define endl '\n'
|
||||
using namespace std;
|
||||
|
||||
string s;
|
||||
|
||||
int main() {
|
||||
getline(cin, s);
|
||||
|
||||
int len = s.size();
|
||||
string curr = ""; // 要输出的这个字符串
|
||||
int kg = 1; // 中断的空格数量
|
||||
int cnt = 0; // 当前空格的数量
|
||||
|
||||
for(char c : s) {
|
||||
curr += c;
|
||||
if(c == ' ') {
|
||||
cnt++;
|
||||
} else {
|
||||
cnt = 0;
|
||||
}
|
||||
|
||||
if(cnt == kg) {
|
||||
cout << curr << endl;
|
||||
cnt = 0;
|
||||
kg++;
|
||||
curr = "";
|
||||
}
|
||||
|
||||
}
|
||||
if(curr.size() >= 1) {
|
||||
cout << curr << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user