# OOP #1 ## Bài tập cho điểm ```c++ #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; long long a, b; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ cin >> a >> b; cout << a + b; return 0; } ``` ## Phân số nhưng khó hơn ```c++ #include <bits/stdc++.h> using namespace std; #define int long long typedef pair<int, int> pii; typedef vector<pii> vii; bool cmp(pii x, pii y){ return ((1.0*x.first / (1.0*x.second)) < (1.0*y.first/(1.0*y.second))); } vii v; signed main(){ ios::sync_with_stdio(0); cin.tie(nullptr); int n; cin >> n; for(int i = 0; i < n; ++i){ int x, y = 0; cin >> x; while(!y){ cin >> y; } v.push_back(make_pair(x, y)); } sort(v.begin(), v.end(), cmp); //cout << cmp(v[0], v[n - 1]) << '\n'; cout << v[0].first << '/' << v[0].second << '\n'; cout << v[n - 1].first << '/' << v[n - 1].second; } ``` ## Phân số nhưng khó hơn phiên bản cao cấp ```c++ #include <bits/stdc++.h> using namespace std; #define int long long typedef pair<int, int> pii; typedef vector<pii> vii; void same(pii &x, pii &y){ int ms = x.second * y.second / __gcd(x.second, y.second); x.first = x.first * (ms / x.second); y.first = y.first * (ms / y.second); x.second = ms; y.second = ms; } bool cmp(pii x, pii y){ same(x, y); return x.first < y.first; } vii v; signed main(){ ios::sync_with_stdio(0); cin.tie(nullptr); int n, k; cin >> n >> k; for(int i = 0; i < n; ++i){ int x, y; cin >> x >> y; v.push_back(make_pair(x, y)); } sort(v.begin(), v.end(), cmp); if(!(k > n || k == 0)){ cout << v[n - k].first << '/' << v[n - k].second << '\n'; cout << v[k - 1].first << '/' << v[k - 1].second; } } ``` ## Tìm ngày kế tiếp ```c++ #include <bits/stdc++.h> using namespace std; #define int long long class Date{ private: int day, month, year; public: Date(); Date(int d, int m, int y){ this -> day = d; this -> month = m; this -> year = y; } void incYear(){ ++year; } void incMonth(){ ++month; if(month == 13){ incYear(); month = 1; } } void incDay(){ ++day; if(((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && day > 31) || ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30) || (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) && day > 29) || (month == 2 && !((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) && day > 28)){ incMonth(); day = 1; } } void print(){ cout << day << '/' << month << '/' << year; } }; bool check(int d, int m, int y){ if(y<=0) return 0; else if(m>12||m<=0) return 0; else { if(d<=0) return 0; else if((m==4||m==6||m==9||m==11)&&d>30) return 0; else if((m==1||m==3||m==5||m==7||m==8||m==10||m==12)&&d>31) return 0; else if(((y%4==0&&y%100!=0)||y%400==0)&&m==2&&d>29) return 0; else if(m==2&&!((y%4==0&&y%100!=0)||y%400==0)&&d>28) return 0; else return 1; } } signed main(){ ios::sync_with_stdio(0); cin.tie(nullptr); int a, b, c; cin >> a >> b >> c; if(!check(a, b, c)){ cout << "ERROR"; return 0; } Date m(a, b, c); m.incDay(); m.print(); } ``` ## Anorld's cat map ```c++ #include <bits/stdc++.h> using namespace std; typedef vector<int> vi; const int N = 1e3 + 3; int n, k; vector<vi> v, f; pair<int, int> getPos(int x, int y){ return make_pair((2 * x + y) % n, (x + y) % n); } void nextConfig(vector<vi> &v){ vector<vi> ans(n, vi(n, 0)); for(int i = 0; i < n; ++i){ for(int j = 0; j < n; ++j){ pair<int, int> pos = getPos(i, j); ans[pos.first][pos.second] = v[i][j]; } } v = ans; } bool check(const vector<vi> &v){ for(int i = 0; i < n; ++i){ for(int j = 0; j < n; ++j){ if(v[i][j] != v[0][0]) return 0; } } return 1; } signed main(){ ios::sync_with_stdio(0); cin.tie(nullptr); cin >> n; v.resize(n, vi(n, 0)); f.resize(n, vi(n, 0)); for(int i = 0; i < n; ++i){ for(int j = 0; j < n; ++j){ cin >> v[i][j]; f[i][j] = v[i][j]; } } if(check(v)){ cout << 0; return 0; } do{ nextConfig(v); ++k; }while(v != f); cout << k; } ``` ## Phân số khó hơn phiên bản max ```c++ #include<bits/stdc++.h> using namespace std; int n; typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef vector<pii> vii; bool cmp(pii x, pii y){ return ((1.0*x.first / (1.0*x.second)) < (1.0*y.first/(1.0*y.second))); } bool cmpll(pll x, pll y){ return ((1.0*x.first / (1.0*x.second)) == (1.0*y.first/(1.0*y.second))); } void mul(pll &x, pii y){ x.first *= y.first; x.second *= y.second; } vii a; vii res; pll k; signed main(){ ios::sync_with_stdio(0); cin.tie(nullptr); cin >> n; for(int i = 0; i < n; ++i){ int x, y; cin >> x >> y; a.push_back(make_pair(x, y)); } sort(a.begin(), a.end(), cmp); cin >> k.first >> k.second; pair<long long, long long> ans; for(int i = 0; i < (1 << (n)); ++i){ pair<long long, long long> tmp = make_pair(1, 1); for(int j = 0; j < n; ++j) if(i & (1 << j)) mul(tmp, a[j]); if(cmpll(k, tmp)){ for(int j = 0; j < n; ++j){ if(i & (1 << j)){ cout << a[j].first << ' ' << a[j].second << '\n'; } } return 0; } } } ```