C++ 圆函数不';似乎工作不正常
C++ 工作是查找字符串序列的平均长度。 我使用了这段代码,它在第1、第2和第4个测试用例(总共5个)中都有效(注意,输入未知)C++ 圆函数不';似乎工作不正常,c++,C++,C++ 工作是查找字符串序列的平均长度。 我使用了这段代码,它在第1、第2和第4个测试用例(总共5个)中都有效(注意,输入未知) #包括 #包括 使用名称空间std; int main(){ 字符串输入; int k=0; 整数和=0; 同时(cin>>输入){ int n=input.size(); k++; 总和+=n; } 浮出=(浮出)总和/k; 在没有看到示例输入的情况下,我只是在猜测格式。当尝试在我的编辑器中运行代码时,我得到一个无限循环。这是因为如果只按Enter键(空字符串),则
#包括
#包括
使用名称空间std;
int main(){
字符串输入;
int k=0;
整数和=0;
同时(cin>>输入){
int n=input.size();
k++;
总和+=n;
}
浮出=(浮出)总和/k;
在没有看到示例输入的情况下,我只是在猜测格式。当尝试在我的编辑器中运行代码时,我得到一个无限循环。这是因为如果只按Enter键(空字符串),则std::cin
不会转换为false
.因此,我的代码假设所有单词都在一行中输入。这意味着我现在必须将该行拆分为单个单词
要使它与std::cin
一起工作,我必须按Ctrl+d
(WSL Debian)来打破循环,我一点也不喜欢这样
最大的问题是你没有去掉标点符号。如果你不这样做,标点符号会增加你的字符串长度,并会使你的平均值下降。你也可以把“对最近的整数进行四舍五入”理解为简单的四舍五入,它可以向上或向下四舍五入。你想要的函数是std::ceil()
,因为它将始终上升到下一个整数
#包括//标准::删除(如果,标准::复制)
#include//std::isalpha
#包括
#包括
#include//std::istream\u迭代器
#包括//标准::累积
#包含//以便于(在代码中)在空格上拆分字符串
#include//用于保存单个单词的动态数组
//将整行字符串拆分为单个单词的向量
标准::向量拆分(标准::字符串行){
std::载体tmp;
std::stringstream(行);
std::copy(std::istream_迭代器(流),
std::istream_迭代器(),std::back_插入器(tmp));
返回tmp;
}
//从单词向量中的每个单词中去除非字母字符
std::向量条(std::向量字){
用于(自动和文字:文字){
word.erase(std::remove_if(word.begin(),word.end(),
[](const auto&x){return!std::isalpha(x);}),
word.end());
}
返回单词;
}
int main(){
std::字符串输入;
std::getline(std::cin,input);//读取整行输入,直到输入
自动字=拆分(输入);
字=条(字);
if(words.size()==0){
std::cout我怀疑您在一个角落的情况下被绊倒了,比如所有标点符号的字符串未经处理而导致浮点异常或类似的情况,比如“,,====!!!”
您可以处理该案例(以及所有案例)以多种不同的方式。一种直接的方法是使用删除所有标点符号,然后通过创建字符串流计算单词和字符数,并简单地将字符和单词相加。不需要向量来保存单词,您只需要关心删除标点和空格的总数,例如
#include <iostream>
#include <sstream>
#include <string>
#include <cctype>
#include <algorithm>
int main (void) {
std::string line {};
size_t nchars = 0, nwords = 0;
if (!getline (std::cin, line)) /* read line of input */
return 1;
line.erase (std::remove_if (line.begin(), /* erase-remove punct */
line.end(),
[](unsigned char c) {
return ispunct(c);
}), line.end());
if (line.find_first_not_of(" \t") == std::string::npos) { /* all punct/space */
std::cout << "avg: 0\n";
return 0;
}
std::stringstream ss (line); /* stringstream from line */
while (ss >> line) { /* count words */
nchars += line.length(); /* add total chars */
nwords += 1; /* add total words */
}
std::cout << "avg: " << (nchars + nwords - 1)/nwords << '\n'; /* result */
}
或
$。/bin/mean\u of\u word\u len
你不能那样做吗?
平均值:3
或
$。/bin/mean\u of\u word\u len
字典里最长的单词是。。。
平均值:5
或
$。/bin/mean\u of\u word\u len
词典中最长的单词是。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
平均值:5
或
$。/bin/mean\u of\u word\u len
,,, === !!!
平均值:0
或
$。/bin/mean\u of\u word\u len
,,a===!!!
平均值:1
仔细检查一下,试一试,如果您还有其他问题,请告诉我。当您尝试了您知道输入和输出的测试用例时,发生了什么事?您是否尝试过我敢打赌,如果您有100个测试用例,无论您发现自己为了使四舍五入起作用做了哪些扭曲,一个或多个都会失败。@Janstew you sh在帖子中(而不是评论中)应该包括问题的确切措辞。例如,如果out==2.5
,结果应该打印出来吗?@Janstew在你的帖子中列出的问题说要汇总。它还说要删除标点符号,我在你的代码中没有看到任何这样做的尝试。有很多好的建议,但过于复杂,因为你可以主要替换input.s原始代码中的ize()
,如果std::count_
(并对代码进行一些小的调整)。std::count_如果
,这是一个很好的建议。我会记住这一点。不……阅读我附加的第二个测试用例……标点字符也会被计算。@Janstew但你的问题明确地说“删除所有标点符号。四舍五入到最接近的整数。”?这不对吗?sry我知道该平台的问题是混乱的…并且没有被控制。我停止使用它。所以不要介意这个问题。
#include <iostream>
#include <sstream>
#include <string>
#include <cctype>
#include <algorithm>
int main (void) {
std::string line {};
size_t nchars = 0, nwords = 0;
if (!getline (std::cin, line)) /* read line of input */
return 1;
line.erase (std::remove_if (line.begin(), /* erase-remove punct */
line.end(),
[](unsigned char c) {
return ispunct(c);
}), line.end());
if (line.find_first_not_of(" \t") == std::string::npos) { /* all punct/space */
std::cout << "avg: 0\n";
return 0;
}
std::stringstream ss (line); /* stringstream from line */
while (ss >> line) { /* count words */
nchars += line.length(); /* add total chars */
nwords += 1; /* add total words */
}
std::cout << "avg: " << (nchars + nwords - 1)/nwords << '\n'; /* result */
}