joj1008
1008: Go hang a salami, I
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
3s | 8192K | 8658 | 1612 | Standard |
Given a character string, determine if it is a palindrome. A palindrome is a word or phrase that reads the same forwards and backwards, like mom or noon. For our purposes, palindromes are case insensitive, so Bob and Anna are palindromes. Also, we are only concerned with alphabetic characters, so punctuation and other non-alphabetic characters may be ignored.
Input
A positive integer denoting the number of cases. Each case will consist of a string of characters, including blanks and non-alphabetic characters. Each case will be on a separate line.
Output
The sentence “<string> is a palindrome.” if the word is a palindrome, or the sentence“<string> is not a palindrome.” if it is not, where <string> is the input string.
Sample Input
5 Allen Alda asdffdsa Race car Go hang a salami, I'm a lasagna hog! Hanna
Sample Output
Allen Alda is not a palindrome. asdffdsa is a palindrome. Race car is a palindrome. Go hang a salami, I'm a lasagna hog! is a palindrome. Hanna is not a palindrome.
Notice: A string without alphabet should not be considered as a palindrome.
This problem is used for contest: 60
Submit / Problem List / Status / Discuss
刚开始做,WA了N次。觉得想法一点问题也没有,就是过不去。
后来发现是没考虑对于字符串里没有元素的情况(可是我觉得考虑这个情况没什么意义~~TT~~)
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <string.h>
4
5 bool ispalindrome(char a[]);
6
7 int main(void)
8 {
9
10 freopen("in.txt", "r", stdin);
11
12 char t[1000];
13 char a[1000];
14 int sample;
15
16 scanf("%d%*c", &sample);
17
18 for (int i=0; i<sample; i++)
19 {
20 gets(a);
21
22 int len = strlen(a);
23 a[len] = '\0';
24 int k = 0;
25 for (int j=0; j<len; j++)
26 {
27 if (isalpha(a[j]))
28 {
29 t[k++] = tolower(a[j]);
30 }
31 }
32 t[k] = '\0'; //注意不是t[k+1] = '\0'
33 if (ispalindrome(t))
34 {
35 printf("%s is a palindrome.\n", a);
36 }
37 else
38 {
39 printf("%s is not a palindrome.\n", a);
40 }
41 }
42
43
44
45 return 0;
46 }
47
48 bool ispalindrome(char a[])
49 {
50 int cnt = strlen(a);
51 if (0 == cnt)
52 {
53 return false;
54 }
55 for (int i=0, j=cnt-1; i<j; i++, j--)
56 {
57 if (a[i] != a[j])
58 {
59 return false;
60 }
61 }
62 return true;
63 }