Условие
Дано вещественное число z и дробь x/y. Проверьте истинность неравенства z<x/y.
Для решения этой задачи необходимо перегрузить операцию «меньше» для вещественного числа и дроби. Обратите внимание, что менять местами аргументы и использовать операцию «больше» в данной задаче запрещено.
Формат входных данных
Во первой строке дается вещественное число z (∣∣z∣∣≤106), заданное с точностью не более 4 знаков после десятичной точки.
Во второй строке даются два целых числа x и y, разделенные символом «/» (∣∣x∣∣,∣∣y∣∣≤109, y≠0).
Формат выходных данных
Выведите «YES», если z<x/y, и «NO», если z≥x/y.
Примеры:
входные данныевыходные данные
2.5
5/2 | NO
-------------------------------------------------
3.5999
18/5 | YES
-------------------------------------------------
-100.5
-200000/1 | NO
C/C++
Решите, пожалуйста, задачу на c++
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class Frac {
public:
using vtype = long long;
Frac() : val({0, 1}) {}
Frac(const string& str) {
val = split(str, '/');
normalize();
}
string value()const { return to_string(val.first) + '/' + to_string(val.second); }
Frac absolute()const { return Frac(abs(val.first), val.second); }
Frac& operator-() {
val.first *= -1;
return *this;
}
private:
Frac(vtype a, vtype b) : val{ a, b } { normalize(); }
pair val;
void normalize() {
auto x = gcd(val);
if (x) {
val.first /= x;
val.second /= x;
if (val.second < 0) {
val.first *= -1;
val.second *= -1;
}
}
}
vtype gcd(pair v) {
v.first = abs(v.first);
if (v.first) {
v.second = abs(v.second);
while (v.first != v.second) {
if (v.first > v.second) swap(v.first, v.second);
v.second -= v.first;
}
}
return v.first;
}
pair split(const string& str, char delim) {
stringstream ss(str);
string token;
pair box;
getline(ss, token, delim);
box.first = stoll(token);
getline(ss, token);
box.second = stoll(token);
return box;
}
friend Frac operator-(const Frac& a, const Frac& b) {
const auto an = a.val.first * b.val.second;
const auto bn = b.val.first * a.val.second;
const auto abn = an - bn;
const auto abd = a.val.second * b.val.second;
return Frac(to_string(abn) + '/' + to_string(abd));
}
friend bool operator<(const Frac& a, const Frac& b) { return double(a.val.first) / a.val.second < double(b.val.first) / b.val.second; }
friend bool operator<(const Frac& a, const double b) { return double(a.val.first) / a.val.second < b; }
friend bool operator<(const double a, const Frac& b) { return a < double(b.val.first) / b.val.second; }
friend bool operator==(const Frac& a, const Frac& b) { return a.val.first == b.val.first && a.val.second == b.val.second; }
friend bool operator==(const Frac& a, const double b) { return double(a.val.first) / a.val.second == b; }
friend bool operator>(const Frac& a, const Frac& b) { return double(a.val.first) / a.val.second > double(b.val.first) / b.val.second; }
friend bool operator>(const Frac& a, const double b) { return double(a.val.first) / a.val.second > b; }
friend Frac operator%(const Frac& a, const Frac& b) {
auto xa = a.absolute();
auto xb = b.absolute();
if (xa < xb) return a;
auto x = xa - xb;
if (x == 0) return Frac(0, 1);
while (x > xb) x = x - xb;
return a.val.first > 0? x : -x;
}
friend Frac operator*(const Frac& a, const vtype b) { return Frac(a.val.first * b, a.val.second); }
friend Frac operator*(const vtype a, const Frac& b) { return Frac(b.val.first * a, b.val.second); }
friend Frac operator*(const Frac& a, const Frac& b) { return Frac(a.val.first * b.val.first, a.val.second * b.val.second); }
friend Frac operator/(const Frac& a, const Frac& b) { return Frac(a.val.first * b.val.second, a.val.second * b.val.first); }
friend Frac operator+(const Frac& a, const Frac& b) {
auto d = a.val.second * b.val.second;
auto n = a.val.first * b.val.second + b.val.first * a.val.second;
return Frac(n, d);
}
friend Frac operator+(const Frac& a, const vtype b) {
auto d = a.val.second;
auto n = a.val.first + b * a.val.second;
return Frac(n, d);
}
friend Frac operator+(const vtype b, const Frac& a) {
auto d = a.val.second;
auto n = a.val.first + b * a.val.second;
return Frac(n, d);
}
friend ostream& operator<<(ostream& out, const Frac& fr) { return out << fr.val.first << '/' << fr.val.second; }
friend istream& operator>>(istream& inp, Frac& fr) {
string str;
inp >> str;
fr.val = fr.split(str, '/');
fr.normalize();
return inp;
}
};
int main() {
double a;
Frac b;
cin >> a >> b;
puts(a < b ? "YES" : "NO");
system("pause > nul");
}
#include <string>
#include <sstream>
using namespace std;
class Frac {
public:
using vtype = long long;
Frac() : val({0, 1}) {}
Frac(const string& str) {
val = split(str, '/');
normalize();
}
string value()const { return to_string(val.first) + '/' + to_string(val.second); }
Frac absolute()const { return Frac(abs(val.first), val.second); }
Frac& operator-() {
val.first *= -1;
return *this;
}
private:
Frac(vtype a, vtype b) : val{ a, b } { normalize(); }
pair val;
void normalize() {
auto x = gcd(val);
if (x) {
val.first /= x;
val.second /= x;
if (val.second < 0) {
val.first *= -1;
val.second *= -1;
}
}
}
vtype gcd(pair v) {
v.first = abs(v.first);
if (v.first) {
v.second = abs(v.second);
while (v.first != v.second) {
if (v.first > v.second) swap(v.first, v.second);
v.second -= v.first;
}
}
return v.first;
}
pair split(const string& str, char delim) {
stringstream ss(str);
string token;
pair box;
getline(ss, token, delim);
box.first = stoll(token);
getline(ss, token);
box.second = stoll(token);
return box;
}
friend Frac operator-(const Frac& a, const Frac& b) {
const auto an = a.val.first * b.val.second;
const auto bn = b.val.first * a.val.second;
const auto abn = an - bn;
const auto abd = a.val.second * b.val.second;
return Frac(to_string(abn) + '/' + to_string(abd));
}
friend bool operator<(const Frac& a, const Frac& b) { return double(a.val.first) / a.val.second < double(b.val.first) / b.val.second; }
friend bool operator<(const Frac& a, const double b) { return double(a.val.first) / a.val.second < b; }
friend bool operator<(const double a, const Frac& b) { return a < double(b.val.first) / b.val.second; }
friend bool operator==(const Frac& a, const Frac& b) { return a.val.first == b.val.first && a.val.second == b.val.second; }
friend bool operator==(const Frac& a, const double b) { return double(a.val.first) / a.val.second == b; }
friend bool operator>(const Frac& a, const Frac& b) { return double(a.val.first) / a.val.second > double(b.val.first) / b.val.second; }
friend bool operator>(const Frac& a, const double b) { return double(a.val.first) / a.val.second > b; }
friend Frac operator%(const Frac& a, const Frac& b) {
auto xa = a.absolute();
auto xb = b.absolute();
if (xa < xb) return a;
auto x = xa - xb;
if (x == 0) return Frac(0, 1);
while (x > xb) x = x - xb;
return a.val.first > 0? x : -x;
}
friend Frac operator*(const Frac& a, const vtype b) { return Frac(a.val.first * b, a.val.second); }
friend Frac operator*(const vtype a, const Frac& b) { return Frac(b.val.first * a, b.val.second); }
friend Frac operator*(const Frac& a, const Frac& b) { return Frac(a.val.first * b.val.first, a.val.second * b.val.second); }
friend Frac operator/(const Frac& a, const Frac& b) { return Frac(a.val.first * b.val.second, a.val.second * b.val.first); }
friend Frac operator+(const Frac& a, const Frac& b) {
auto d = a.val.second * b.val.second;
auto n = a.val.first * b.val.second + b.val.first * a.val.second;
return Frac(n, d);
}
friend Frac operator+(const Frac& a, const vtype b) {
auto d = a.val.second;
auto n = a.val.first + b * a.val.second;
return Frac(n, d);
}
friend Frac operator+(const vtype b, const Frac& a) {
auto d = a.val.second;
auto n = a.val.first + b * a.val.second;
return Frac(n, d);
}
friend ostream& operator<<(ostream& out, const Frac& fr) { return out << fr.val.first << '/' << fr.val.second; }
friend istream& operator>>(istream& inp, Frac& fr) {
string str;
inp >> str;
fr.val = fr.split(str, '/');
fr.normalize();
return inp;
}
};
int main() {
double a;
Frac b;
cin >> a >> b;
puts(a < b ? "YES" : "NO");
system("pause > nul");
}
Похожие вопросы
- Решите 2 задачи на C++
- Решите пожалуйста задачу на языке Си!!!
- Задача по программированию. Решить на Python или C++
- Помогите решить задачу по C++!
- Нужно срочно решить задачу на C++
- Помогите решить задачу на C++
- Помогите решить задачу на C++.
- Задача по C++
- Помогите пожалуйста решить задачу на C ИЛИ C#
- Задача на C++ (Остатки).
000000.cpp:21:1: error: invalid use of template-name 'std::pair' without an argument list
pair val;
^~~~
000000.cpp:21:1: note: class template argument deduction is only available with -std=c++17 or -std=gnu++17
In file included from /usr/include/c++/8/bits/stl_algobase.h:64,
from /usr/include/c++/8/bits/char_traits.h:39,
from /usr/include/c++/8/ios:40,
from /usr/include/c++/8/ostream:38,
from /usr/include/c++/8/iostream:39,
from 000000.cpp:1:
/usr/include/c++/8/bits/stl_pair.h:208:12: note: 'template struct std::pair' declared here
struct pair
Там 23000 символов...