0% found this document useful (0 votes)
7 views31 pages

LTNC25 OnTap

The document contains multiple C++ code snippets demonstrating various programming concepts such as function overloading, operator overloading, templates, recursion, and data structures like arrays and stacks. Each snippet includes a main function that performs specific tasks such as calculating areas, manipulating coordinates, finding maximum values, and converting numbers. The code is structured to handle input and output, often using standard input and output streams.

Uploaded by

gooddoggy1509
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views31 pages

LTNC25 OnTap

The document contains multiple C++ code snippets demonstrating various programming concepts such as function overloading, operator overloading, templates, recursion, and data structures like arrays and stacks. Each snippet includes a main function that performs specific tasks such as calculating areas, manipulating coordinates, finding maximum values, and converting numbers. The code is structured to handle input and output, often using standard input and output streams.

Uploaded by

gooddoggy1509
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

#include <iostream>

#include <iomanip>
using namespace std;

double dienTich(double r) {
return 3.14 * r * r;
}

double dienTich(double length, double width) {


return length * width;
}

int main() {
float r, length, width;
cin >> r >> length >> width;

cout << fixed << setprecision(2);


cout << dienTich(r) << endl;
cout << dienTich(length, width);

return 0;
}
---------------------------------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
using namespace std;

struct ToaDo {
double x, y;
};
istream &operator >> (istream &in, ToaDo &d) {
in >> d.x >> d.y;
return in;
}

ostream &operator << (ostream &out, ToaDo d) {


out << fixed << setprecision(2);
out << "(" << d.x << ", " << d.y << ")";
return out;
}

main() {
ToaDo a;
cin >> a;
cout << a;
return 0;
}
---------------------------------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
using namespace std;

struct ToaDo {
double x, y;
};

istream &operator >> (istream &in, ToaDo &d) {


in >> d.x >> d.y;
return in;
}
ostream &operator << (ostream &out, ToaDo d) {
out << fixed << setprecision(2);
out << "(" << d.x << ", " << d.y << ")";
return out;
}

ToaDo operator + (ToaDo a, ToaDo b) {


ToaDo c;
c.x = a.x + b.x;
c.y = a.y + b.y;
return c;
}

ToaDo operator - (ToaDo a, ToaDo b) {


ToaDo c;
c.x = a.x - b.x;
c.y = a.y - b.y;
return c;
}

main() {
ToaDo a, b;
cin >> a >> b;
cout << (a + b) << endl << (a - b);
return 0;
}
---------------------------------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
using namespace std;
struct ToaDo {
double x, y;
};

istream &operator >> (istream &in, ToaDo &d) {


in >> d.x >> d.y;
return in;
}

ostream &operator << (ostream &out, ToaDo d) {


out << fixed << setprecision(2);
out << "(" << d.x << ", " << d.y << ")";
return out;
}

ToaDo operator + (ToaDo a, ToaDo b) {


ToaDo c;
c.x = a.x + b.x;
c.y = a.y + b.y;
return c;
}

ToaDo operator - (ToaDo a, ToaDo b) {


ToaDo c;
c.x = a.x - b.x;
c.y = a.y - b.y;
return c;
}

bool operator < (ToaDo a, ToaDo b) {


if (a.x < b.x)
return true;
if (a.x == b.x && a.y < b.y)
return true;
return false;
}

bool operator > (ToaDo a, ToaDo b) {


if (a.x > b.x)
return true;
if (a.x == b.x && a.y > b.y)
return true;
return false;
}

main() {
ToaDo a, b;
cin >> a >> b;

if (a < b)
cout << "nho hon";
else if (a > b)
cout << "lon hon";
else
cout << "trung nhau";

return 0;
}
---------------------------------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
using namespace std;

struct ToaDo {
double x, y;
};

istream &operator >> (istream &in, ToaDo &td) {


in >> td.x >> td.y;
return in;
}

ostream &operator << (ostream &out, ToaDo td) {


out << fixed << setprecision(2);
out << '(' << td.x << ", " << td.y << ')';
return out;
}

