中办副主任马岳利2017:谁帮我编写这个程序?

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/07 16:15:25
回文问题:递归法判断所输入的一行字符是否回文.这里说的回文是指输入的一行字符,以”-”
为中心,其两边的字符是左右对称的.
例如
输入:ABCDE-EDCBA
输出:IT IS SYMMETRY.
用PASCAL语言写好吗?

这个问题如果不用递归倒还简单, 试一下递归.

program TestSym;

var
S: String;

function Sym(start: Integer): Boolean; { 测是否回文, start为左侧的字符的下标 }
var
tail: Integer; { tail为右侧字符的下标 }
begin
tail := Length(S) - start + 1;
if start > tail then { 个数为偶数了, 返回假 }
Sym := False
else if start = tail then { 中心, 看是否为 "-" }
Sym := S[start] = '-'
else Sym := (S[start] = S[tail]) and { 是否回文 }
Sym(start + 1); { 递归 }
end;

begin
Write('请输入字符: ');
Readln(S);
if (Length(S) >= 1) and Sym(1) then
Writeln('IT IS SYMMETRY!')
else Writeln('IT IS NOT SYMMETRY!');
end.

由于目前手边没有编译器, 没调试, 你自己调试一下.

#include "stdio.h"
main()
{char a[100];
int i,j,t;
t=0;
gets(a);
for(i=0,j=strlen(a)-1;i<j;i++,j--)
if(a[i]!=a[j])
t=1;
if(t=0)
printf("It is symmetry.");
}