uva11429(生成随机数 期望)-LMLPHP

uva11429(生成随机数 期望)-LMLPHP

uva11429(生成随机数 期望)-LMLPHP

//
// main.cpp
// uva11429
//
// Created by New_Life on 16/8/4.
// Copyright © 2016年 chenhuan001. All rights reserved.
// #include <iostream>
#include <math.h>
#include <string>
#include <stdio.h>
#include <algorithm>
using namespace std; #define N 1010
#define eps 1e-12 long double g[N]; int main(int argc, const char * argv[]) {
int r,n;
while(scanf("%d%d",&r,&n) && !(r==&&n==))
{
for(int i=;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
g[i] = (long double)a/b;
}
long double sum = 1.0;
long double dr = ;
long double up = ;
long double ans = 1.0;
long double preans;
do
{
long double tmp=;
dr *= r;
up *= r;
for(int i=;i<n;i++)
{
long long tmpi = (long long)(g[i]*dr+eps);
g[i] -= (long double)tmpi/dr;
up += tmpi;
} preans = ans;
ans += (dr-up)/dr; //ans += sum;
}while(ans-preans>1e-);
printf("%.6Lf\n",ans);
}
return ;
}
/*
100 3
1 3 1 3 1 3
2 4
1 4 1 4 1 4 1 4
2 3
1 3 1 3 1 3
10 2
1 2 1 2
*/

//用long double 才过的...

04-14 09:25