Posted on 2012-03-01 20:10
hoshelly 閱讀(1134)
評論(0) 編輯 收藏 引用 所屬分類:
Programming
- 描述
“回文”是一種特殊的數(shù)或者文字短語。他們無論是順讀還是倒讀,結(jié)果都一樣。例如:12321, 55555,45554。讀入一個5位整數(shù),判斷它是否是回文數(shù)。
- 輸入
多組測試數(shù)據(jù),每組一行,一個五位整數(shù),數(shù)據(jù)以0結(jié)尾。
- 輸出
對每組輸入數(shù)據(jù)輸出一行,如果輸入數(shù)是回文數(shù),輸出“Yes.” ,否則輸出 “No.” 。
- 樣例輸入
12345 12321 11111 0
- 樣例輸出
No. Yes. Yes.
源代碼如下,注意循環(huán)長度為(length/2+1)。
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
int main()
{
int n,length,i=0,c;
char str[6];
while(scanf("%d",&n)!=EOF)
{
if(n==0)
exit(1);
c=0;
sprintf(str,"%d",n);
length=strlen(str);
for(i=0;i<(length/2+1);i++)
{
if(str[i]==str[length-i-1])
c++;
else
break;
}
if(c==3)
printf("Yes.\n");
else
printf("No.\n");
}
return 0;
}