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

 题意:

要求字符串只有a,v,i,n四种字母,每次随机取四个,问取得avin的概率
a,v,i,n的概率是a,v,i,n出现的次数/字符串的长度的四次方
但是输出结果时需要约分,例如2/256,你要输出1/128。

思路:

这道题我是先全部遍历一遍,用a、v、i、n分别计算出’a’、 ‘v’、 ‘i’、 'n’四个字符出现的次数。然后判断是否这四个字符都出现过,如果没有出现过,直接输出0/1,四个字符都出现过的话,就算出
一共四个位置;每个位置可以有n种放法: q = N*N*N*N,成功方案数:p = a*v*i*n;记得需要约分。

代码如下: 

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
char b[105];
int gcd(int a,int b)
{
    if (b==0)return a;
    return gcd(b,a%b);
}
int main()
{
	int N;
	while(cin>>N)
	{
		int a=0,v=0,i=0,n=0;//分别统计四个字母出现的次数
		for(int j=0;j<N;j++)//遍历统计 
		{
			cin>>b[j];
			if(b[j]=='a')
			{
				a++;
			}
			else if(b[j]=='v')
			{
				v++;
			}
			else if(b[j]=='i')
			{
				i++;
			}
			else if(b[j]=='n')
			{
				n++;
			}
		}
		
		if(N>=4)
		{
			if(a!=0&&v!=0&&i!=0&&n!=0)
			{
				int p=a*v*i*n,q=N*N*N*N;
				int sum=gcd(p,q);
				printf("%d/%d\n",p/sum,q/sum);
			}
			else printf("0/1\n");
		}
		else printf("0/1\n");
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值