第一轮
最后一版(AC)
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main() {
// getline()读取整行,便于字符串流分割
string line;
getline(cin, line);
// 用istringstream创建输入字符串流对象
vector<string> words;
string word;
istringstream iss(line);
// 字符串分割成单词
while (iss >> word) {
words.push_back(word);
}
string last_word = words.back();
cout << last_word.size() << endl;
}
// 64 位输出请用 printf("%lld")
- 如何读取一整行带空格的字符串并以空格为界分割?
- getline()读取,istringstream创建输入字符串流对象。
- 再配合一个string和一个vector<string>就可以分割完毕。
- 如何在
vector<string>
的最后追加字符串?——push_back()
。 - 如何获得
vector<string>
的最后一个字符串?——back()
。
第二轮
第一版(AC)
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main() {
string s;
getline(cin, s);
istringstream iss(s);
int l;
string word;
while(iss >> word){
l = word.size();
}
cout << l;
return 0;
}
// 64 位输出请用 printf("%lld")
- 由于此题只需要确定长度,因此没必要声明一个
vector<sting>
来存储所有单词,只需不断更新长度即可。