- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这是我用 C++ 编写的用于检查回文单词的程序。它不以通常的方式工作(反转单词并检查是否相同),而是直接从头到尾检查每个单词;
madam、ada、hannah、racecar 是我试过的词,它们似乎是正确的。
#include <iostream>
std::string is_palindrome(std::string text)
{
int length=text.size(); //No. of characters
int index=text.size()-1; //No. of indexes
int i;
int x=0; //To store no of same character from both ends
for(i=0;i<=index;i++)
{
if(text[i]==text[index-i])
{x++;}
}
if(x==length) //If all characters are same form opp ends
{
return "true";
}
else
{
return "false";
}
}
int main() {
std::cout << is_palindrome("madam") << "\n";
std::cout << is_palindrome("happy") << "\n";
}
结果是正确的,我只是找不到这样的代码,所以只是想检查一下。
最佳答案
你的函数太复杂了。参数应该是常量引用类型,函数应该返回 bool 值。
下面是一个演示程序,展示了函数如何使用循环。
#include <iostream>
#include <iomanip>
#include <string>
bool is_palindrome( const std::string &s )
{
std::string::size_type i = 0, n = s.length();
while ( i < n / 2 && s[i] == s[n - i - 1] ) ++i;
return i == n / 2;
}
int main()
{
std::cout << std::boolalpha << is_palindrome( "madam" ) << "\n";
std::cout << std::boolalpha << is_palindrome( "happy" ) << "\n";
return 0;
}
它的输出是
true
false
您可以编写更短的函数,如下面的演示程序所示。但是该函数的效率低于使用循环的函数,因为它需要为临时字符串分配内存 std::string( std::rbegin( s ), std::rend( s ) )
#include <iostream>
#include <iomanip>
#include <string>
#include <iterator>
bool is_palindrome( const std::string &s )
{
return s == std::string( std::rbegin( s ), std::rend( s ) );
}
int main()
{
std::cout << std::boolalpha << is_palindrome( "madam" ) << "\n";
std::cout << std::boolalpha << is_palindrome( "happy" ) << "\n";
return 0;
}
程序输出与上图相同
true
false
关于c++ - 谁能检查这个 Palindrome c++ 代码是否正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56645433/
我正在解决这个问题 longest palindromic substring leetcode 上的问题,我遵循动态编程方法,创建了一个 n*n bool 表(我猜这也是此问题的标准解决方案)并成功
我是编程新手,所以请多多包涵。我目前正在研究函数逻辑。我正在尝试编写一个函数来查看传递的字符串是否为回文(真或假)。 这是我的代码, function palindrome(word){ if(w
题目地址:https://leetcode.com/problems/super-palindromes/description/ 题目描述 Let's say a positive intege
本文关键词:回文数,回文,题解,Leetcode, 力扣,Python, C++, Java 题目地址:https://leetcode.com/problems/palindrome-number
题目地址:https://leetcode.com/problems/palindrome-partitioning/description/ 题目描述 Given a string s, par
题目地址:https://leetcode.com/problems/valid-palindrome/description/ 题目描述 Given a string, determine if
题目地址:https://leetcode.com/problems/shortest-palindrome/description/ 题目描述 Given a string s, you are
题目地址:https://leetcode.com/problems/palindrome-pairs/description/ 题目描述 Given a list of unique words
题目地址:https://leetcode.com/problems/palindromic-substrings/description/ 题目描述 Given a string, your t
题目地址:https://leetcode.com/problems/longest-palindrome/open in new window Difficulty: Easy 题目描
该程序运行时不会引发任何异常,但无论输入如何,结果始终相同:““空白”是回文。”每次输入都是回文时,我只是想知道是否有人对为什么会发生这种情况有任何建议?下面是该程序的代码: class Palind
我是一名 Java 开发新手。我想用Java编写代码来计算段落中回文词的数量。 假设是:用户可以输入包含尽可能多的句子的段落。每个单词之间以空格分隔,每个句子之间以句点分隔,单词前后的标点符号将被忽略
我正在上一门编程入门类(class),通过 myProgrammingLab 将大量 Material 深入到我们的脑海中。我在递归的概念上遇到了一些麻烦……对我来说它有点被击中或错过了。这个特殊的问
#include using namespace std; void palindrome(char *s){ if (s[1] == 0) { return;
我的一般问题是如何弄清楚如何使用 DFS。这似乎是我知识的薄弱部分。我的想法很模糊,但当问题发生变化时,我经常会卡住。这让我很困惑。 对于这个问题,我卡在了如何用递归编写 DFS 上。给定一个字符串
最长回文子串,题解,leetcode, 力扣,python, C++, java 题目地址:https://leetcode.com/problems/longest-palindromic-sub
题目地址:https://leetcode.com/problems/palindrome-linked-list/#/descriptionopen in new window 题目描述 Giv
题目地址:https://leetcode.com/problems/longest-palindromic-subsequence/description/ 题目描述 Given a strin
这是一些代码,以“直接样式”确定列表是否是 n+1 比较中的回文 pal_d1 :: Eq a => [a] -> Bool pal_d1 l = let (r,_) = walk l l in r
这个问题在这里已经有了答案: String replace method is not replacing characters (5 个答案) 关闭 2 年前。 我正在努力理解我的代码对于这个 L
我是一名优秀的程序员,十分优秀!