Algorithm
FAVDICE - Favorite Dice
BuggyD loves to carry his favorite die around. Perhaps you wonder why it's his favorite? Well, his die is magical and can be transformed into an N-sided unbiased die with the push of a button. Now BuggyD wants to learn more about his die, so he raises a question:
What is the expected number of throws of his die while it has N sides so that each number is rolled at least once?
Input
The first line of the input contains an integer t, the number of test cases. t test cases follow.
Each test case consists of a single line containing a single integer N (1 <= N <= 1000) - the number of sides on BuggyD's die.
Output
For each test case, print one line containing the expected number of times BuggyD needs to throw his N-sided die so that each number appears at least once. The expected number must be accurate to 2 decimal digits.
Example
Input: 2 1 12 Output: 1.00 37.24
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <map>
#define s(t) scanf("%d",&t);
using namespace std;
int main(){
int t;
s(t);
while(t--){
int n;
s(n);
double ans=0;
for(int i=1;i<=n;i++){
ans+=static_cast<double>(n)/i;
}
printf("%.2lf\n",ans);
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
1
12
Output
37.24
#2 Code Example with Java Programming
Code -
Java Programming
#include<stdio.h>
int main()
{
int t,n,i;
float s;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
s=0;
for(i=1;i<=n;i++)
s= s+ (float)1/(i);
s=(float)(s*n);
printf("%.2f\n",s);
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
1
12
Output
37.24
Demonstration
SPOJ Solution-FAVDICE Favorite Dice-- Solution in C, C++, Java, Python ,FAVDICE,SPOJ Solution