ToaDo operator + (ToaDo a, ToaDo b) {


ToaDo res;
res.x = a.x + b.x;
res.y = a.y + b.y;
return res;
}

ToaDo operator - (ToaDo a, ToaDo b) {


ToaDo res;
res.x = a.x - b.x;
res.y = a.y - b.y;
return res;
}
bool operator > (ToaDo a, ToaDo b) {
if (a.x > b.x)
return true;

if (a.x == b.x && a.y > b.y)


return true;

return false;
}

bool operator < (ToaDo a, ToaDo b) {


if (a.x < b.x)
return true;

if (a.x == b.x && a.y < b.y)


return true;

return false;
}

// Tien to
ToaDo operator ++ (ToaDo &td) {
td.x += 1;
td.y += 1;
return td;
}

// Hau to
ToaDo operator ++ (ToaDo &td, int) {
ToaDo prev = td;
td.x += 1;
td.y += 1;
return prev;
}

// Tien to
ToaDo operator -- (ToaDo &td) {
td.x -= 1;
td.y -= 1;
return td;
}

// Hau to
ToaDo operator -- (ToaDo &td, int) {
ToaDo prev = td;
td.x -= 1;
td.y -= 1;
return td;
}

int main() {
ToaDo a;
cin >> a;

ToaDo b = a++;
cout << a << endl;
cout << --b;

return 0;
}
---------------------------------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
using namespace std;

struct ToaDo {
double x, y;
};

istream &operator >> (istream &in, ToaDo &td) {


in >> td.x >> td.y;
return in;
}

ostream &operator << (ostream &out, ToaDo td) {


out << fixed << setprecision(2);
out << '(' << td.x << ", " << td.y << ')';
return out;
}

ToaDo operator + (ToaDo a, ToaDo b) {


ToaDo res;
res.x = a.x + b.x;
res.y = a.y + b.y;
return res;
}

ToaDo operator - (ToaDo a, ToaDo b) {


ToaDo res;
res.x = a.x - b.x;
res.y = a.y - b.y;
return res;
}
bool operator > (ToaDo a, ToaDo b) {
if (a.x > b.x)
return true;

if (a.x == b.x && a.y > b.y)


return true;

return false;
}

bool operator < (ToaDo a, ToaDo b) {


if (a.x < b.x)
return true;

if (a.x == b.x && a.y < b.y)


return true;

return false;
}

// Tien to
ToaDo operator ++ (ToaDo &td) {
td.x += 1;
td.y += 1;
return td;
}

// Hau to
ToaDo operator ++ (ToaDo &td, int) {
ToaDo prev = td;
td.x += 1;
td.y += 1;
return prev;
}

// Tien to
ToaDo operator -- (ToaDo &td) {
td.x -= 1;
td.y -= 1;
return td;
}

// Hau to
ToaDo operator -- (ToaDo &td, int) {
ToaDo prev = td;
td.x -= 1;
td.y -= 1;
return td;
}

struct Array {
int size;
ToaDo data[10];

ToaDo &operator [] (int idx) {


return data[idx];
}

ToaDo getMax() {
ToaDo res = data[0];
for (int i = 1; i < size; i++)
if (res < data[i])
res = data[i];
return res;
}

ToaDo sum() {
ToaDo res = data[0];
for (int i = 1; i < size; i++)
res = res + data[i];
return res;
}
};

istream &operator >> (istream &in, Array &a) {


in >> a.size;
for (int i = 0; i < a.size; i++)
in >> a[i];
return in;
}

int main() {
Array a;
cin >> a;
cout << a.getMax() << endl;
cout << a.sum();
return 0;
}
---------------------------------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
using namespace std;

template<typename T>
void timMax() {
T x, y, z;
cin >> x >> y >> z;

T ans = x;
if (ans < y) ans = y;
if (ans < z) ans = z;

cout << fixed << setprecision(2);


cout << ans;
}

int main() {
char kt;
cin >> kt;

if (kt == 'a')
timMax<int>();

if (kt == 'b')
timMax<double>();

if (kt == 'c')
timMax<char>();

return 0;
}
---------------------------------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
using namespace std;

