Decimal-and-fraction problem

Hello,

I've been struggling with this problem for a few days. Hope you guys can help me out.

Problem: A pendulum is swinging. Assume in the 1st time it swings, the velocity count is 1. Everytime it swings, specifically in the i-th time, the velocity count increases by 1/(1+2+3+...+i) (pls don't care about physics-related stuff). Calculate the velocity of the pendulum in the i-th time it swings, in its reduced fraction form.

#in. First line contains T (number of test). For each next line, input one integer only.

#out. With each test, print out in a single line the numerator and denominator of the result in simplified fraction form with a space between.

Example : #in 2 1 3 #out 1 1 3 2

Code below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
#define ldb long double
int decimal_digits (ldb dec) // calculating number of decimal digits 
{
    string str = to_string(dec);
    int num = str.length();
    for (int i=num-1;i>=0;i--) {
        if (str[i]==0) {
            num--;
            if (str[i]!=0) break;
        }
    }
    return num-2;
}
int FractionSimplifier(int a, int b) // simplifying
{
    for (int i=b;i>=2;i--) {
        if (a%i == 0 && b%i == 0) {
            a/=i;
            b/=i;
        }
    }
    return a;
}
void Print(int a,int b) // execute 
{
    cout << FractionSimplifier(a,b) << ' ' << FractionSimplifier(b,a) << endl;
}
ldb velocity(int n) // velocity count
{
    ldb sum=0,veloc=0;
    for (int i=1;i<=n;i++) {
        sum+=i;
        veloc+=(1/sum);
    }
    return veloc;
}
int main () // main drive 
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t,n;
    cin >> t;
    for (int i=1;i<=t;i++) {
        cin >> n;
        if (n==1) {
            cout << "1" << ' ' << "1" << endl;
            continue;
        }
        ldb veloc = velocity(n);
        ldb dec_part = veloc-int(veloc);
        int dec_dig = decimal_digits(dec_part);
        int denom_res = pow(10,dec_dig);
        //in this line i've tried denom_res = 10*dec_dig but it didn't work 
        // either. pretty sure the code broke because of this but
        // i still don't know why 
        int l = int(dec_part*denom_res);
        int num_res = int(veloc)*denom_res + l;
        Print(num_res,denom_res);
    }
    return 0;
}
Last edited on
I would start with your function decimal_digits.
Write some tests and see if it works.
Also it would be helpful to add some comments to your code.
Finally enable all warnings on your compiler.
Ok, the velocity for i=3 is 3/2?

Is this how the velocity increases:
1
2
3
4
i  velocity
1  1/1
2  1/1 + 1/(1+2) = 4/3
3  4/3 + 1/(1+2+3) = 8/6+1/6 = 9/6 = 3/2

I don't see any decimals in this calculation.


If you are participating in some competitive programming challenge where you should demonstrate your skill and knowledge and where the problems are mainly about math and logic, then what does it tell about you if you use floating point values in this question?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <sstream>
using namespace std;

int hcf( int a, int b ) { return b == 0 ? a : hcf( b, a % b ); }

int main()
{
// istream &in = cin;
   istringstream in( "2 1 3" );

   int T, a, b, c, n;
   in >> T;
   while( T-- )
   {
      in >> n;
      a = 2 * n;   b = n + 1;   c = hcf( a, b );
      cout << a / c << ' ' << b / c << '\n';
   }
}

Thanks for your help. Now that I've solved it, I'm closing this now
Topic archived. No new replies allowed.