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
解题思路:这道题我是先全部遍历一遍,用a、v、i、n分别计算出’a’、 ‘v’、 ‘i’、 'n’四个字符出现的次数。然后判断是否这四个字符都出现过,如果没有出现过,直接输出0/1,四个字符都出现过的话,就算出
( a / N ) * ( v * N ) * ( i / N ) * ( n * N ),记得要化简(约分)。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=105;
char b[maxn];
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 ans=__gcd(p,q);
cout<<p/ans<<"/"<<q/ans<<endl;
}
else cout<<"0/1"<<endl;
}
else cout<<"0/1"<<endl;
}
return 0;
}