Skip to content

Commit e6c9eb4

Browse files
committed
.
1 parent 20c33ba commit e6c9eb4

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
2+
//------------------------------------------------------------------------------
3+
#include <iostream>
4+
#include <vector>
5+
// #include <bits/stdc++.h>
6+
// #include <cmath>
7+
// #include <algorithm>
8+
// #include <unordered_map>
9+
// #include <map>
10+
// #include <set>
11+
// #include <unordered_set>
12+
//------------------------------------------------------------------------------
13+
using namespace std;
14+
//------------------------------------------------------------------------------
15+
#define FastIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
16+
#define v(Type) vector<Type>
17+
#define w(T) \
18+
int T; \
19+
cin >> T; \
20+
while (T--)
21+
#define int long long int
22+
#define mod 1000000007ll
23+
#define endl "\n"
24+
//------------------------------------------------------------------------------
25+
// Any fucntion can be called using Math.function_name();
26+
//------------------------------------------------------------------------------
27+
class Math
28+
{
29+
public:
30+
//Returns gcd of two numbers
31+
int gcd(int a, int b)
32+
{
33+
return (a % b == 0) ? b : gcd(b, a % b);
34+
}
35+
36+
//Returns lcm of two numbers
37+
int lcm(int a, int b)
38+
{
39+
return a * (b / gcd(a, b));
40+
}
41+
42+
// Returns flag array isPrime
43+
// isPrime[i] = true (if i is Prime)
44+
// isPrime[i] = false (if i is not Prime)
45+
vector<bool> *seiveOfEratosthenes(const int N)
46+
{
47+
vector<bool> *isPrime = new vector<bool>(N + 1, true);
48+
(*isPrime)[0] = (*isPrime)[1] = false;
49+
for (int i = 2; i * i <= N; ++i)
50+
if ((*isPrime)[i])
51+
for (int j = i * i; j <= N; j += i)
52+
(*isPrime)[j] = false;
53+
54+
return isPrime;
55+
}
56+
57+
//Returns (x ^ n)
58+
int pow(const int &x, int n)
59+
{
60+
if (n == 0)
61+
return 1;
62+
int h = pow(x, n / 2);
63+
return (n & 1) ? h * h * x : h * h;
64+
}
65+
66+
//Returns (x ^ n) % M
67+
int pow(const int &x, int n, const int &M)
68+
{
69+
if (n == 0)
70+
return 1;
71+
int h = pow(x, n / 2) % M;
72+
return (n & 1) ? (h * h * x) % M : (h * h) % M;
73+
}
74+
75+
//Returns all Primes <= N
76+
vector<int> *primesUptoN(const int N)
77+
{
78+
vector<bool> *isPrime = seiveOfEratosthenes(N);
79+
vector<int> *Primes = new vector<int>;
80+
if (2 <= N)
81+
(*Primes).push_back(2);
82+
for (int i = 3; i <= N; i += 2)
83+
if ((*isPrime)[i])
84+
(*Primes).push_back(i);
85+
return Primes;
86+
}
87+
88+
} Math;
89+
//------------------------------------------------------------------------------
90+
void solve()
91+
{
92+
93+
}
94+
//------------------------------------------------------------------------------
95+
int32_t main()
96+
{
97+
FastIO;
98+
int test = 1;
99+
w(T)
100+
{
101+
cout << "Case #" << test++ << ": ";
102+
solve();
103+
}
104+
return 0;
105+
}
106+
//------------------------------------------------------------------------------
107+

Practice/temp2.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class Solution {
2+
public:
3+
string fractionToDecimal(int numerator, int denominator) {
4+
5+
if(numerator == 0){
6+
return "0";
7+
}
8+
9+
bool isPointIntroduced = false;
10+
string s = "";
11+
unordered_map<int,int> mp;
12+
bool numNeg = (numerator < 0);
13+
bool denNeg = (denominator < 0);
14+
15+
numerator = abs(numerator);
16+
denominator = abs(denominator);
17+
18+
while(1){
19+
20+
if(numerator < denominator){
21+
if(isPointIntroduced){
22+
numerator *= 10;
23+
s += '0';
24+
}else{
25+
if(s.size() == 0){
26+
s += '0';
27+
}
28+
s += '.';
29+
numerator *= 10;
30+
isPointIntroduced = true;
31+
}
32+
}else{
33+
34+
if(numerator % denominator == 0){
35+
s += to_string(numerator/denominator);
36+
if(numNeg^denNeg){
37+
s = '-' + s;
38+
}
39+
return s;
40+
}else if(mp.count(numerator) > 0){
41+
s.insert(mp[numerator],"(");
42+
s += ')';
43+
if(numNeg^denNeg){
44+
s = '-' + s;
45+
}
46+
return s;
47+
}else{
48+
mp[numerator] = s.length();
49+
s += to_string(numerator/denominator);
50+
numerator %= denominator;
51+
if(isPointIntroduced)
52+
numerator *= 10;
53+
}
54+
55+
}
56+
57+
}
58+
59+
}
60+
};

0 commit comments

Comments
 (0)