如何编写正确的C ++ Palindrome代码? [英] How do I write a proper C++ Palindrome code?

查看:74
本文介绍了如何编写正确的C ++ Palindrome代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我只是想知道目前的逻辑回文是否有效,因为我已经看到很多其他方法来解决这类问题。所有我都在问我是否坚持使用这种方法(可能在尝试其他示例时遇到一些问题......)。



至于运行时间到目前为止它对我来说很好。



代码:

 #include< iostream> 
#include< string>
using namespace std;

int main()
{
int length = 0;
char输入[80];字符串res;
cout<<请输入一个字符串:;
cin.getline(输入,80);

for(length = 0; input [length]!='\ 0'; length ++);

for(int i = length - 1; i> = 0; i--)
{
res + = input [i];
}

if(input == res)
{
cout<< input<<is a palindrome!<< endl;
}
其他
{
cout<<输入<<不是痛苦的!<< endl;
}
返回0;
}

解决方案

这就是:如果用户输入超过80个字符会怎样?这是一个严重的问题,你必须解决它。使用 std :: string 类型,您根本不必使用 char [] 类型关于它。如果您选择使用 std :: string ,请输入它,而不是以null结尾的字符串。此外,对于 std :: string ,您不需要检查空字符:use string :: length()相反。请彻底查看此课程: http://www.cplusplus.com/reference/string/string [ ^ ]。



另外,你重复80两次。这是不可支持的。明确地将其定义为常量。



您可以使用并比较字符串值,但这效率不高。 (是的,我知道性能根本不重要,但是你不是为了学习编程吗?这是尝试做到最好的一个很好的理由。)这是你可以做的:使用常量字符串,只需比较一个循环中的两个字符,从最左边和最右边的字符开始,直到你到达中间。它应该适用于奇数和偶数长度,以及空字符串(空字符串是回文)。通过这种方式,您可以在首次出现差异时从循环中断,而无需检查所有字符。



-SA

Hello guys, I just wanted to know if the current logic palindrome is valid or not since I have seen so many other ways to solve such kind of problem. All I am asking if it is valid for me to stick with this method or not (maybe has some problems trying other examples...).

As for the runtime, it worked fine for me so far.

Code:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int length = 0;
    char input[80]; string res;
    cout<<"Please enter a string: ";
    cin.getline(input, 80);
    
    for (length = 0; input[length] != '\0'; length++);
    
    for (int i = length - 1; i >= 0 ; i--)
    {
        res += input[i];
    }
    
    if (input == res)
    {
        cout<<input<<" is a palindrome!"<<endl;
    }
    else
    {
        cout<<input<<" is NOT a paindrome!"<<endl;
    }
    return 0;
}

解决方案

This at this: what happens if a user enters more than 80 characters? This is a serious problem, you have to fix it. With the type std::string, you don't have to use the char[] type at all and care about it. If you choose to use std::string, input it, not null-terminated string. Also, for std::string, you don't need to check up null character: use string::length() instead. Please review this class thoroughly: http://www.cplusplus.com/reference/string/string[^].

Also, you repeat "80" twice. This is not supportable. Define it as a constant explicitly.

You can use and compare string values, but this is not efficient. (Yes, I understand that performance is not critical at all, but aren't you do it for learning programming? This is a good reason to try to do the very best.) Here is what you can do: using a constant string, just compare two characters in a loop, starting from leftmost and rightmost character, until you reach the middle. It should work for both odd and even lengths, as well as empty string (and empty string is a palindrome). This way, you can break from the loop at first discrepancy you account, without checking up all the characters.

—SA


这篇关于如何编写正确的C ++ Palindrome代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