怎么用C++判断回数?

来源:百度知道 编辑:UC知道 时间:2024/06/30 16:05:39
比如ABCDCBA?如何用C++来判断?小弟刚学.

i++,j--然后判断是否相等。

双循环i++,j--

既然是c++,就不要用那么寻常的办法,用点稍微刺激的。就是将每位数字进入一个栈和一个队列,然后看出栈序和出队列序是否一样,代码如下。

#include <iostream>
#include <stack>
#include <queue>

using namespace std;

int main( )
{
char data[ 100 ];
int i;
queue<char> q;
stack<char> s;
cin >> data;
for ( i = 0; i < strlen( data ); i++ )
{
q.push( data[ i ] );
s.push( data[ i ] );
}
while ( !q.empty( ) )
{
if ( q.front( ) != s.top( ) )
{
cout << "No!";
break;
}
q.pop( );
s.pop( );
}
if ( q.empty( ) )
cout << "Yes!";
return 0;
}

用栈解决吧
入栈前,和出栈后的结果是不是一样就可以判断啦~

#include <iostream.h>
#include <string.h>
main()
{char *a=new char[20];
int b,c=0;
cin>>a;
if (strlen(a)%2==0)
for (b=1;b<=