C++刷题——2802: 推断字符串是否为回文

Description
编敲代码,推断输入的一个字符串是否为回文。

若是则输出“Yes”。否则输出“No”。

所谓回文是指順读和倒读都是一样的字符串。 Input Output Sample Input abcddcba Sample Output Yes


/* Copyright (c) 2014, 烟台大学计算机学院
 * All rights reserved.
 * 文件名:test.cpp
 * 作者:陈丹妮
 * 完毕日期:2015年 6 月 1 日
 * 版 本 号:v1.0
 */
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    char a[81];
    gets(a);
    int i,n=0,s=0;
    for(i=0; a[i]!=0; i++)
        n++;
    for(i=0; i<n/2; i++)
    {
        if(a[i]==a[n-i-1])
            s++;
    }
    if(s==n/2)
        cout<<"Yes"<<endl;
    else
        cout<<"No"<<endl;
    return 0;
}

 

心得体会:这次写的比上次写的简单多了。主要是想了个比較巧的方法,这就是进步!

                   所以做题前,先用数学思维想想有没有简单的办法解决,这样会简单的多!继续努力。!

posted on 2017-06-18 12:03  blfbuaa  阅读(405)  评论(0编辑  收藏  举报