[Add] completed projects

This commit is contained in:
tiennm99 committed 2023-03-02 21:08:12 +07:00
1 parent df15f4da0b
commit 61473f02eb
12 files changed
+883

No files matched your search

BIN
View File
Binary file not shown.
+60
View File
@@ -0,0 +1,60 @@
#include <iostream>
#define MAX_SIZE 10
using namespace std;
int main()
{
int n;
cout << "Nhap n = ";
cin >> n;
while (cin.fail() || n < 0 || n > MAX_SIZE)
{
cin.clear();
cin.ignore(999, '\n');
cout << "Nhap khong thoa yeu cau! Vui long nhap lai: n = ";
cin >> n;
}
cout << endl;
int i, j, a = 0, chi_so_i = 0, chi_so_j = 0, chieu_dai = n - 1, mang[MAX_SIZE][MAX_SIZE];
while (a < n*n)
{
if (chieu_dai == 0)
{
mang[n/2][n/2] = ++a;
}
for (j = chi_so_j; j - chi_so_j < chieu_dai; j++)
{
mang[chi_so_i][j] = ++a;
}
chi_so_j = j;
for (i = chi_so_i; i - chi_so_i < chieu_dai; i++)
{
mang[i][chi_so_j] = ++a;
}
chi_so_i = i;
for (j = chi_so_j; chi_so_j - j < chieu_dai; j--)
{
mang[chi_so_i][j] = ++a;
}
chi_so_j = j;
for (i = chi_so_i; chi_so_i - i < chieu_dai; i--)
{
mang[i][chi_so_j] = ++a;
}
chi_so_j = j + 1;
chi_so_i = i + 1;
chieu_dai -= 2;
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cout << mang[i][j] << '\t';
}
cout << endl;
}
return 0;
}
BIN
View File
Binary file not shown.
+95
View File
@@ -0,0 +1,95 @@
#include <iostream>
#include <cmath>
using namespace std;
void xu_ly(int n)
{
int i, j, m = 0, vong = ceil(n/2), chi_so_i = 0, chi_so_j = 0, chieu_dai = n - 1, a[n][n], b[n][n];
bool chan = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
a[i][j] = ++m;
}
}
while (vong)
{
if (chan)
{
for (j = chi_so_j; j - chi_so_j < chieu_dai; j++)
{
b[chi_so_i][j] = a[chi_so_i][j + 1];
}
chi_so_j = j;
for (i = chi_so_i; i - chi_so_i < chieu_dai; i++)
{
b[i][chi_so_j] = a[i + 1][chi_so_j];
}
chi_so_i = i;
for (j = chi_so_j; chi_so_j - j < chieu_dai; j--)
{
b[chi_so_i][j] = a[chi_so_i][j - 1];
}
chi_so_j = j;
for (i = chi_so_i; chi_so_i - i < chieu_dai; i--)
{
b[i][chi_so_j] = a[i - 1][chi_so_j];
}
}
else
{
for (i = chi_so_i; i - chi_so_i < chieu_dai; i++)
{
b[i][chi_so_j] = a[i + 1][chi_so_j];
}
chi_so_i = i;
for (j = chi_so_j; j - chi_so_j < chieu_dai; j++)
{
b[chi_so_i][j] = a[chi_so_i][j + 1];
}
chi_so_j = j;
for (i = chi_so_i; chi_so_i - i < chieu_dai; i--)
{
b[i][chi_so_j] = a[i - 1][chi_so_j];
}
chi_so_i = i;
for (j = chi_so_j; chi_so_j - j < chieu_dai; j--)
{
b[chi_so_i][j] = a[chi_so_i][j - 1];
}
}
chi_so_i = i + 1;
chi_so_j = j + 1;
chieu_dai -= 2;
chan = !chan;
vong--;
if (chieu_dai == 0)
{
b[n/2][n/2] = a[n/2][n/2];
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cout << b[i][j] << '\t';
}
cout << endl;
}
}
int main()
{
int n;
cin >> n;
while (cin.fail() || n < 0)
{
cin.clear();
cin.ignore(999, '\n');
cin >> n;
}
xu_ly(n);
return 0;
}
BIN
View File
Binary file not shown.
+155
View File
@@ -0,0 +1,155 @@
#include <iostream>
#include <cmath>
using namespace std;
typedef struct
{
int tu, mau;
} phan_so;
void nhap(phan_so &x)
{
cout << "Tu = ";
cin >> x.tu;
while (cin.fail())
{
cin.clear();
cin.ignore(999, '\n');
cout << "Nhap loi! Tu = ";
cin >> x.tu;
}
cout << "Mau = ";
cin >> x.mau;
while (cin.fail() || x.mau == 0)
{
cin.clear();
cin.ignore(999, '\n');
cout << "Nhap loi! Mau = ";
cin >> x.mau;
}
}
int gcd(int a, int b)
{
a = abs(a);
b = abs(b);
if (a%b == 0)
{
return b;
}
else
{
if (b > a)
{
return gcd(b, a);
}
else
{
return gcd(b, a%b);
}
}
}
void toi_gian(phan_so &x)
{
int t = gcd(x.tu, x.mau);
x.tu /= t;
x.mau /= t;
if (x.mau < 0)
{
x.tu *= -1;
x.mau *= -1;
}
}
void xuat(phan_so x)
{
cout << x.tu;
if (x.mau != 1)
{
cout << "/" << x.mau;
}
}
phan_so cong(phan_so a, phan_so b)
{
phan_so t;
t.tu = a.tu*b.mau + a.mau*b.tu;
t.mau = a.mau*b.mau;
return t;
}
phan_so tru(phan_so a, phan_so b)
{
phan_so t;
t.tu = a.tu*b.mau - a.mau*b.tu;
t.mau = a.mau*b.mau;
return t;
}
phan_so nhan(phan_so a, phan_so b)
{
phan_so t;
t.tu = a.tu*b.tu;
t.mau = a.mau*b.mau;
return t;
}
phan_so chia(phan_so a, phan_so b)
{
phan_so t;
t.tu = a.tu*b.mau;
t.mau = a.mau*b.tu;
return t;
}
int main()
{
phan_so a, b, tong, hieu, tich, thuong;
cout << "Nhap tu va mau cua phan so a:" << endl;
nhap(a);
toi_gian(a);
cout << "Nhap tu va mau cua phan so b:" << endl;
nhap(b);
toi_gian(b);
cout << endl << "a = ";
xuat(a);
cout << endl << "b = ";
xuat(b);
cout << endl;
tong = cong(a, b);
toi_gian(tong);
cout << "a + b = ";
xuat(tong);
cout << endl;
hieu = tru(a, b);
toi_gian(hieu);
cout << "a - b = ";
xuat(hieu);
cout << endl;
tich = nhan(a, b);
toi_gian(tich);
cout << "a*b = ";
xuat(tich);
cout << endl;
if (b.tu != 0)
{
thuong = chia(a, b);
toi_gian(thuong);
cout << "a/b = ";
xuat(thuong);
cout << endl;
}
else
{
cout << "Phan so b = 0 nen khong the thuc hien phep chia!" << endl;
}
return 0;
}
BIN
View File
Binary file not shown.
+52
View File
@@ -0,0 +1,52 @@
#include <stdio.h>
#include <assert.h>
void DrawSpiralLine(const int n, const int line)
{
// Throw an assertion when faulty parameters are supplied.
assert(line >= 0 && line < n && n > 0);
const int square = n * n;
int i = 0;
// Simple case: The first line of a spiral.
if (line == 0)
{
for (i = 0; i < n; i++)
{
printf("%02d ", (square - 1) - i);
}
}
// Simple case: The last line of a spiral.
else if (line == (n - 1))
{
for (i = 0; i < n; i++)
{
printf("%02d ", square - (3 * n) + 2 + i);
}
}
// Complex case: Line with other inner spirals.
else
{
// Print the first character.
printf("%02d ", square - (4 * n) + 4 + (line - 1));
// Print a line of the rest
DrawSpiralLine(n - 2, line - 1);
// Print the last character
printf("%02d ", (square - 1) - n - (line - 1));
}
}
void DrawSpiral(const int n)
{
int i = 0;
// Draw the lines of the spiral.
for (i = 0; i < n; i++)
{
DrawSpiralLine(n, i);
printf("\n");
}
}
int main(void)
{
DrawSpiral(5);
printf("\n");
DrawSpiralLine(6, 2);
return 0;
}
BIN
View File
Binary file not shown.
+41
View File
@@ -0,0 +1,41 @@
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Nhap so phan tu cua mang: ";
cin >> n;
int arr[n][n] = {0};
for (int i = 0; i < n; i++)
{
cin >> arr[0][i];
}
for (int i = 1; i < n; i++)
{
for (int j = 0; j < n - i; j++)
{
arr[i][j] = arr[i - 1][j] + arr[0][i + j];
}
}
int max = arr[0][0], chi_so_i = 0, chi_so_j = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - i; j++)
{
if (arr[i][j] >= max)
{
max = arr[i][j];
chi_so_i = i;
chi_so_j = j;
}
}
}
for (int i = chi_so_j; i <= chi_so_i + chi_so_j; i++)
{
cout << arr[0][i] << "\t";
}
cout << endl << max << endl;
return 0;
}