template<typename T>
struct Array {
int size;
T data[100];

T tong() {
T ans = 0;
for (int i = 0; i < size; i++)
ans += data[i];
return ans;
}
};

template<typename T>
istream &operator >> (istream &in, Array<T> &a) {
a.size = 0;
while (in >> a.data[a.size])
a.size++;
return in;
}

template<typename T>
ostream &operator << (ostream &out, Array<T> a) {
out << fixed << setprecision(2);
out << a.tong();
return out;
}
int main() {
char kt;
cin >> kt;

if (kt == 'a') {
Array<int> a;
cin >> a;
cout << a;
}

if (kt == 'b') {
Array<double> b;
cin >> b;
cout << b;
}

return 0;
}
---------------------------------------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

bool cp(int n) {
return sqrt(n) == (int) sqrt(n);
}

int main() {
ifstream fin("input.txt");
int n = 0;
int a[100];

while (fin >> a[n]) n++;

for (int i = 0; i < n; i++)


if (cp(a[i]))
cout << a[i] << ' ';

fin.close();
return 0;
}
---------------------------------------------------------------------------------------------------
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;

bool check(int n) {
for (int i = 2; i*i <= n; i++)
if (n % i == 0)
return false;
return n > 1;
}

int main() {
freopen("output.txt", "w", stdout);

int n = 0;
int a[100];
while (cin >> a[n]) n++;
for (int i = 0; i < n; i++)
if (check(a[i]))
cout << a[i] << ' ';

return 0;
}
---------------------------------------------------------------------------------------------------
#include <iostream>
#include <fstream>
using namespace std;

int div(int n) {
int res = 0;

for (int i = 1; i <= n; i++)


if (n % i == 0)
res += i;

return res;
}

int main() {
ifstream fin("input.txt");
ofstream fout("output.txt");

int n = 0;
int a[100];

while (fin >> a[n]) n++;


int ans = 0;
for (int i = 0; i < n; i++)
if (div(ans) < div(a[i]))
ans = a[i];

fout << ans;

fin.close();
fout.close();
return 0;
}
---------------------------------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
using namespace std;

double laiSuat(int n) {
if (n == 0)
return 1;
return 1.07 * laiSuat(n - 1);
}

int main() {
int n;
cin >> n;
cout << fixed << setprecision(2) << laiSuat(n);
return 0;
}
---------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
int virus(int n) {
if (n == 0)
return 1;
return 2 * virus(n - 1);
}

int main() {
int n;
cin >> n;
cout << virus(n);
return 0;
}
---------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;

int sum(int n) {
if (n < 10)
return n;

return n % 10 + sum(n / 10);


}

int main() {
int n;
cin >> n;
cout << sum(n);
return 0;
}
---------------------------------------------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;

#define int long long

int Pow(int x, int n) {


if (n == 1)
return x;

return x * Pow(x, n - 1);


}

int S(int x, int n) {


if (n == 0)
return 1;

return Pow(x, n) + S(x, n - 1);


}

int32_t main() {
int x, n;
cin >> x >> n;
cout << S(x, n);
return 0;
}
---------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;

int A(int i) {
if (i == 0 || i == 1)
return i;

if (i % 2 == 0)
return A(i / 2);
else
return A(i / 2) + A(i / 2 + 1);
}

int main() {
int n;
cin >> n;

int ans = 0;
for (int i = 0; i <= n; i++)
ans = max(ans, A(i));

cout << ans;


return 0;
}
---------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;

int A(int i) {
if (i == 0 || i == 1)
return i;

if (i % 2 == 0)
return A(i / 2);
else
return A(i / 2) + A(i / 2 + 1);
}

int main() {
int n;
cin >> n;

int ans = 0;
for (int i = 0; i <= n; i++)
ans = max(ans, A(i));

cout << ans;


return 0;
}
---------------------------------------------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;

int C(int n, int k) {


if (k == 0 || k == n)
return 1;

return C(n - 1, k) + C(n - 1, k - 1);


}

int main() {
int n, k;
cin >> n >> k;
cout << C(n, k);
return 0;
}
---------------------------------------------------------------------------------------------------
#include <iostream>
#include <stack>
using namespace std;

