2021-05-23

F - String

Avin has a string. He would like to uniform-randomly select four characters (selecting the same character is allowed) from it. You are asked to calculate the probability of the four characters being ”avin” in order.
Input
The first line contains n (1 ≤ n ≤ 100), the length of the string. The second line contains the string. To simplify the problem, the characters of the string are from ’a’, ’v’, ’i’, ’n’.
Output
Print the reduced fraction (the greatest common divisor of the numerator and denominator is 1), representing the probability. If the answer is 0, you should output “0/1”.
Sample Input
4
avin
4
aaaa
Sample Output
1/256
0/1

在这里插入代码片
#include<stdio.h>
#include<math.h>
int gcd(int zi , int mu)
{
	if(mu==0){
		return zi;
	}
	else{
		return gcd(mu,zi%mu);
	}
}

int main()
{
	int n,i;
	char a[150];
	int fenzi;
	while(scanf("%d",&n)!=EOF)   //可以无限输入n 
	{
		int fenmu=1;
		int sum1=0;
		int sum2=0;
		int sum3=0;
		int sum4=0;
		
		scanf("%s",a);   //输入字符串 
		for(i=0;i<n;i++)  
		{
			if(a[i]=='a')  //记录字符串中字符'a'的个数 
			{
				sum1++;
			}
			if(a[i]=='v')  //记录字符串中字符'v'的个数 
			{
				sum2++;
			}
			if(a[i]=='i')   //记录字符串中字符'i'的个数 
			{
				sum3++;
			}
			if(a[i]=='n')    //记录字符串中字符'n'的个数 
			{
				sum4++;
			}
		}
		for(i=0;i<4;i++)   //总的种类数,由于是拿出4个,因此是4*4*4*4 
		{
			fenmu=fenmu*n;
		}
		fenzi=sum1*sum2*sum3*sum4;   //符合条件的种类数(字符串中相同字符当成两种)-----分子 
		if(fenzi!=0)    //字符串中有包含avin这四种字符 
		{
			if(gcd(fenzi,fenmu)!=0)  //判断是否需要约分,相当于判断分子分母是否有最大公约数 
			{
				printf("%d/%d\n",fenzi/gcd(fenzi,fenmu),fenmu/gcd(fenzi,fenmu));    //对分子分母进行约分操作 
			}
			else    //gcd(x,sum)==0的时候 ----没有最大公约数,也就是不需要约分的情况 
			{
				printf("%d/%d\n",fenzi,fenmu);
			}
		}
		else   //x==0的情况----也就是字符串中本来就没有凑齐avin这四种字符   
		{
			printf("0/1\n");  //则输出0/1 
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值