init commit
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
class Sum
|
||||
{
|
||||
private:
|
||||
int a, b;
|
||||
|
||||
public:
|
||||
int
|
||||
get() const {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
Sum(int a, int b) : a(a), b(b) {};
|
||||
Sum(Sum sum, int b) : a(sum.get()), b(b) {};
|
||||
};
|
||||
Executable
+27
@@ -0,0 +1,27 @@
|
||||
#include <iostream>
|
||||
|
||||
class A {
|
||||
private:
|
||||
int x;
|
||||
bool to_print;
|
||||
public:
|
||||
A() {
|
||||
x = 0;
|
||||
to_print = false;
|
||||
}
|
||||
|
||||
A(const A& a) {
|
||||
std::cin >> x;
|
||||
int t;
|
||||
std::cin >> t;
|
||||
x += t;
|
||||
to_print = true;
|
||||
}
|
||||
|
||||
~A() {
|
||||
if (to_print) {
|
||||
std::cout << x << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
Executable
+36
@@ -0,0 +1,36 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cctype>
|
||||
|
||||
int main() {
|
||||
int c;
|
||||
c = getchar();
|
||||
|
||||
bool is_num = false;
|
||||
|
||||
while (c != EOF) {
|
||||
if (c == '0' && !is_num) {
|
||||
int old = c;
|
||||
c = getchar();
|
||||
|
||||
if (!(c == EOF || !isdigit(c))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
putchar(old);
|
||||
} else if (isdigit(c)) {
|
||||
is_num = true;
|
||||
} else {
|
||||
is_num = false;
|
||||
}
|
||||
|
||||
putchar(c);
|
||||
c = getchar();
|
||||
}
|
||||
|
||||
if (c != '\n') {
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Executable
+33
@@ -0,0 +1,33 @@
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
int x1_1, y1_1, x1_2, y1_2;
|
||||
int x2_1, y2_1, x2_2, y2_2;
|
||||
std::cin >> x1_1 >> y1_1 >> x1_2 >> y1_2;
|
||||
std::cin >> x2_1 >> y2_1 >> x2_2 >> y2_2;
|
||||
|
||||
double k1, k2;
|
||||
k1 = static_cast<double>(y1_2 - y1_1) / (x1_2 - x1_1);
|
||||
k2 = static_cast<double>(y2_2 - y2_1) / (x2_2 - x2_1);
|
||||
|
||||
if (k1 == k2) {
|
||||
if (y1_1 - k1 * x1_1 == y2_1 - k2 * x2_1) {
|
||||
std::cout << 2 << std::endl;
|
||||
} else {
|
||||
std::cout << 0 << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
double b1 = y1_1 - k1 * x1_1;
|
||||
double b2 = y2_1 - k2 * x2_1;
|
||||
|
||||
double x_intersect = (b2 - b1) / (k1 - k2);
|
||||
double y_intersect = k1 * x_intersect + b1;
|
||||
|
||||
std::cout.precision(6);
|
||||
std::cout << std::fixed << x_intersect << " " << y_intersect << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Executable
+19
@@ -0,0 +1,19 @@
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <cmath>
|
||||
|
||||
int main() {
|
||||
double tmp, arip = 0, squar = 0;
|
||||
int n = 0;
|
||||
while (std::cin >> tmp) {
|
||||
++n;
|
||||
arip += (tmp - arip) / n;
|
||||
squar += (tmp * tmp - squar) / n;
|
||||
}
|
||||
|
||||
std::cout << std::setprecision(10);
|
||||
|
||||
std::cout << arip << std::endl << std::sqrt(squar - arip * arip) << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
class C {
|
||||
public:
|
||||
static const char c;
|
||||
static void f() {};
|
||||
};
|
||||
|
||||
// const char C::c = '+';
|
||||
// int main() {
|
||||
// C ob;
|
||||
// C::f();
|
||||
// return 0;
|
||||
// }
|
||||
@@ -0,0 +1,42 @@
|
||||
class C
|
||||
{
|
||||
int x;
|
||||
public:
|
||||
C() { x = 0; }
|
||||
C(double tmp) { x = 0; }
|
||||
C(int tmp) { x = tmp; }
|
||||
C(const C o1, const C o2) { x = o1.x + o2.x; }
|
||||
C(const C* o) {}
|
||||
friend C
|
||||
operator+(const C& o1, const C& o2) {
|
||||
return C(o1.x + o2.x);
|
||||
}
|
||||
C
|
||||
operator++() {
|
||||
return C(++x);
|
||||
}
|
||||
int operator~() const { return ~x; }
|
||||
int
|
||||
operator*(const C* obj2) {
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
C
|
||||
func1(const C& v1, int v2) {
|
||||
return C(v2 + v1, ~v1);
|
||||
}
|
||||
|
||||
void
|
||||
func2(const C* p1, double p2) {
|
||||
C v1 = p2;
|
||||
C v2[2][2];
|
||||
C v3 = func1(func1(func1(&p1[3], p2), ~p1[2]), ++v1 * v2[1]);
|
||||
}
|
||||
|
||||
int
|
||||
main() {
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
class BinaryNumber
|
||||
{
|
||||
string str;
|
||||
public:
|
||||
BinaryNumber(const string& s = "0") {
|
||||
str = s;
|
||||
}
|
||||
operator string () const {
|
||||
return str;
|
||||
}
|
||||
const BinaryNumber& operator++() {
|
||||
std::string::iterator i = str.end() - 1;
|
||||
while (*i == '1' && i != str.begin()) {
|
||||
*i = '0';
|
||||
--i;
|
||||
}
|
||||
if (i == str.begin()) {
|
||||
str.insert(str.begin(), '1');
|
||||
} else {
|
||||
*i = '1';
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,56 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class Row
|
||||
{
|
||||
public:
|
||||
int* const cells = new int[3];
|
||||
|
||||
int*
|
||||
begin() const {
|
||||
return cells;
|
||||
}
|
||||
|
||||
int*
|
||||
end() const {
|
||||
return cells + 3;
|
||||
}
|
||||
|
||||
int& operator[](int ind) const {
|
||||
return cells[ind];
|
||||
}
|
||||
};
|
||||
|
||||
class Matrix
|
||||
{
|
||||
public:
|
||||
const Row r[3];
|
||||
|
||||
const Row*
|
||||
begin() const {
|
||||
return r;
|
||||
}
|
||||
|
||||
const Row*
|
||||
end() const {
|
||||
return r + 3;
|
||||
}
|
||||
|
||||
int&
|
||||
operator[](int ind1, int ind2) {
|
||||
return r[ind1][ind2];
|
||||
}
|
||||
};
|
||||
|
||||
// int
|
||||
// main() {
|
||||
// Matrix m;
|
||||
// m[1, 1] = 5;
|
||||
|
||||
// for (const auto& row : m) {
|
||||
// for (auto cell : row) {
|
||||
// cout << cell << " ";
|
||||
// }
|
||||
// cout << "\n";
|
||||
// }
|
||||
// }
|
||||
@@ -0,0 +1,81 @@
|
||||
#include <cmath>
|
||||
#include <string>
|
||||
|
||||
namespace numbers {
|
||||
class complex {
|
||||
double c_re, c_im;
|
||||
public:
|
||||
complex(double r = 0, double i = 0) : c_re(r), c_im(i) {}
|
||||
explicit complex(const std::string& s) {
|
||||
sscanf(s.c_str(), "(%lf,%lf)", &c_re, &c_im);
|
||||
}
|
||||
double re() const {
|
||||
return c_re;
|
||||
}
|
||||
double im() const {
|
||||
return c_im;
|
||||
}
|
||||
double abs2() const {
|
||||
return c_re * c_re + c_im * c_im;
|
||||
}
|
||||
double abs() const {
|
||||
return sqrt(abs2());
|
||||
}
|
||||
std::string to_string() const {
|
||||
char buf[100];
|
||||
sprintf(buf, "(%.10g,%.10g)", c_re, c_im);
|
||||
|
||||
return std::string(buf);
|
||||
}
|
||||
complex& operator+=(const complex& other) {
|
||||
c_re += other.c_re;
|
||||
c_im += other.c_im;
|
||||
|
||||
return *this;
|
||||
}
|
||||
complex& operator-=(const complex& other) {
|
||||
c_re -= other.c_re;
|
||||
c_im -= other.c_im;
|
||||
|
||||
return *this;
|
||||
}
|
||||
complex& operator*=(const complex& other) {
|
||||
double r = c_re * other.c_re - c_im * other.c_im;
|
||||
double i = c_re * other.c_im + c_im * other.c_re;
|
||||
c_re = r;
|
||||
c_im = i;
|
||||
|
||||
return *this;
|
||||
}
|
||||
complex& operator/=(const complex& other) {
|
||||
double r = (c_re * other.c_re + c_im * other.c_im) / other.abs2();
|
||||
double i = (c_im * other.c_re - c_re * other.c_im) / other.abs2();
|
||||
c_re = r;
|
||||
c_im = i;
|
||||
|
||||
return *this;
|
||||
}
|
||||
friend complex operator+(const complex& C, const complex& other) {
|
||||
return complex(C.c_re + other.c_re, C.c_im + other.c_im);
|
||||
}
|
||||
friend complex operator-(const complex& C, const complex& other) {
|
||||
return complex(C.c_re - other.c_re, C.c_im - other.c_im);
|
||||
}
|
||||
friend complex operator*(const complex& C, const complex& other) {
|
||||
return complex(C.c_re * other.c_re - C.c_im * other.c_im, C.c_re * other.c_im + C.c_im * other.c_re);
|
||||
}
|
||||
friend complex operator/(const complex& C, const complex& other) {
|
||||
double r = (C.c_re * other.c_re + C.c_im * other.c_im) / other.abs2();
|
||||
double i = (C.c_im * other.c_re - C.c_re * other.c_im) / other.abs2();
|
||||
|
||||
return complex(r, i);
|
||||
}
|
||||
};
|
||||
|
||||
complex operator-(const complex& C) {
|
||||
return complex(-C.re(), -C.im());
|
||||
}
|
||||
complex operator~(const complex& C) {
|
||||
return complex(C.re(), -C.im());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <cmc_complex.h>
|
||||
#include <cmc_complex_stack.h>
|
||||
#include <cmc_complex_eval.h>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
using namespace numbers;
|
||||
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
complex C(argv[1]);
|
||||
double R = std::stod(argv[2]);
|
||||
int N = std::stoi(argv[3]);
|
||||
std::vector<std::string> record;
|
||||
for (int i = 4; i < argc; i++) {
|
||||
record.push_back(argv[i]);
|
||||
}
|
||||
|
||||
complex I, z, next = C + R;
|
||||
double h = 2 * M_PI;
|
||||
double s = h / N;
|
||||
for (double i = 0; i < h; i += s) {
|
||||
z = next;
|
||||
next = C + R * complex(std::cos(i), std::sin(i));
|
||||
I += eval(record, (z + next) / 2) * (next - z);
|
||||
}
|
||||
|
||||
std::cout << I.to_string() << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
|
||||
void process(const std::vector<uint64_t>& from, std::vector<uint64_t>& into, int step) {
|
||||
auto iterf = from.begin();
|
||||
auto itert = into.rbegin();
|
||||
|
||||
while (iterf < from.end() && itert != into.rend()) {
|
||||
*itert += *iterf;
|
||||
++itert;
|
||||
iterf += step;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
|
||||
void process(std::vector<int64_t>& mas, int64_t limit) {
|
||||
auto itert = mas.end();
|
||||
int dst = 0;
|
||||
|
||||
for (auto it = mas.begin() + mas.size() - 1; it != mas.begin() - 1; --it) {
|
||||
dst = it - mas.begin();
|
||||
|
||||
if (*it >= limit) {
|
||||
mas.insert(itert, *it);
|
||||
itert = mas.end();
|
||||
}
|
||||
|
||||
it = mas.begin() + dst;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// #include <iostream>
|
||||
|
||||
// int main() {
|
||||
// std::vector<int64_t> mas = { 1, 4, 3, 2 };
|
||||
// int64_t limit = 3;
|
||||
|
||||
// process(mas, limit);
|
||||
|
||||
// for (const auto& val : mas) {
|
||||
// std::cout << val << " ";
|
||||
// }
|
||||
|
||||
// return 0;
|
||||
// }
|
||||
@@ -0,0 +1,22 @@
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
|
||||
void process(const std::vector<int>& mas1, std::vector<int>& mas2) {
|
||||
std::set<int> set1(mas1.begin(), mas1.end());
|
||||
|
||||
if (set1.empty() || mas2.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto it = mas2.end() - 1; it != mas2.begin() - 1; --it) {
|
||||
int dst = it - mas2.begin();
|
||||
|
||||
if (std::find(set1.begin(), set1.end(), dst) != set1.end()) {
|
||||
mas2.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
bool comp(unsigned int a, unsigned int b) {
|
||||
int res1 = 0;
|
||||
while (a != 0) {
|
||||
res1 += a & 1;
|
||||
a = a >> 1;
|
||||
}
|
||||
|
||||
int res2 = 0;
|
||||
while (b != 0) {
|
||||
res2 += b & 1;
|
||||
b = b >> 1;
|
||||
}
|
||||
|
||||
return res1 < res2;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
std::vector<unsigned int> v;
|
||||
unsigned int tmp;
|
||||
while (std::cin >> tmp) {
|
||||
v.push_back(tmp);
|
||||
}
|
||||
|
||||
std::stable_sort(v.begin(), v.end(), comp);
|
||||
|
||||
for (auto x : v) {
|
||||
std::cout << x << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
std::map<std::string, std::vector<int>> mp;
|
||||
|
||||
std::string name;
|
||||
int grade;
|
||||
while (std::cin >> name >> grade) {
|
||||
mp[name].push_back(grade);
|
||||
}
|
||||
|
||||
for (auto x : mp) {
|
||||
std::cout << x.first << " ";
|
||||
|
||||
double tmp = 0;
|
||||
int n = 0;
|
||||
for (auto i : x.second) {
|
||||
tmp += i;
|
||||
++n;
|
||||
}
|
||||
|
||||
std::cout << tmp / n << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#include <map>
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
long long mod = 4294967161;
|
||||
|
||||
std::map<std::pair<long long, long long>, long long> mp;
|
||||
|
||||
long long r, c, v;
|
||||
while (std::cin >> r >> c >> v) {
|
||||
if (!r && !c && v == mod) break;
|
||||
|
||||
mp[std::make_pair(r, c)] = v;
|
||||
}
|
||||
|
||||
while (std::cin >> r >> c >> v) {
|
||||
mp[std::make_pair(r, c)] += v;
|
||||
mp[std::make_pair(r, c)] %= mod;
|
||||
}
|
||||
|
||||
for (auto x : mp) {
|
||||
if (x.second) std::cout << x.first.first << " " << x.first.second << " " << x.second << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#include <map>
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
long long mod = 4294967161;
|
||||
|
||||
std::map<std::pair<long long, long long>, long long> mp;
|
||||
|
||||
long long r, c, v;
|
||||
while (std::cin >> r >> c >> v) {
|
||||
if (!r && !c && v == mod) break;
|
||||
|
||||
mp[std::make_pair(r, c)] = v;
|
||||
}
|
||||
|
||||
while (std::cin >> r >> c >> v) {
|
||||
// mp[std::make_pair(r, c)] *= v;
|
||||
mp[std::make_pair(r, c)] %= mod;
|
||||
}
|
||||
|
||||
for (auto x : mp) {
|
||||
if (x.second) std::cout << x.first.first << " " << x.first.second << " " << x.second << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
#include <iterator>
|
||||
|
||||
template <typename Container>
|
||||
typename Container::value_type process(const Container& container) {
|
||||
using ValueType = typename Container::value_type;
|
||||
|
||||
if (container.empty()) {
|
||||
return ValueType{};
|
||||
}
|
||||
|
||||
auto it = container.rbegin();
|
||||
ValueType sum = *it;
|
||||
|
||||
for (int i = 0; it != container.rend() && i < 5; ++i, ++it) {
|
||||
if (i == 2 || i == 4) {
|
||||
sum += *it;
|
||||
}
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// #include <iostream>
|
||||
// #include <vector>
|
||||
#include <iterator>
|
||||
|
||||
template <typename Container, typename Predicate>
|
||||
Container myfilter(const Container& container, Predicate pred) {
|
||||
Container result;
|
||||
|
||||
for (const auto& x : container) {
|
||||
if (pred(x)) {
|
||||
result.insert(result.end(), x);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#include <iterator>
|
||||
#include <functional>
|
||||
|
||||
template <typename ForwardIt, typename Compare = std::less<>>
|
||||
void selection_sort(ForwardIt first, ForwardIt last, Compare comp = Compare()) {
|
||||
for (auto fit = first; fit != last; ++fit) {
|
||||
auto min_it = fit;
|
||||
|
||||
for (auto sit = std::next(fit); sit != last; ++sit) {
|
||||
if (comp(*sit, *min_it)) {
|
||||
min_it = sit;
|
||||
}
|
||||
}
|
||||
|
||||
std::swap(*fit, *min_it);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Figure {
|
||||
public:
|
||||
virtual double get_square() const = 0;
|
||||
virtual ~Figure() {};
|
||||
};
|
||||
@@ -0,0 +1,58 @@
|
||||
#include <cmath>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
// class Figure {
|
||||
// public:
|
||||
// virtual double get_square() const = 0;
|
||||
// virtual ~Figure() {};
|
||||
// };
|
||||
|
||||
class Rectangle : Figure {
|
||||
double a;
|
||||
double b;
|
||||
public:
|
||||
Rectangle(double a = 0, double b = 0) : a(a), b(b) {}
|
||||
double get_square() const {
|
||||
return a * b;
|
||||
}
|
||||
static Rectangle* make(std::string s) {
|
||||
std::istringstream ss(s);
|
||||
double a, b;
|
||||
ss >> a >> b;
|
||||
|
||||
return new Rectangle(a, b);
|
||||
}
|
||||
};
|
||||
|
||||
class Square : Figure {
|
||||
double a;
|
||||
public:
|
||||
Square(double a = 0) : a(a) {}
|
||||
double get_square() const {
|
||||
return a * a;
|
||||
}
|
||||
static Square* make(std::string s) {
|
||||
std::istringstream ss(s);
|
||||
double a;
|
||||
ss >> a;
|
||||
|
||||
return new Square(a);
|
||||
}
|
||||
};
|
||||
|
||||
class Circle : Figure {
|
||||
double r;
|
||||
public:
|
||||
Circle(double r = 0) : r(r) {}
|
||||
double get_square() const {
|
||||
return M_PI * r * r;
|
||||
}
|
||||
static Circle* make(std::string s) {
|
||||
std::istringstream ss(s);
|
||||
double r;
|
||||
ss >> r;
|
||||
|
||||
return new Circle(r);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,36 @@
|
||||
class Figure {
|
||||
public:
|
||||
virtual bool equals(const Figure*) const = 0;
|
||||
virtual ~Figure() {}
|
||||
};
|
||||
|
||||
class Rectangle : Figure {
|
||||
int a;
|
||||
int b;
|
||||
public:
|
||||
Rectangle(int a = 0, int b = 0) : a(a), b(b) {}
|
||||
bool equals(const Figure* fig) const {
|
||||
const Rectangle* rect = dynamic_cast<const Rectangle*>(fig);
|
||||
if (rect) {
|
||||
return a == rect->a && b == rect->b;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
class Triangle : Figure {
|
||||
int a;
|
||||
int b;
|
||||
int c;
|
||||
public:
|
||||
Triangle(int a = 0, int b = 0, int c = 0) : a(a), b(b), c(c) {}
|
||||
bool equals(const Figure* fig) const {
|
||||
const Triangle* tri = dynamic_cast<const Triangle*>(fig);
|
||||
if (tri) {
|
||||
return a == tri->a && b == tri->b && c == tri->c;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
|
||||
class A {
|
||||
std::string s;
|
||||
public:
|
||||
A(const std::string& s) : s(s) {}
|
||||
~A() { std::cout << s << std::endl; }
|
||||
};
|
||||
|
||||
void f() {
|
||||
std::string s;
|
||||
|
||||
if (!(std::cin >> s)) {
|
||||
throw 0;
|
||||
}
|
||||
|
||||
A a(s);
|
||||
f();
|
||||
}
|
||||
|
||||
int main() {
|
||||
try {
|
||||
f();
|
||||
}
|
||||
catch (...) {
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
Некоторая рекурсивная функция func от трех целых аргументов a, b, k (a, b >= 1, k >= 0) определена следующим образом:
|
||||
|
||||
func(a, b, k) == a + b при k == 0
|
||||
func(a, b, k) == a при k > 0, b == 1
|
||||
func(a, b, k) == f(a, f(a, b - 1, k), k - 1) при k > 0, b > 1
|
||||
|
||||
На стандартном потоке ввода подаются тройки чисел: два 64-битных знаковых целых положительных числа a, b и 32-битное целое неотрицательное число k. Для каждой тройки чисел на стандартный поток вывода напечатайте значение описанной выше функции.
|
||||
|
||||
Параметры на входе будут таковы, что вычисление завершится за разумное время. Для вычислений достаточно 64-битного целого знакового типа.
|
||||
|
||||
Для возврата значения из рекурсии используйте исключения. Выход из рекурсивной функции с помощью явного или неявного return запрещен.
|
||||
|
||||
Напишите свой класс (например, Result) для передачи результата вычислений вместе с исключением. Не используйте выбрасывание исключений базовых (например, int) типов.
|
||||
|
||||
В комментарии в начале программы опишите, что из себя представляет эта рекурсивная функция.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
class Result {
|
||||
long long val;
|
||||
public:
|
||||
Result(long long val) : val(val) {}
|
||||
long long value() const { return val; }
|
||||
};
|
||||
|
||||
// Функция определена по условию. Выкидывает результат в виде exception
|
||||
void f(long long a, long long b, int k) {
|
||||
if (k == 0) {
|
||||
throw Result(a + b);
|
||||
}
|
||||
|
||||
if (b == 1) {
|
||||
throw Result(a);
|
||||
}
|
||||
|
||||
try {
|
||||
f(a, b - 1, k);
|
||||
}
|
||||
catch (const Result& result) {
|
||||
f(a, result.value(), k - 1);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
long long a, b;
|
||||
int k;
|
||||
while (std::cin >> a >> b >> k) {
|
||||
try {
|
||||
f(a, b, k);
|
||||
}
|
||||
catch (const Result& result) {
|
||||
std::cout << result.value() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
Реализуйте класс S следующим образом:
|
||||
|
||||
Класс хранит целое число (типа int).
|
||||
Число считывается со стандартного потока ввода в конструкторе.
|
||||
Число выводится на стандартный поток вывода в деструкторе.
|
||||
Следующая программа:
|
||||
|
||||
using namespace std;
|
||||
void func(S v)
|
||||
{
|
||||
if (v) {
|
||||
func(move(v));
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
func(S());
|
||||
}
|
||||
|
||||
считывает последовательность целых чисел и выводит на стандартный поток вывода их сумму.
|
||||
Если входная последовательность пустая, программа не выводит ничего.
|
||||
|
||||
Проверка на переполнение не требуется.
|
||||
|
||||
Глобальные переменные, static, mutable запрещены.
|
||||
|
||||
Сдаваемый на проверку класс должен подключать необходимые заголовочные файлы.
|
||||
Examples
|
||||
Input
|
||||
|
||||
1 2 3
|
||||
|
||||
Output
|
||||
|
||||
6
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class S {
|
||||
int v;
|
||||
int sum;
|
||||
bool last;
|
||||
bool first;
|
||||
public:
|
||||
S(S&& s) {
|
||||
int val;
|
||||
first = false;
|
||||
|
||||
if (std::cin >> val) {
|
||||
v = val;
|
||||
last = false;
|
||||
sum = s.sum + v;
|
||||
} else {
|
||||
last = true;
|
||||
sum = s.sum;
|
||||
}
|
||||
|
||||
}
|
||||
~S() {
|
||||
if (last && !first) {
|
||||
std::cout << sum << std::endl;
|
||||
}
|
||||
}
|
||||
S() : v(0), sum(0), last(true), first(true) {
|
||||
int val;
|
||||
|
||||
if (std::cin >> val) {
|
||||
v = val;
|
||||
last = false;
|
||||
sum = v;
|
||||
}
|
||||
}
|
||||
|
||||
operator bool() const {
|
||||
return !last;
|
||||
}
|
||||
};
|
||||
|
||||
// using namespace std;
|
||||
// void func(S v) {
|
||||
// if (v) {
|
||||
// func(move(v));
|
||||
// }
|
||||
// }
|
||||
|
||||
// int main() {
|
||||
// func(S());
|
||||
// }
|
||||
Reference in New Issue
Block a user