}}{}{{
你试下
英语不好还想学编程??逆天之路,不由分说!! 数学太差还想学编程??离经叛道,义无返顾!!
呵呵,这下试下!
#include <stdio.h>
#include <stdlib.h>
#define NUMBER 7
int panduan(char achar[], int n);
int duiying(char achar[], int n);
int main()
{
char achar[NUMBER], ch;
int i;
while(EOF != scanf("%c", &ch))/*判断结束*/
{
i = 0;
achar[0] = ch;
for(i = 1;;i ++)
{
scanf("%c", &achar[i]);
if(achar[i] == '\n')
break;
}
if(panduan(achar, i - 1) && duiying(achar, i - 1))
printf("true\n");
else
printf("false\n");
}
return 0;
}
int panduan(char achar[], int n)/*判断顺序是否正确*/
{
int i = (n + 1) / 2, j;
for(j = 0;j < i - 1;j ++)
if(((achar[j] - achar[j + 1] < 0) && (achar[j] == '(' && achar[j + 1] == '<')) || (achar[j] == '<' && achar[j + 1] == '('))
return 0;
return 1;
}
int duiying(char achar[], int n)/*判断是否对应*/
{
int i = (n + 1) / 2, j;
for(j = 0;j < i;j ++)
if(achar[n - j] - achar[j] != 1 || achar[n - j] - achar[j] != 2)/*判断这里出现了错误*/
return 0;
return 1;
}
比之前简短些
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int *a,i=0,sum=0;
char c[81];
gets(c);
while(c[++i]);
a=(int *)malloc(i*sizeof(int));
for(i=0;c[i];i++)
{
switch(c[i])
{
case '{':a[i]=4;break;
case '[':a[i]=3;break;
case '(':a[i]=2;break;
case '<':a[i]=1;break;
case '>':a[i]=-1;break;
case ')':a[i]=-2;break;
case ']':a[i]=-3;break;
case '}':a[i]=-4;break;
default:a[i]=0;sum=1;break;
}
if(sum==1)
break;
}
if(sum)
puts("false");
else
if(i&1)
puts("false");
else
{
for(i=1;c[i]&&a[i-1]>=a[i];i++);
if(!c[i])
{
sum=i;
for(i=0;i<sum/2&&a[i]==-a[sum-1-i];i++);
if(i==sum/2)
puts("True");
else
puts("false");
}
else
puts("false");
}
free(a);
return 0;
}
[此贴子已经被作者于2007-3-19 16:28:38编辑过]