输入一长度为n的字符串,若其n为偶数,则将字符串从中间反转,若为奇数,则将前后各(n-1)/2个字符反转,中间字符不动。
/* All rights reserved.
* 文件名称:test.cpp
* 作者:陈丹妮
* 完成日期:2015年 5 月 21 日
* 版 本 号:v1.0
*/
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a;
while(cin>>a)
{
int i,n=0,s;
for(i=0; a[i]!='\0'; i++)
{
n++;
}
s=n/2;
for(i=s-1; i>=0; i--)
{
cout<<a[i];
}
if((n)%2!=0)
{
cout<<a[s];
for(i=n-1; i>s; i--)
{
cout<<a[i];
}
}
else
for(i=n-1; i>=s; i--)
{
cout<<a[i];
}
cout<<endl;
}
return 0;
}
心得体会:仔细看题目,还是希望能够更快更简便的写出程序出来!!继续努力,加油吧
原文:http://blog.csdn.net/nufangdongde/article/details/45890411