如何判斷回文(palindrome) ? (C/C++) (C) (STL)
2010-10-26 19:29
204 查看
Abstract
回文是學習C語言時,常出現的作業與考題。
Introduction
所謂回文,就事字串的第一個字元等於最後一個字元,第二個字元等於倒數第二個字元。
palindrome.c / C
/*
(C) OOMusou 2008 http://oomusou.cnblogs.com
Filename : palindrome.c
Compiler : Visual C++ 9.0 / Visual Studio 2008
Description : Demo how to test palindrome
Release : 10/27/2008 1.0
*/
#include <stdio.h>
#include <string.h>
int is_palindrome(char *s) {
int i, len;
len = strlen(s);
for(i = 0; i < (len /2); i++) {
if (s[i] != s[len-i-1])
return 0;
}
if (i >= len/2)
return 1;
else
return 0;
}
int main() {
char s[256];
while(1) {
gets(s);
if (is_palindrome(s))
printf("%s is palindrome\n", s);
else
printf("%s is not palindrome\n", s);
}
}
18行
for(i = 0; i < (len /2); i++) {
if (s[i] != s[len-i-1])
return 0;
}
依序檢查字串第一個字元是否等於最後一個字元,第二個字元是否等於倒數第二個字元,若發現任何一個字元不同,就不是回文。
感謝沐枫的提醒,在C++有更精簡的寫法。
palindrome.cpp / C++
/*
(C) OOMusou 2008 http://oomusou.cnblogs.com
Filename : palindrome.cpp
Compiler : Visual C++ 9.0 / Visual Studio 2008
Description : Demo how to test palindrome
Release : 10/30/2008 1.0
*/
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int is_palindrome(string s) {
return equal(s.begin(), s.begin() + s.length() /2, s.rbegin());
}
int main() {
string s;
while(1) {
cin >> s;
if (is_palindrome(s))
cout << s << " is palindrome" << endl;
else
cout << s << " is not palindrome" << endl;
}
}
17行
return equal(s.begin(), s.begin() + s.length() /2, s.rbegin());
使用STL的equal()演算法,一行就可以判斷是不是回文了。
回文是學習C語言時,常出現的作業與考題。
Introduction
所謂回文,就事字串的第一個字元等於最後一個字元,第二個字元等於倒數第二個字元。
palindrome.c / C
/*
(C) OOMusou 2008 http://oomusou.cnblogs.com
Filename : palindrome.c
Compiler : Visual C++ 9.0 / Visual Studio 2008
Description : Demo how to test palindrome
Release : 10/27/2008 1.0
*/
#include <stdio.h>
#include <string.h>
int is_palindrome(char *s) {
int i, len;
len = strlen(s);
for(i = 0; i < (len /2); i++) {
if (s[i] != s[len-i-1])
return 0;
}
if (i >= len/2)
return 1;
else
return 0;
}
int main() {
char s[256];
while(1) {
gets(s);
if (is_palindrome(s))
printf("%s is palindrome\n", s);
else
printf("%s is not palindrome\n", s);
}
}
18行
for(i = 0; i < (len /2); i++) {
if (s[i] != s[len-i-1])
return 0;
}
依序檢查字串第一個字元是否等於最後一個字元,第二個字元是否等於倒數第二個字元,若發現任何一個字元不同,就不是回文。
感謝沐枫的提醒,在C++有更精簡的寫法。
palindrome.cpp / C++
/*
(C) OOMusou 2008 http://oomusou.cnblogs.com
Filename : palindrome.cpp
Compiler : Visual C++ 9.0 / Visual Studio 2008
Description : Demo how to test palindrome
Release : 10/30/2008 1.0
*/
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int is_palindrome(string s) {
return equal(s.begin(), s.begin() + s.length() /2, s.rbegin());
}
int main() {
string s;
while(1) {
cin >> s;
if (is_palindrome(s))
cout << s << " is palindrome" << endl;
else
cout << s << " is not palindrome" << endl;
}
}
17行
return equal(s.begin(), s.begin() + s.length() /2, s.rbegin());
使用STL的equal()演算法,一行就可以判斷是不是回文了。
相关文章推荐
- (筆記) 如何判斷回文(palindrome) ? (C/C++) (C) (STL)
- 如何判断一个单向链表是否为回文链表(Palindrome Linked List)
- Palindrome Number (判定一个整数是否是回文)
- 如何使用STL PriorityQueue 优先队列
- iphone开发之C++和Objective-C混编 如何在xcode中用C++的STL
- [LeetCode] Palindrome Linked List 回文链表
- 如何在VC6.0中使用SGI-STL
- uval1297 Palindrome 后缀数组求最长回文字串,lcp,rmq
- 如何判断数字 回文数
- POJ 4617 Palindrome(最长回文子串)
- 如何列出陣列中大於n的所有元素? (C/C++) (STL)
- 如何导出标准模板库(STL)类的实例化和包含STL类对象数据成员的类
- [LeetCode]—Palindrome Partitioning II 回文分割,求最小分割数
- (原創) 如何使用for_each() algorithm? (C/C++) (STL)
- 【URAL 1297】Palindrome 最长回文子串
- URAL 1297 Palindrome(最长回文子串 )
- lintcode 中等题:Palindrome Linked List 回文链表
- URAL - 1297 Palindrome(后缀数组求最长回文子串)
- 【C++ STL应用与实现】12: 如何使用std::stack
- 如何选择STL容器