int main() {
string s;
cin >> s;
stack<char> st;

for (int i = 0; i < s.size(); i++)


st.push(s[i]);

while (!st.empty()) {
cout << st.top();
st.pop();
}

return 0;
}
---------------------------------------------------------------------------------------------------
#include <iostream>
#include <stack>
using namespace std;

int main() {
int n;
cin >> n;
stack<int> st;

if (n == 0) {
cout << 0 << endl;
return 0;
}

while (n > 0) {
st.push(n % 2);
n /= 2;
}

while (!st.empty()) {
cout << st.top();
st.pop();
}

cout << endl;


return 0;
}
---------------------------------------------------------------------------------------------------
#include <iostream>
#include <stack>
#include <string>
using namespace std;

int main() {
string s;
cin >> s;
stack<char> st;

for (int i = 0; i < s.size(); i++) {


char c = s[i];

if (c == '(') {
st.push(c);
continue;
}

if (st.empty()) {
cout << "NO" << endl;
return 0;
}

st.pop();
}

if (st.empty())
cout << "YES" << endl;
else
cout << "NO" << endl;

return 0;
}
---------------------------------------------------------------------------------------------------
#include <iostream>
#include <queue>
using namespace std;

int main() {
int n, k;
cin >> n >> k;

int x;
queue<int> q;
for (int i = 0; i < n; i++) {
cin >> x;
q.push(x);
}

for (int i = 0; i < k; i++) {


x = q.front();
q.push(x);
q.pop();
}

while (!q.empty()) {
cout << q.front() << ' ';
q.pop();
}

return 0;
}
---------------------------------------------------------------------------------------------------
#include <iostream>
#include <vector>
using namespace std;

int main() {
int x;
vector<int> v;

while (cin >> x) {


v.push_back(x);
}

vector<int>::iterator it;
for (it = v.begin(); it != v.end(); it++)
cout << *it << ' ';
cout << endl;

vector<int>::reverse_iterator rit;
for (rit = v.rbegin(); rit != v.rend(); rit++)
cout << *rit << ' ';

return 0;
}
---------------------------------------------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;
int main (){
int x,k,l,n,q;
cin>>n>>q;
vector <int> v;
for (int i=0 ; i<n;i++){
cin>>x;
v.push_back (x);
}
for (int i=0;i<q;i++){
cin>>l;
if (l==1){
cin>>x>>k;
v.insert(v.begin()+k,x);
}
else if (l==2){
cin>>k;
v.erase(v.begin()+k);
}
else {
cin >> k;
cout<< v[k]<<endl;
}
}
}
---------------------------------------------------------------------------------------------------
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
int x;
vector<int> v;

while (cin >> x) {


v.push_back(x);
}

sort(v.begin(), v.end());

for (int i = 0; i < v.size(); i++)


cout << v[i] << ' ';

return 0;
}
---------------------------------------------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;
bool comp(int a, int b) {
return abs(a) > abs(b);
}

main() {
int x;
vector<int> v;

while (cin >> x)


v.push_back(x);

sort(v.begin(), v.end(), comp);

for (int i = 0; i < v.size(); i++)


cout << v[i] << ' ';

return 0;
}
---------------------------------------------------------------------------------------------------
#include <iostream>
#include <algorithm>
using namespace std;

bool chinhPhuong(int n) {
return sqrt(n) == (int) sqrt(n);
}

int main() {
int x;
vector<int> v;
while (cin >> x)
v.push_back(x);

cout << count_if(v.begin(), v.end(), chinhPhuong);


return 0;
}
---------------------------------------------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;

bool snt(int n) {
for (int i = 2; i*i <= n; i++)
if (n % i == 0)
return false;
return n > 1;
}

int main() {
int x;
vector<int> v;

while (cin >> x)


v.push_back(x);

sort(v.begin(), v.end());

vector<int>::iterator it = remove_if(v.begin(), v.end(), snt);


v.erase(it, v.end());

for (int i = 0; i < v.size(); i++)


cout << v[i] << ' ';
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy