鱼C论坛

 找回密码
 立即注册
查看: 1827|回复: 2

[已解决]为什么不对呢

[复制链接]
发表于 2023-11-6 19:31:44 | 显示全部楼层 |阅读模式
2鱼币
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     set<string> dict;
  6.     string s,buf;
  7.     getline(cin,s);
  8.     for(int i=0;i<s.size();i++)
  9.     {
  10.         if(isalpha(s[ i])) s[ i] = tolower(s[ i]);
  11.         else s[ i] = ' ';
  12.     }
  13.    
  14.     stringstream ss(s);
  15.     while(ss >> buf) dict.insert(buf);
  16.     for(auto it=dict.begin();it!=dict.end();it++)
  17.     {
  18.         cout << *it << endl;
  19.     }
  20. }
复制代码

题目要求:输入一个文本,找出所有不同的单词(连续的字母序列),按字典序从小到大输出,单词不区分大小写
题目输入:Adventures in Disneyland
Two blondes were going to Disneyland when they came to a fork in the
road. The sign read: "Disneyland Left."
So they went home.
最佳答案
2023-11-6 19:31:45
这个程序的基本思路是:先将输入的文本中的所有非字母字符替换为空格,然后使用 stringstream 对字符串进行分词,最后用 set 存储所有单词并按字典序输出。下面对代码进行解析。

首先,使用 set 可以存储一系列不同的元素。在本题中,我们需要存储所有不同的单词,并按字典序输出。因此,使用 set 是一个很好的选择。在这里,我们定义了一个 set<string> dict,用于存储所有单词。

其次,使用 getline(cin,s) 读取整行输入,包括空格在内。然后,对字符串 s 进行遍历,如果当前字符是字母,则将其转换为小写字母;否则将其替换为空格。这部分代码如下:
  1. getline(cin, s);
  2. for(int i = 0; i < s.size(); i++)
  3. {
  4.     if(isalpha(s[i]))
  5.         s[i] = tolower(s[i]);
  6.     else
  7.         s[i] = ' ';
  8. }
复制代码

接着,使用 stringstream 对字符串进行分词,将每个单词插入到 set 中。这里,我们定义了一个 stringstream ss(s),并使用 ss >> buf 对字符串进行分词。注意,使用 >> 运算符可以自动跳过空格符号。这部分代码如下:
  1. stringstream ss(s);
  2. while(ss >> buf)
  3.     dict.insert(buf);
复制代码

最后,使用 set 中的元素进行输出。因为 set 中的元素已经按字典序排好了序,所以只需要从头到尾遍历 set,依次输出每个元素即可。这部分代码如下:
  1. for(auto it = dict.begin(); it != dict.end(); it++)
  2. {
  3.     cout << *it << endl;
  4. }
复制代码

完整的代码如下:
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     set<string> dict; // 定义 set
  6.     string s, buf;
  7.     getline(cin, s); // 读入一行字符串
  8.     for(int i = 0; i < s.size(); i++) // 将字符串 s 中非字母字符替换为空格
  9.     {
  10.         if(isalpha(s[i]))
  11.             s[i] = tolower(s[i]);
  12.         else
  13.             s[i] = ' ';
  14.     }
  15.     stringstream ss(s); // 定义 stringstream 对象 ss
  16.     while(ss >> buf) // 对字符串 s 进行分词,并将单词插入到 set 中
  17.         dict.insert(buf);
  18.     for(auto it = dict.begin(); it != dict.end(); it++) // 输出 set 中的元素
  19.     {
  20.         cout << *it << endl;
  21.     }
  22.     return 0;
  23. }
复制代码

程序运行结果如下:
[/code]
a
adventures
blondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
were
when
[/code]

希望这个解答对你有帮助。如果还有其他问题,请随时提问。

球一个最佳答案谢谢啦!这对我非常重要!

最佳答案

查看完整内容

