| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2840 人关注过本帖
标题:[求助]一道ACM题
只看楼主 加入收藏
perfect
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:81
专家分:0
注 册:2006-11-19
收藏
得分:0 

#include <stdio.h>
typedef struct
{
char a[21]; /* 存放字符串,最好不要超过20个*/
int i; /* 当前字符的位置 */
int n; /* 字符串的长度 */
}str;

str st;

main()
{
int k;
printf("please input a string : \n");
scanf("%s",st.a);
st.i=st.n=0;
while(st.a[st.n]!='\0')
st.n++;
st.n--;
judge();
if(st.i<st.n)
printf("NO\n");
else printf("YES\n");
}

int judge()
{
if(st.i>st.n) /* 如果大于字符串的长度,还没退出函数,则必是错误的句子 */
{
printf("NO\n"); exit(0);
}
if(st.a[st.i]>='p' && st.a[st.i]<='z')
{
st.i++;
return;
}
if(st.a[st.i]=='N')
{
while(st.a[st.i]=='N')
st.i++;
return;
}
switch(st.a[st.i])
{
case 'C': st.i++; judge(); judge(); break;
case 'D': st.i++; judge(); judge(); break;
case 'E': st.i++; judge(); judge(); break;
case 'I': st.i++; judge(); judge(); break;
default : printf("NO\n"); exit(0);
}
}


片言可以明百意 坐驰可以役万里
2006-11-22 10:59
cwande
Rank: 2
等 级:新手上路
威 望:3
帖 子:333
专家分:0
注 册:2006-8-18
收藏
得分:0 
以下是引用perfect在2006-11-22 9:53:05的发言:
这并不能保证两个句子是合法的 如果是 CpqDr 就不能识别出它是错的句子

可以的,不然你运行一下我的程序.......


汗,都懒得写代码了.......... cheat了一个威望,哈.....
2006-11-22 16:57
perfect
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:81
专家分:0
注 册:2006-11-19
收藏
得分:0 

之前没看到你的程序
原来你是倒着判断的啊


片言可以明百意 坐驰可以役万里
2006-11-22 22:14
perfect
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:81
专家分:0
注 册:2006-11-19
收藏
得分:0 
强啊,对于这个题目果然是个好方法

片言可以明百意 坐驰可以役万里
2006-11-22 22:39
perfect
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:81
专家分:0
注 册:2006-11-19
收藏
得分:0 
以下是引用perfect在2006-11-22 10:59:21的发言:

#include <stdio.h>
typedef struct
{
char a[21]; /* 存放字符串,最好不要超过20个*/
int i; /* 当前字符的位置 */
int n; /* 字符串的长度 */
}str;

str st;

main()
{
int k; // 多余的变量
printf("please input a string : \n");
scanf("%s",st.a);
st.i=st.n=0;
while(st.a[st.n]!='\0')
st.n++;
st.n--;
judge();
if(st.i<st.n) // 对最后st.i的值没有注意
printf("NO\n");
else printf("YES\n");
}

int judge()
{
if(st.i>st.n) /* 如果大于字符串的长度,还没退出函数,则必是错误的句子 */
{ // 不需要判断,因为最后的字符是'\0',属于非法字符
printf("NO\n"); exit(0);
}
if(st.a[st.i]>='p' && st.a[st.i]<='z')
{
st.i++;
return;
}
if(st.a[st.i]=='N')
{
while(st.a[st.i]=='N')
st.i++;
return; // 对N没有测试
}
switch(st.a[st.i])
{
case 'C': st.i++; judge(); judge(); break;
case 'D': st.i++; judge(); judge(); break;
case 'E': st.i++; judge(); judge(); break;
case 'I': st.i++; judge(); judge(); break;
default : printf("NO\n"); exit(0);
}
}

程序修改如下
只判断一个字符串,判断多个字符串,就要用到goto语句

#include <stdio.h>
typedef struct
{
char a[21];
int i; /* 当前字符的位置 */
int n; /* 字符串的长度 */
}str;

str st;

main()
{
printf("please input a string : \n");
scanf("%s",st.a);
st.i=st.n=0;
while(st.a[st.n]!='\0')
st.n++;
judge();
if(st.i<st.n)
printf("NO\n");
else printf("YES\n");
}

int judge()
{

if(st.a[st.i]>='p' && st.a[st.i]<='z')
{
st.i++;
return;
}

switch(st.a[st.i])
{
case 'N': while(st.a[st.i]=='N') st.i++; judge(); break;
case 'C': st.i++; judge(); judge(); break;
case 'D': st.i++; judge(); judge(); break;
case 'E': st.i++; judge(); judge(); break;
case 'I': st.i++; judge(); judge(); break;
default : printf("NO\n"); exit(0);
}
}

程序写的太急,没想到有这么多的错误.....
只是刚入门啊,继续学习中........


片言可以明百意 坐驰可以役万里
2006-11-26 10:14
快速回复:[求助]一道ACM题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030099 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved