【问题标题】:Print longest name in a given string打印给定字符串中最长的名称
【发布时间】:2020-11-04 09:51:32
【问题描述】:

我编写了以下程序来显示给定字符串中最长的名称,但输出与预期不符。

#include <iostream>
using namespace std;

int main() 
{
        
        int t;
        cin>>t;
        
        while(t--)
        {
       
            int n;
            cin>>n;
            
            string rr[n];
        
           for(int i=0;i<n;i++)
           {
             cin>>rr[i];
           }
                   
           string max = rr[0];
            for(int j=1;j<n;j++)
            {
              if(rr[j]>max)
              {
                 max=rr[j];
                
              }
            }
        
        cout<<"Longest name in the string is:"<<max<<endl;
        }
 
    return 0;
}
2 // no of test case
6 // no of elements in string
Harsh
Gaurav
GauravMiglani
HarshAgarwal
GeeksforGeeksGeeks
Programmiz


Longest name in the string is:Programmiz

【问题讨论】:

  • 你好。欢迎来到堆栈溢出。根据它的编码方式,这是来自某个编码竞赛/平台吗?如果您添加了链接,将会很有帮助。此外,您可能想详细说明这个问题。目前尚不清楚“最长的名字”是什么意思。另外,在示例中,应该有 2 个测试用例,但只提到了一个
  • 您显示的是实际输出还是预期输出?描述程序应该做什么的细节。请注意,正如所写,您的程序甚至无法读取您显示的输入中的第二个整数。
  • if(rr[j]&gt;max) ==> if(rr[j].length() &gt; max.length())
  • @AbhayAravinda 抱歉,我从未使用过 stackflow。这是链接。 practice.geeksforgeeks.org/problems/display-longest-name/0/…
  • @paddy 这是实际输出。

标签: c++ string algorithm loops max


【解决方案1】:

可变长度数组,如本代码 sn-p

int n;
cin>>n;

string rr[n];

不是标准的 C++ 功能。相反,您应该使用标准容器std::vector&lt;std::string&gt;

在这个if语句中

if(rr[j]>max)

你不是在比较字符串的长度。看来你的意思

if( rr[j].length() > max.length() )

变量tn 也应该有一个无符号整数类型,例如

unsigned int t = 0;
//...
unsigned int n = 0;

注意已经有标准算法std::max_element可以代替手动写循环使用。

例如

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

//...

std::vector<std::string> v( n );

//...

auto it = std::max_element( std::begin( v ), std::end( v ),
                            []( const auto &s1, const auto &s2 )
                            {
                                return s1.length() < s2.length();
                            } );
 
std::cout << "Longest name in the string is: " << *it << std::endl;

这是一个演示程序。

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

int main() 
{
    std::vector<std::string> v = 
    { 
        "one", "two", "three", "four", "vide", "six", "seven", "eight", "nine", "ten"
    };


    auto it = std::max_element( std::begin( v ), std::end( v ),
                            []( const auto &s1, const auto &s2 )
                            {
                                return s1.length() < s2.length();
                            } );
                            
    
    std::cout << "Longest name in the string is: " << *it << std::endl;
    
    return 0;
}

它的输出是

Longest name in the string is: three

即程序输出第一个最大长度的字符串。如果您需要找到具有最大长度的最后一个字符串,请使用反向迭代器,如下所示。

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

int main() 
{
    std::vector<std::string> v = 
    { 
        "one", "two", "three", "four", "vide", "six", "seven", "eight", "nine", "ten"
    };


    auto it = std::max_element( std::rbegin( v ), std::rend( v ),
                            []( const auto &s1, const auto &s2 )
                            {
                                return s1.length() < s2.length();
                            } );
                            
    
    std::cout << "Longest name in the string is: " << *it << std::endl;
    
    return 0;
}

现在程序输出是

Longest name in the string is: eight

【讨论】:

  • 嘿!非常感谢..你能给我一些资源来提升我在 cpp 中的技能吗?我只是一个初学者。
  • @sumitpatil 我也是 C++ 的初学者。我从来没有做过 C++ 程序员。:)
  • @sumitpatil 首先从网上下载 C++ 17 和 C++ 20 标准的工作草案。
  • 这是一个正确的链接。 eel.is/c++draft 好的。那么接下来呢?
  • 我不认为阅读 C++ 标准是一个完整的初学者学习 C++ 的最佳方式。他们可能没有发现那里的讽刺。
【解决方案2】:

试试这个代码!! 在字符串中查找最小和最大的单词

// CPP Program to find Smallest and 
// Largest Word in a String 
#include<iostream> 
#include<cstring> 
using namespace std; 

void minMaxLengthWords(string input, string &minWord, string &maxWord) 
{ 
    // minWord and maxWord are received by reference 
    // and not by value 
    // will be used to store and return output 
    int len = input.length(); 
    int si = 0, ei = 0; 
    int min_length = len, min_start_index = 0, max_length = 0, max_start_index = 0; 

    // Loop while input string is not empty 
    while (ei <= len) 
    { 
        if (ei < len && input[ei] != ' ') 
            ei++; 
        
        else
        { 
            // end of a word 
            // find curr word length 
            int curr_length = ei - si; 
        
            if (curr_length < min_length) 
            { 
                min_length = curr_length; 
                min_start_index = si; 
            } 
            
            if (curr_length > max_length) 
            { 
                max_length = curr_length; 
                max_start_index = si; 
            } 
            ei++; 
            si = ei; 
        } 
    } 
    
    // store minimum and maximum length words 
    minWord = input.substr(min_start_index, min_length); 
    maxWord = input.substr(max_start_index, max_length); 
} 

// Driver code 
int main() 
{ 
    string a = "GeeksforGeeks A Computer Science portal for Geeks"; 
    string minWord, maxWord; 
    minMaxLengthWords(a, minWord, maxWord); 
    
    // to take input in string use getline(cin, a); 
    cout << "Minimum length word: "
        << minWord << endl 
        << "Maximum length word: "
        << maxWord << endl; 
} 

【讨论】:

  • 当有人编写了一个程序并需要帮助时,仅仅编写一个不同的程序并说“你来了”而没有进一步的解释是完全没有帮助的。事实上,这个程序看起来像是取自另一个网站,而你没有引用它的来源。这可能侵犯了版权,或者至少违反了您同意的 Stack Overflow 条款。
  • 我完全理解你的意思。这个程序没有被复制。我试图解决来自 geeksforgeeks 的问题。这是问题practice.geeksforgeeks.org/problems/display-longest-name/0/… 的链接。如果发生了一些错误,我很抱歉,这是无意的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-20
  • 1970-01-01
  • 1970-01-01
  • 2021-02-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多