杭电ACM1020 VS 1039

1020题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1020
1039题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1039
其实两个题是类似的,都是给一个字符串,检测连续的重复字符的子串。不过1039的附加条件更多
//1020的代码

#include<stdio.h>
#include<string.h>

char str[10005];

void main()
{
	int i;
	int n;
	char ch;
	int nLen;
	int count;
	scanf("%d",&n);
	getchar();
	while(n--)
	{
		gets(str);
		nLen = strlen(str);
		if(nLen < 1)
			continue;
		ch = str[0];
		count = 1;
		for(i = 1; i < nLen; ++i)
		{
			if(ch == str[i])
				++count;
			else
			{
				if(count > 1)
					printf("%d",count);
				printf("%c",ch);
				ch = str[i];
				count = 1;
			}
		}
		if(count > 1)
			printf("%d",count);
		printf("%c\n",ch);
	}
}

//1039题目中有三个条件。
1、字符串至少包含一个元音
2、不能出现三个以上连续的元音或辅音。
3、不能出现两个连续相同的字符,除了'ee','oo'之外。

#include<stdio.h>
#include<string.h>

bool IsVowel(char ch)
{
	if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
		return true;
	else
		return false;
}

void main()
{
	char str[22];
	int i;
	int nLen;
	int count;
	int flag;
	int flagV;

	while(gets(str))
	{
		nLen = strlen(str);
		if(nLen < 1)
			continue;
		if(strcmp(str, "end") == 0)
			break;

		count = 1;
		flag = IsVowel(str[0]) ? 1 : 0;//1表示元音,0表示辅音
		flagV = flag;//标记有没有出现过元音
		for(i = 1; i < nLen; ++i)
		{
			//上一次和这次都是元音      或者都是辅音
			if((flag && IsVowel(str[i])) || (!flag && !IsVowel(str[i])) )
				++count;
			else
			{
				flag ^= 1;
				count = 1;
				flagV = flagV | flag;
			}

			if(count >= 2)
			{
				if(count > 2 || (str[i] == str[i - 1] && str[i] != 'e' && str[i] != 'o'))
					break;
			}
		}
		printf("<%s> is ",str);
		if(i < nLen || !flagV)
			printf("not acceptable.\n");
		else
			printf("acceptable.\n");
		
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值