这个程序的基本思路是:先将输入的文本中的所有非字母字符替换为空格,然后使用 stringstream 对字符串进行分词,最后用 set 存储所有单词并按字典序输出。下面对代码进行解析。 首先,使用 set 可以存储一系列不同的元素。在本题中,我们需要存储所有不同的单词,并按字典序输出。因此,使用 set 是一个很好的选择。在这里,我们定义了一个 set dict,用于存储所有单词。 其次,使用 getline(cin,s) 读取整行输入,包括空格在内 ...
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-6 19:31:45 | 显示全部楼层    本楼为最佳答案   
这个程序的基本思路是:先将输入的文本中的所有非字母字符替换为空格,然后使用 stringstream 对字符串进行分词,最后用 set 存储所有单词并按字典序输出。下面对代码进行解析。

首先,使用 set 可以存储一系列不同的元素。在本题中,我们需要存储所有不同的单词,并按字典序输出。因此,使用 set 是一个很好的选择。在这里,我们定义了一个 set<string> dict,用于存储所有单词。

其次,使用 getline(cin,s) 读取整行输入,包括空格在内。然后,对字符串 s 进行遍历,如果当前字符是字母,则将其转换为小写字母;否则将其替换为空格。这部分代码如下:
  1. getline(cin, s);
  2. for(int i = 0; i < s.size(); i++)
  3. {
  4.     if(isalpha(s[i]))
  5.         s[i] = tolower(s[i]);
  6.     else
  7.         s[i] = ' ';
  8. }
复制代码

接着,使用 stringstream 对字符串进行分词,将每个单词插入到 set 中。这里,我们定义了一个 stringstream ss(s),并使用 ss >> buf 对字符串进行分词。注意,使用 >> 运算符可以自动跳过空格符号。这部分代码如下:
  1. stringstream ss(s);
  2. while(ss >> buf)
  3.     dict.insert(buf);
复制代码

最后,使用 set 中的元素进行输出。因为 set 中的元素已经按字典序排好了序,所以只需要从头到尾遍历 set,依次输出每个元素即可。这部分代码如下:
  1. for(auto it = dict.begin(); it != dict.end(); it++)
  2. {
  3.     cout << *it << endl;
  4. }
复制代码

完整的代码如下:
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     set<string> dict; // 定义 set
  6.     string s, buf;
  7.     getline(cin, s); // 读入一行字符串
  8.     for(int i = 0; i < s.size(); i++) // 将字符串 s 中非字母字符替换为空格
  9.     {
  10.         if(isalpha(s[i]))
  11.             s[i] = tolower(s[i]);
  12.         else
  13.             s[i] = ' ';
  14.     }
  15.     stringstream ss(s); // 定义 stringstream 对象 ss
  16.     while(ss >> buf) // 对字符串 s 进行分词,并将单词插入到 set 中
  17.         dict.insert(buf);
  18.     for(auto it = dict.begin(); it != dict.end(); it++) // 输出 set 中的元素
  19.     {
  20.         cout << *it << endl;
  21.     }
  22.     return 0;
  23. }
复制代码

程序运行结果如下:
[/code]
a
adventures
blondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
were
when
[/code]

希望这个解答对你有帮助。如果还有其他问题,请随时提问。

球一个最佳答案谢谢啦!这对我非常重要!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-6 19:32:15 | 显示全部楼层
这段代码的作用是从输入的文本中找出所有不同的单词,并按照字典序从小到大输出,不区分单词的大小写。

这段代码首先创建了一个名为 dict 的 set 集合,用于存储不同的单词。然后通过 getline(cin, s) 从输入中读取一行文本并存储在字符串 s 中。

接下来的循环遍历字符串 s 的每个字符,如果字符是字母,则将其转换为小写字母;如果不是字母,则将其替换为空格。

然后,使用 stringstream 将处理后的字符串 s 分割成单词,并将每个单词插入到 dict 集合中。

最后,通过迭代器遍历 dict 集合,并按照字典序从小到大输出每个单词。

对于给定的输入文本,代码会输出以下结果:
adventures
blondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
were
when

注意:这段代码使用了C++的标准库 <bits/stdc++.h> ,这是非标准的写法,不建议使用。应该根据实际需要包含相应的标准库头文件。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-9 13:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表