[ZCMU OJ]1633: 酷酷的单词(遍历)

Description

输入一些仅由小写字母组成的单词。你的任务是统计有多少个单词是“酷”的,即每种字母出
现的次数都不同。
比如 ada 是酷的,因为 a 出现 2 次,d 出现 1 次,而 1 和 2 不同。再比如,banana 也是酷的,
因为 a 出现 3 次,n 出现 2 次,b 出现 1 次。但是,bbacccd 不是酷的,因为 a 和 d 出现的次数
相同(均为 1 次)。

Input

输入包含不超过 30 组数据。每组数据第一行为单词个数 n  (1<=n<=10000)。以下 n 行各包含一
个单词,字母个数为 1~30。

Output

对于每组数据,输出测试点编号和酷单词的个数。

Sample Input

2 ada bbacccd 2 illness a

Sample Output

Case 1: 1 Case 2: 0

_____________________________________________________________________________

利用自定义函数判断是否为“酷酷的数”,统计每个单词中字母出现次数,再进行遍历。最后输出记得注意输出中的空格。

#include<bits/stdc++.h>
using namespace std;
bool cool(char a[10001])
{
	int i,j;
	int zimu[30]={0};//26个字母出现次数(数组稍微开大一丢丢) 
	char maxc;
	for(i=0;i<strlen(a);i++) 
	{
		zimu[a[i]-'a']++;
		if(a[i]>maxc)//找到出现过的最大字母 
		maxc=a[i];
	}
	
	for(i=0;i<=maxc-'a';i++)//遍历 
	{
		for(j=i+1;j<=maxc-'a';j++)
		{
			if(zimu[i]==zimu[j]&&zimu[i]!=0&&zimu[j]!=0)
			return false;
		}
	}
	return true;
}
int main()
{
	int t;
	int num=1;
	while(cin>>t)
	{
		int cnt_cool=0;
		for(int i=0;i<t;i++)
		{
			char x[10001];
			cin>>x;
			if(cool(x)==true&&strlen(x)!=1) //注意样例中单个字母的情况 
			cnt_cool++;
		}
		printf("Case %d: %d\n",num++,cnt_cool);//注意输出的空格 
		
		
		
	}
}

猜你喜欢

转载自blog.csdn.net/Solar_Zheng0817/article/details/124432122