String hdu-6572

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

因为是要挑出来四个字符看能组成avin的概率,所以只需要x=pow(n,4)就可以了

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
	int n;string ss;
	while(cin>>n>>ss)
	{
		long long aa=0,vv=0,ii=0,nn=0;long long z=0;long long x=0;
		x=pow(n,4);
		for(int i=0;i<n;i++)
		{
			if(ss[i]=='a') aa++;
			else if(ss[i]=='v') vv++;
			else if(ss[i]=='i') ii++;
			else if(ss[i]=='n') nn++;
		}
		if(aa==0||vv==0||ii==0||nn==0) z=0;	
		else z=aa*vv*ii*nn;
		long long zx=__gcd(z,x);
		if(zx)
		{
			z=z/zx;
			x=x/zx;
		}
		cout<<z<<"/"<<x<<endl;
	}
	
	return 0;
}
发布了31 篇原创文章 · 获赞 0 · 访问量 337

猜你喜欢

转载自blog.csdn.net/weixin_44828107/article/details/103000607