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.
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.
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?
#include <iostream>
#include <sstream>
usingnamespace 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';
}
}