LightOj 1234 Harmonic Number Solution



 Problem Link:  https://lightoj.com/problem/harmonic-number

Solution in C++: 

///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat  Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
///**********ALLAH IS ALMIGHTY************///
#include<bits/stdc++.h>
using namespace std;
#define R 0.57721566490 ///R is Euler's constant
double a[1000009];
int main()
{

    int t,x,ts,i;
    double ans,fans,s,n,k;
    ans = 0;
    for (i = 1; i <= 1000000; i++)
    {
        s+= 1.0 / i;
        a[i] =s;
    }
    cin>>t;
    for ( ts = 1; ts <= t; ts++)
    {
        cin>>x;
        if(x <= 1000000)
        {
            printf("Case %d: %.9lf\n", ts, a[x]);
        }
        else

        {
            /// finding harmonic sum formula
            n=x+.5;
            fans=log(n)+R;
            printf("Case %d: %.9lf\n", ts, fans);
        }

    }
}



No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.