POJ 2880 句中最长的单词
- 时间限制:
- 1000ms
- 内存限制:
- 65536kB
- 描述
- 输入一个英文句子,长度不超过40个字符。编写程序,输出句子中最长的一个单词。
- 输入
- 长度不超过40的字符串
- 输出
- 句中最长的单词
- 样例输入
-
This is a test sentence
- 样例输出
-
sentence
- 提示
- 1.输入只有一个句子,不需使用while
2.若句尾有标点,则标点和最后一个单词可看成是一个单词,可以不用作额外判断
3.假设句中最长的单词只有一个
(1)、源代码:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(){
int i, n, a, b, num;
string str, temp, r;
getline(cin, str);
stringstream ss;
ss << str;
while(!ss.eof()){
ss >> temp;
if(temp.length() > r.length())
r = temp;
}
cout << r << endl;
return 0;
}
(2)、解题思路:利用sstream。
(3)、可能出错:略