You are on page 1of 35

Menu đầu tiên

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
char choice;
do
{
system("cls");// xuat hien lai lua chon ma khong can chay lai chuong trinh
cout << " ======= MENU ======= \n"
<< "a. Nhap so nguyen n > 0, tinh tong cac so chia het cho 5, tich cac so khong chia
het cho 5\n"
<< "b. Nhap so nguyen n, kiem tra xem n co phai la SNT khong\n"
<< "c. Nhap so nguyen x va y, tinh tong cac so le trong doan x den y\n"
<< "d. Nhap 3 so thuc, sap xep theo thu tu tang dan\n"
<< "x. Thoat!\n"
<< "--------------------\n"
<< ">> Lua chon:";
cin >> choice;
switch (choice)
{
case 'X':case 'x':
cout << "Cam on ban da su dung chuong trinh!\n";
return 0; // Ket thuc chuong trinh
case 'a': case'A':
cout << "=== Nhap so nguyen n > 0, tinh tong cac so chia het cho 5, tich cac so
khong chia het cho 5 ===\n";
{
int n;
do {
cout << "Nhap so nguyen n > 0:";
cin >> n;
if (n <= 0)
cout << "Vui long nhap lai so nguyen >0!\n";
} while (n <= 0);
int i = 1, tongCH5 = 0, tichKCH5 = 1, demtich = 0;
// Tinh tong va tich
while (i <= n)
{
if (i % 5 == 0)
{
tongCH5 += i;
i++;
}
else
{
demtich++;
tichKCH5 *= i;
i++;
}
}
cout << "Tong cac so chia het cho 5 tu 1 den " << n << "la:" << tongCH5
<< endl;
if (demtich != 0)
cout << "Tich cac so khong chia het cho 5 tu 1 den " << n <<
"la:" << tichKCH5 << endl;
else
cout << "Tich cac so chia het cho 5!" << endl;
}
break;
case 'b': case 'B':
cout << "=== Nhap so nguyen n, kiem tra xem n co phai la SNT khong cach 1===\
n";
{
int n;
do
{
cout << "Nhap so nguyen n:";
cin >> n;
if (n <= 0)
cout << "Vui long nhap lai so nguyen n!";
} while (n<=0);
int i = 1, SNT = 0;
do
{
if (n % i == 0) SNT++;
i++;
} while (i <= n);
if (SNT == 2)
cout << "KQ kiem tra:" << n << "la SNT" << endl;
else
cout << "KQ kiem tra:" << n << "khong la SNT" << endl;
}
break;
case 'p': case 'P':
cout << "=== Nhap so nguyen n, kiem tra xem n co phai la SNT khong cach 2===\
n";
{
int n;
cout << "Nhap so nguyen n > 0:";
cin >> n;
bool laSNT = true;
if (n < 2)
laSNT = false;
else
{
int tmp = sqrt(n);
int i = 2;
while (i <= tmp)
{
if (n % i == 0)
{
laSNT = false;
break;
}
i++;
}
}
cout << "KQ kiem tra:" << n << (laSNT ? "la" : "khong la") << "SNT\n";
}
break;
case 'c': case 'C':
cout << "=== Nhap so nguyen x va y, tinh tong cac so le trong doan x den y cach 1\
n";
{
int x, y, tongle = 0, tongchan = 0;
cout << "Nhap so nguyen x:"; cin >> x;
cout << "Nhap so nguyen y:"; cin >> y;
for (int i = x; i <= y; i++)
{
if (i % 2 != 0)
tongle += i;
else
tongchan += i;
}
cout << "KQ tong cac so le trong doan x den y la:" << tongle << endl;
cout << "KQ tong cac so chan trong doan x den y la:" << tongchan <<
endl;
/* cach nay co the lam ra duoc 2 truong hop tong le va tong chan nhung
nhuoc diem la phai
nhap x be hon y, neu nhap y be hon x se khong chay duoc chuong trinh*/
}
break;
case 'o': case'O':
cout << "=== Nhap so nguyen x va y, tinh tong cac so le trong doan x den y cach 2\
n";
{
int x, y;
cout << "Nhap x, y:";
cin >> x >> y;
int tmp, i, tongle = 0;
if (x > y)
{
tmp = x;
x = y;
y = tmp;
/* VD: nhap x=5, y=6
tmp la 5
x=y=6
y=tmp=5*/
}
i = x;
while (i <= y)
{
if (i % 2 != 0)
tongle += i;
i++;
}
cout << "KQ tong cac so le trong doan x va y la:" << tongle << endl;
/*Cach nay giup khi nhap x > y co the tu dao lai nhu x < y de co the phu
hop yeu cau de bai*/
}
break;
case 'd': case'D':
cout << "=== Nhap 3 so thuc, sap xep theo thu tu tang dan\n";
{
double a, b, c;
cout << "Nhap 3 so thuc:";
cin >> a >> b >> c;
// 5 4 3
// a b c
// 4 5 3
// 3 5 4
// 3 4 5
if (a >= b)
{
double tmp = a;
a = b;
b = tmp;
}
if (a >= c)
{
double tmp = a;
a = c;
c = tmp;
}
if (b >= c)
{
int tmp = b;
b = c;
c = tmp;
}
cout << "Thu tu tang dan cua 3 so la:" << a << "<" << b << "<" << c << "\
n";
}
break;
default:
cout << "Lua chon khong hop le!\n";
break; // dung chuong trinh output lua chon khong hop le khi chon sai!
}
system("pause");
} while (1);// xuat hien lai lua chon khi nguoi dung nhap sai lua chon
return 0;
}
Function of Hải Đăng office
#include <iostream>
#include <cmath>
using namespace std;
long S=0;
long SLUS(long N)
{
long dem=0;
for (int i=2;i<N;i++)
{
if (N%i==0) dem++;
}
return dem;
}
long Giaithua(long N)
{
long giaithua=1;
for (int i=1;i<=N;i++)
{
giaithua*=i;
}
return giaithua;
}
long Tohop(long n,long m)
{
long S;
S=Giaithua(n)/Giaithua(m)/Giaithua(n-m);
return S;
}
long sdn(long N)
{
long S=0;
while (N>0)
{
S=S*10+N%10;
N=N/10;
}
return S;
}
void Inchuoi(char KT, long SL, bool Xuonghang)
{
for (int i=1;i<=SL;i++)
{
cout<<KT;
}
if (Xuonghang) cout<<endl;
return;
}
long Nhapso(string S,long Min,long Max)
{
long n;
do
{
cout<<S;
cin>>n;
if (n<Min||n>Max)
cout<<"Nhap sai nhap lai\n";
} while (n<Min||n>Max);
return n;
}
int main()
{
long R=Nhapso("Nhap chieu R HCN:",1,10);
long D=Nhapso("Nhap chieu D HCN:",R,10);
for (int j=1;j<=R;j++)
{
Inchuoi('*',D,true);
}

/*
long k=Nhapso("Nhap 1 thang: ",1,12);
cout<<"So ban nhap:"<<k;

int h=7;
for (int i=1;i<=h;i++)
{
Inchuoi(' ',h-i,false);
//for (int j=i;j<=i+;j++)
//{
Inchuoi('*',i+(i-1),false);
//}
Inchuoi(' ',h-i,true);
}

int h=5;
for (int i=1;i<=h;i++)
{
Inchuoi(' ',h-i,false);
Inchuoi('@',i,true);
}

int h=5;
for (int i=h;i>0;i--)
{
Inchuoi('*',i,true);
}

int h=5;
for (int i=1;i<=h;i++)
{
Inchuoi('*',i,true);
}

long U=2000,V=3000;
for (long i=U;i<=V;i++)
{
if (sdn(i)==i) cout<<i<<endl;
}

long k=10000,dem=0;
for (long i=k;dem<=100;i++)
{
if (i==sdn(i))
{
cout<<i<<endl;
dem++;
}
}

int k;
cin>>k;
for (int j=0;j<=k;j++)
{
for (int i=0;i<=j;i++)
{
cout<<Tohop(j,i)<<" ";
}
cout<<endl;
}

double S=0,M;
cin>>M;
for (int i=1;i<=M;i++)
{
S+=1.0/Giaithua(i);
}
cout<<S;

long n=6,m=4;
long S;
S=Giaithua(n)/Giaithua(m)/Giaithua(n-m);
cout<<S;

cout<<"Nhap 1 so nguyen bat ky: ";


long k;
cin>>k;

//double kq=sqrt(double(k));
long M=SLUS(k);
if (M==0) cout<<"K la so nguyen to!";
else cout<<"K ko la so nguyen to!";

long A=100,B=500;
for (int i=A;i<=B;i++)
{
if (SLUS(i)==0) cout<<i<<endl;
}

int dem=0;
for (int i=2;;i++)
{
if (SLUS(i)==0)
{
cout<<i<<",";
dem++;
}
if (dem==100) break;
}
*/
system("pause");
return 0;
}
Menu2_2351010164_Nguyễn Hoàng Tấn Phúc
#include <iostream>
using namespace std;
const int SIZE = 50;
int main()
{
int choice, A[SIZE], n, i = 0;
bool init = false;
while (true)
{
system("cls");
cout << "=======MENU=======\n"
<< "1. Nhap gia tri cho mang\n"
<< "2. Xuat cac gia tri dang luu trong mang\n"
<< "3. Tinh tong cac so le, tich cac so chan trong mang (dung bien dem xu li truong
hop khong co so chan/ le trong mang)\n"
<< "4. Xoa phan tu tai vi tri bat ky trong mang\n"
<< "5. Sap xep cac gia tri trong mang theo thu tu giam dan\n"
<< "0. Thoat chuong trinh!\n"
<< "----------------------\n"
<< ">> Lua chon:";
cin >> choice;
switch (choice)
{
case 0:
cout << "Cam on ban da su dung chuong trinh!\n";
system("pause");
return 0;
case 1:
{
cout << "=== Nhap gia tri cho mang ===\n";
do
{
cout << "Nhap bao nhieu phan tu cho mang:";
cin >> n;
if (n <= 0 || n > SIZE)
cout << "Nhap lai so phan tu n!\n";
} while (n <= 0 || n > SIZE);
cout << "Nhap " << n << " so nguyen:\n";
for (int i = 0; i < n; i++)
{
cout << "Nhap vao phan tu A[" << i << "]:";
cin >> A[i];
}
init = true;
}
break;
case 2:
{
cout << "=== Xuat cac gia tri dang luu trong mang ===\n";
if (init)
{
cout << "Mang dang luu tru la:";
for (int i = 0; i < n; i++)
cout << A[i] << " ";
}
else
{
cout << "Mang chua duoc khoi tao!\n";
}
}
break;
case 3:
{
cout << "=== Tinh tong cac so le, tich cac so chan trong mang(dung bien dem xu li
truong hop khong co so chan / le trong mang) === \n";
if (init)
{
int tongle = 0, tichchan = 1, demle = 0, demchan = 0;
for (int i = 0; i < n; i++)
{
if (A[i] % 2 != 0)
{
tongle += A[i];
demle++;
}
else
{
tichchan *= A[i];
demchan++;
}
}
if (demle <= 0)
{
cout << "Khong co so le trong mang!\n";
}
else
{
cout << "Tong cac so le:" << tongle << endl;
}
if (demchan <= 0)
{
cout << "Khong co so chan trong mang!\n";
}
else
{
cout << "Tich cac so chan:" << tichchan << endl;
}
}
else
{
cout << "Mang chua duoc khoi tao!\n";
}
}
break;
case 4:
{
cout << "=== Xoa phan tu tai vi tri bat ky trong mang ===\n";
if (init)
{
int erase;
cout << "Ban muon xoa phan tu thu may:";
cin >> erase;
for (int i = erase - 1; i < n; i++)
{
A[i] = A[i + 1];
}
n--;
for (int i = 0; i < n; i++)
{
cout << A[i];
if (i < n - 1)
{
cout << ",";
}
}
}
else
{
cout << "Mang chua duoc khoi tao!\n";
}
}
break;
case 5:
{
cout << "=== Sap xep cac gia tri trong mang theo thu tu giam dan ===\n";
if (init)
{
int gan, j;
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
if (A[i] > A[j])
{
gan = A[i];
A[i] = A[j];
A[j] = gan;
}
}
cout << "Mang sau khi sap xep la:";
for (int i = 0; i < n; i++)
{
cout << A[i] << " ";
}
}
else
{
cout << "Mang chua duoc khoi tao!\n";
}
}
break;
default:
cout << "Lua chon khong hop le!\n";
break;
}
system("pause");
}
return 0;
}
Biển số xe
#include <iostream>
using namespace std;
int main()
{
int bienso;
cout << "Cac bien so co tong chu so la 9:" << endl;
for (bienso = 10000; bienso <= 99999; bienso++)
{
int tongchuso = 0;
int tmp = bienso;
// Tính tổng cac chữ số
while (tmp != 0)
{
tongchuso += tmp % 10;
tmp /= 10;
}
// Kiểm tra tổng các chữ số và in ra số thỏa mãn
if (tongchuso == 9)
{
cout << bienso << " ";
}
}
cout << endl;
system("pause");
return 0;
}
Cuối Kỳ Bài 2 Về Hàm
#include <iostream>
#include <ctime>
using namespace std;
void NhapMang(long a[], long n)
{
for (int i = 0; i < n; i++)
{
srand(time(NULL));// Note
a[i] = rand() % 10 + 1;// Note
}
}
void XuatMang(const long a[], long n)
{
for (int i = 0; i < n; i++)
{
cout << "Phong hoc thu " << i + 1 << ":";
cout << a[i] << " ";
}
}
long PhamVi(const long a[], long n, long min, long max)
{
int dem = 0;
for (int i = 0; i < n; i++)
{
if (a[i] >= min && a[i] <= max)
{
cout << i + 1 << "\t" << a[i] << endl;
dem++;
}
}
return dem;
}
long GiaTriMin(const long a[], long n)
{
long min = a[0];
for (int i = 1; i < n; i++)
{
if (a[i] < min)
{
min = a[i];
}
}
return min;
}
int main()
{
const int maxsize = 15;
long arr[maxsize];
long n = 10;
cout << "So cho ngoi cua mot phong hoc la:";
NhapMang(arr, n);
XuatMang(arr, n);
int choice;
do
{
system("cls");
cout << "===== MENU =====\n"
<< "1. So cho hoc it nhat\n"
<< "2. In danh sach cac phong hoc va so\n"
<< "3. Phong hoc co so luong\n"
<< "4. In so cho cua n phong hoc dau tien\n"
<< "5. Ket thuc\n"
<< ">> Lua chon:";
cin >> choice;
switch (choice)
{
case 5:
{
cout << "Ket thuc chuong trinh\n";
system("pause");
return 0;
} break;
case 1:
{
cout << "So cho hoc it nhat:\n";
cout << GiaTriMin(arr, n);
cout << endl;
} break;
case 2:
{
cout << "In danh sach cac phong hoc va so cho ngoi\n";
XuatMang(arr, n);
cout << endl;
} break;
case 3:
{
cout << "Phong hoc co so cho pham vi [x;y]\n";
int x, y;
do
{
cout << "Nhap pham vi x, y (x <= y):";
cin >> x >> y;
if (x > y && x <= 0)
{
cout << "Vui long nhap lai x <= y!\n";
}
} while (x > y && x <= 0);
long dempv = PhamVi(arr, n, x, y);
cout << "\n So luong phong hoc thoa dieu kien:" << dempv << endl;
} break;
case 4:
{
cout << "In so cho cua n phong hoc dau tien\n";
{
int m;
do
{
cout << "nhap so luong phong muon in:\n";
cin >> m;
if (m <= 0 || m > n)
{
cout << "Vui long nhap lai gia tri tu 1 den " << n <<
endl;
}
} while (m <= 0 || m > n);
XuatMang(arr, m);
}
} break;
default:
cout << "Vui long nhap lai lua chon!\t";
break;
}
system("pause");
} while (1);
return 0;
}
Cuối Kỳ Bài 1 Về Hàm
#include <iostream>
#include <cmath>
long SoLuongUocSo(long N)
{
int dem = 0;
for (int i = 0; i < N; i++)
{
if (N % i == 0)
{
dem++;
}
}
return dem;
}
bool SoNguyenTo(long N)
{
if (N < 2)
{
return false;
}
else
{
for (int i = 2; i <= sqrt(N); i++)
{
if (N % i == 0)
{
return false;
}
}
}
return true;
}
long SoDaoNguoc(long N)
{
int tong = 0;
while (N > 0)
{
tong = 10 * tong + N % 10;
N = N / 10;
}
return tong;
}
using namespace std;
int main()
{
int choice;
do
{
system("cls");
cout << "===== MENU =====\n"
<< "1. Kiem tra so doi xung\n"
<< "2. In so nguyen to trong pham vi tu A den B\n"
<< "3. So doi xung nho hon hay bang N\n"
<< "0. Thoat chuong trinh\n"
<< ">> Lua chon:";
cin >> choice;
switch (choice)
{
case 0:
{
cout << "Cam on ban da su dung chuong trinh\n";
system("pause");
return 0;
}
case 1:
{
cout << "Chuc nang 1:\n";
int N;
cout << "Nhap N:";
cin >> N;
int tmp = N;
if (SoDaoNguoc(N) == tmp)
{
cout << N << " so doi xung\n";
}
else
{
cout << N << " khong phai la so doi xung\n";
}
} break;
case 2:
{
cout << "Chuc nang 2:\n";
int A, B;
cout << "Nhap pham vi A den B:";
cin >> A >> B;
if (A >= B)
{
int tmp = A;
A = B;
B = tmp;
}
cout << "So nguyen to trong pham vi A den B:\n";
for (int i = A; i <= B; i++)
{
if (SoNguyenTo(i))
cout << i << " ";
}
cout << endl;
} break;
case 3:
{
cout << "Chuc nang 3:\n";
int N;
do
{
cout << "Nhap N:";
cin >> N;
if (N <= 0)
{
cout << "Vui long nhap lai N > 0\t";
}
} while (N <= 0);
cout << "Cac so doi xung lon hon 0 va nho hon " << N << " la:\n";
for (int i = 1; i < N; i++)
{
if (SoDaoNguoc(i))
{
cout << i << " ";
}
}
} break;
default:
cout << "Vui long nhap lai lua chon\n";
break;
}
system("pause");
} while (1);
return 0;
}
Bài toán con ốc sên
// Một số bài tập nâng cao Cấu trúc điều khiển.
/*Giếng sâu H mét, ốc sên bò từ đáy đến miệng giếng. Ban ngày
leo N mét, đêm tụt xuống D mét. Hỏi mất bao nhiêu ngày leo tới
miệng giếng.*/
#include <iostream>
using namespace std;
int main()
{
// H N D M
int H, N, D, M = 0, leo = 0;
cout << "Nhap chiều sâu giếng H, leo N mét, tụt xuống D mét:";
cin >> H >> N >> D;
do
{
leo = leo + D;
M = M + 1;
} while (leo <= H);
cout << "Ket qua:" << M << endl;
system("pause");
return 0;
}
E!
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double n, kq;
system("cls");
do
{
cout << "Nhap so nguyen n:";
cin >> n;
if (n < 0 || n>100)
{
cout << "Vui long nhap lai n!" << endl;
}
} while (n < 0 || n>100);
kq = pow((1 + 1 / n), n);
cout << "Ket qua:" << kq << endl;
system("pause");
return 0;
}
Bài toán gà chó
#include <iostream>
using namespace std;
int main()
{
int ga = 0, cho = 0;
for (ga = 0; ga <= 36; ga++)
{
cho = 36 - ga;
if (4 * cho + 2 * ga == 100)
{
cout << "ga:" << ga << "\t" << "cho:" << cho << endl;
}
}
system("pause");
return 0;
}
Function normal
#include <iostream>
using namespace std;
float DienTichHCN(float a, float b)
{
float c = a * b;
return c;
}
float ChuViHCN(float a, float b)
{
return (a + b) * 2;
}
int SoLonNhat(int a, int b)
{
int max = a;
if (b > max)
{
max = b;
}
return max;
}
float LuyThua(float m, int n)
{
float mu = pow(m, n);
return mu;
}
int sum(int z)
{
if (z == 1)
{
return 1;
}
return z + sum(z - 1);
}
int Giaithua(int z)
{
if (z == 1)
{
return 1;
}
return z * Giaithua(z - 1);
}
int GiaiThua2(int z)
{
int tich = 1;
for (int i = z; i > 0; i--)
{
tich *= i;
}
return tich;
}
bool Sohoanhao(int z)
{
int tong = 0;
for (int i = 1; i < z; i++)
{
if (z % i == 0)
{
tong += i;
}
}
if (tong == z)
{
return true;
}
else
{
return false;
}
}
int main()
{
int x, y;
float c = 2.5;
int d = 2;
cout << "Nhap chieu dai va chieu rong:";
cin >> x >> y;
cout << "Dien tich HCN:" << DienTichHCN(x, y) << endl;
cout << "Chu vi HCN:" << ChuViHCN(x, y) << endl;
cout << "So lon nhat giua 2 so nguyen:" << SoLonNhat(x, y) << endl;
cout << "Mu:" << LuyThua(c, d) << endl;
int z;
cout << "Nhap z:";
cin >> z;
cout << "Tong tu 1 den " << z << ":" << sum(z) << endl;
cout << "Giai thua z!:" << Giaithua(z) << endl;
cout << "Giai thua 2:" << GiaiThua2(z) << endl;
if (Sohoanhao(z))
{
cout << z << " Numbers Pefect" << endl;
}
else
{
cout << z << " Not Numbers Pefect\n";
}
system("pause");
return 0;
}
Ôn tập giữa kỳ BT2
Youtube téch for life
Ôn tập giữa kì BT1
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
char choice;
do
{
system("cls");
cout << "=======MENU=======\n"
<< "a. Tinh tong: S = 1 + 2 + 3 +...+ n (n>0)\n"
<< "b. Tinh dien tich hinh tron voi ban kinh (S = PI*r^2), lam tron voi 3 chu so thap phan voi PI =
3,1416\n"
<< "c. Kiem tra 1 so nguyen co phai so nguyen to khong\n"
<< "d. Nhap 3 so nguyen, tim so nho nhat, so lon nhat\n"
<< "e. Tiep theo cau d, sap xep cac so thu tu theo thu tu giam dan, tang dan\n"
<< "x. Thoat chuong trinh!\n"
<< ">> Lua chon:";
cin >> choice;
switch (choice)
{
case 'a': case'A':
{
cout << "===== Tinh tong =====\n";
int sum = 0, n;
do
{
system("cls");
cout << "Nhap n:";
cin >> n;
if (n <= 0)
{
cout << "Nhap lai n!\n";
}
} while (n <= 0);
for (int i = 0; i <= n; i++)
{
sum += i;
}
cout << "Ket qua:" << sum << endl;
} break;
case 'b': case 'B':
{
cout << "===== Dien tinh hinh tron =====\n";
double dientich, r;
const double PI = 3.1416;
do
{
cout << "Nhap ban kinh r:";
cin >> r;
if (r <= 0)
{
cout << "Nhap lai r!\n";
}
} while (r <= 0);
dientich = (PI * pow(r, 2));
cout << "Dien tich hinh tron la:" << setprecision(3) << fixed << dientich << endl;
} break;
case 'c': case'C':
{
cout << "===== Kiem tra so nguyen to =====\n";
bool snt = true;
int n;
cout << "Nhap n:";
cin >> n;
if (n < 2)
{
bool n = false;
}
else
{
for (int i = 2; i <= sqrt(n); i++)
{
if (n % i == 0)
{
snt = false;
break;
}
}
}
cout << n << (snt ? " la snt" : " khong la snt") << endl;
} break;
case 'd': case 'D':
{
cout << "===== Tim so be nhat, so lon nhat =====\n";
int a, b, c;
cout << "Nhap 3 so nguyen:";
cin >> a >> b >> c;
int maxN = a;
if (b > maxN)
{
maxN = b;
}
if (c > maxN)
{
maxN = c;
}
cout << "So lon nhat la:" << maxN << endl;
int minN = a;
if (b < minN)
{
minN = b;
}
if (c < minN)
{
minN = c;
}
cout << "So nho nhat la:" << minN << endl;
} break;
case 'e': case 'E':
{
cout << "===== Sap xep theo thu tu tang dan, giam dan =====\n";
int a, b, c, tmp;
cout << "Nhap 3 so nguyen:";
cin >> a >> b >> c;
// a b c
if (a > b)
{
tmp = a;
a = b;
b = tmp;
}
if (a > c)
{
tmp = a;
a = c;
c = tmp;
}
if (b > c)
{
tmp = b;
b = c;
c = tmp;
}
cout << "Giam dan:" << a << "<" << b << "<" << c << endl;
cout << "Tang dan:" << a << ">" << b << ">" << c << endl;
}
default:
break;
}
system("pause");
} while (choice != 'x' || choice != 'X');
return 0;
}
Mảng đếm tổng số chẵn hoặc số lẻ và có số lẻ hay số chẳn hay không
#include <iostream>
using namespace std;
const int SIZE = 100;
int main()
{
int a[SIZE], n;
do
{
system("cls");
cout << "Nhap so phan tu trong mang la:";
cin >> n;
if (n < 0 || n > SIZE)
{
cout << "So phan tu vuot qua yeu cau! Vui long nhap lai!\n";
}
} while (n<0 || n>SIZE);
for (int i = 0; i < n; i++)
{
cout << "Nhap phan tu a[" << i << "]:";
cin >> a[i];
}
int demchan = 0, demle = 0, tongchan = 0, tongle = 0;
cout << "Mang dang luu tru so chan la:";
for (int i = 0; i < n; i++)
{
if (a[i] % 2 == 0)
{
cout << a[i] << " ";
tongchan += a[i];
demchan++;
}
}
cout << endl;
cout << "Mang dang luu tru so le la:";
for (int i = 0; i < n; i++)
{
if (a[i] % 2 == 1)
{
cout << a[i] << " ";
tongle += a[i];
demle++;
}
}
cout << endl;
if (demchan > 0)
{
cout << "Co " << demchan << " so chan trong mang" << endl;
cout << "Tong cac so chan trong mang:" << tongchan << endl;
}
else
{
cout << "Khong co so chan trong mang!\n";
}
if (demle > 0)
{
cout << "Co " << demle << " so le trong mang\n";
cout << "Tong cac so le trong mang:" << tongle << endl;
}
else
{
cout << "Khong co so le trong mang!\n";
}
system("pause");
return 0;
}
Test
#include <iostream>
#define PI 3.1416
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
char choice;
bool init = false;
do
{
system("cls");
cout << "=======MENU=======\n"
<< "a. S=1+2+...+n(n>0)\n"
<< "b. Tinh dien tich hinh tron, lam tron den 3 chu so thap phan, voi PI=3,1416\n"
<< "c. Kiem tra 1 so nguyen co phai so nguyen to khong\n"
<< "d. Nhap vao 3 so nguyen, tim so lon nhat, so nho nhat\n"
<< "e. Tiep theo cau d, sap xep cac so theo thu tu tang dan, giam dan\n"
<< "x. Thoat chuong trinh\n"
<< ">> Lua chon:";
cin >> choice;
switch (choice)
{
case 'x': case 'X':
{
cout << "Cam on ban da su dung chuong trinh\n";
return 0;
}
case 'a': case 'A':
{
cout << "=== Chuc nang 1 ===\n";
int n, tong = 0;
/*try_again:
cout << "Nhap n:";
cin >> n;
if (n <= 0)
{
goto try_again;
cout << "Nhap n:";
}*/
do
{
cout << "Nhap n:";
cin >> n;
if (n <= 0)
{
cout << "Nhap lai gia tri n!\n";
}
} while (n <= 0);
for (int i = 0; i <= n; i++)
{
tong += i;
}
cout << "S=1+2+3+...+n(n>0):" << tong << endl;
} break;
case 'b': case 'B':
{
cout << "=== Chuc nang 2 ===\n";
double dientich, r;
try_again:
cout << "Nhap r:";
cin >> r;
if (r <= 0)
{
goto try_again;
cout << "Nhap lai r:";
}
dientich = (PI * pow(r, 2));
cout << "Dien tich hinh tron:" << setprecision(3) << fixed << dientich << endl;
} break;
case 'c': case 'C':
{
cout << "== Chuc nang 3 ===\n";
int n;
cout << "Nhap n:";
cin >> n;
bool is_nt = true;
if (n < 2)
{
is_nt = false;
}
else
{
for (int i = 2; i <= sqrt(n); i++)
{
if (i % 2 == 0)
{
is_nt = false;
}
}
}
cout << n << (is_nt ? " la SNT\n" : " khong phai SNT\n");
} break;
case 'd': case 'D':
{
cout << "=== Chuc nang 4 ===\n";
int a, b, c;
cout << "Nhap a, b, c:";
cin >> a >> b >> c;
init = true;
int maxn = a;
if (b > maxn)
{
maxn = b;
}
if (c > maxn)
{
maxn = c;
}
cout << "So lon nhat:" << maxn << endl;
int minn = a;
if (b < minn)
{
minn = b;
}
if (c < minn)
{
minn = c;
}
cout << "So be nhat:" << minn << endl;
} break;
case 'e': case 'E':
{
cout << "=== Chuc nang 5 ===\n";
if (init)
{
int a, b, c, tmp;
cout << "Nhap a, b, c:";
cin >> a >> b >> c;
if (a > b)
{
tmp = a;
a = b;
b = tmp;
}
if (a > c)
{
tmp = a;
a = c;
c = tmp;
}
if (b > c)
{
tmp = b;
b = c;
c = tmp;
}
cout << "Tang dan:" << a << "<" << b << "<" << c << endl;
cout << "Giam dan:" << c << ">" << b << ">" << a << endl;
}
} break;
default:
break;
}
system("pause");
} while (1);
return 0;
}
Test 2
#include <iostream>
const int SIZE = 100;
using namespace std;
int main()
{
/*int a[SIZE], soduongbn = INT_MAX, soamln = INT_MIN;
int n;
cout << "Nhap n:";
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "Phan tu a[" << i << "]:";
cin >> a[i];
}
cout << "Mang:";
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
for (int i = 0; i < n; i++)
{
if (soamln < a[i] && a[i] < 0)
{
soamln = a[i];
}
if (soduongbn > a[i] && a[i] > 0)
{
soduongbn = a[i];
}
}
if (soamln < 0 && soamln != INT_MIN)
{
cout << "so am lon nhat:" << soamln << endl;
}
else
{
cout << "Khong co!" << endl;
}
if(soduongbn>0&&soduongbn!=INT_MAX)
{
cout<<"so duong be nhat:"<<soduongbn<<endl;
}
else
{
cout << "Khong co!\n";
}*/
// buble short
// Sap xep mang
int a[SIZE], n;
cout << "nhap n:";
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "Phan tu a[" << i << "]:";
cin >> a[i];
}
cout << "Mang:";
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
/*for (int i = n - 1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
if (a[j] > a[j + 1])
{
int tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
}*/
// in class
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] > a[j])
{
int tmp = a[j];
a[j] = a[j];
a[j] = tmp;
}
}
}
cout << "Thu tu tang dan:";
for (int i = 0; i < n; i++)
{
cout << a[i];
if (i < n - 1)
{
cout << "<";
}
}
cout << endl;
system("pause");
return 0;
}
Bài toán con trâu
#include <iostream>
using namespace std;
int main()
{
double x, y, z;// trâu đứng:x trâu nằm:y trâu già:z.
for (x = 1; x < 100; x++)
{
for (y = 1; y < 100; y++)
{
for (z = 1; z < 100; z++)
{
if ((x + y + z == 100) && (15 * x + 9 * y + z == 300))
{
cout << "x:" << x << "\t" << "y:" << y << "\t" << "z:" << z <<
endl;
}
}
}
}
system("pause");
return 0;
}
Vẽ tam giác
#include <iostream>
using namespace std;
int main()
{
int H;
system("cls");
do
{
cout << "Nhap chieu cao tam giac:";
cin >> H;
if (H <= 0)
{
cout << "Vui long nhap lai H!" << endl;
}
} while (H <= 0);
// tam giac ben phai.

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


{
for (int j = H - i; j < H; j++)
{
cout << "*";
}
cout << endl;
}
// tam giac ben trai.
for (int i = H; i >= 1; i--)
{
for (int j = 1; j <= H; j++)
{
if (j >= i)
{
cout << "*";
}
else cout << " ";
}
cout << endl;
}
// tam giac can.
for (int i = 1; i <= H; i++)
{
for (int j = 1; j <= 2 * H - 1; j++)
{
if (j >= H - i + 1 && j <= H + i - 1)
{
cout << "*";
}
else cout << " ";
}
cout << endl;
}
system("pause");
return 0;
}
Tìm ước chung lớn nhất trong 2 số a và b
While (a!=b)
{
If (a>b) a=a-b
Else b=b-a
}
Cout<<a hoặc b
Ôn tập online
#include <iostream>
#define MAX 100

using namespace std;

bool checkPrime(int x)
{
if (x < 2)
return false;

for (int i = 2; i <= sqrt(x); i++)


if (x % i == 0)
return false;

return true;
}

void inputArray(int a[], int &n)


{
cout << "n = ";
cin >> n;

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


{
cout << "a[" << i << "] = ";
cin >> a[i];
}
}

void outputArray(int a[], int n)


{
for (int i = 0; i < n; i++)
cout << a[i] << "\t";
cout << endl;
}

int calSum(int a[], int n)


{
int t = 0;
for (int i = 0; i < n; i++)
t += a[i];

return t;
}

int calSumPrime(int a[], int n)


{
int t = 0;
for (int i = 0; i < n; i++)
if (checkPrime(a[i]) == true)
t += a[i];

return t;
}

int findPositiveMax(int a[], int n)


{
int max = 0;
for (int i = 0; i < n; i++)
if (a[i] > 0 && a[i] > max)
max = a[i];

return max;
}

int findPrimeMin(int a[], int n)


{
int min = INT_MAX;
for (int i = 0; i < n; i++)
if (checkPrime(a[i]) == true && a[i] < min)
min = a[i];

return min;
}

// 2 3 5 4 3 5
int findFirstX(int a[], int n, int x)
{
for (int i = 0; i < n; i++)
if (a[i] == x)
return i;

return -1;

}
int findLast1(int a[], int n, int x)
{
int idx = -1;
for (int i = 0; i < n; i++)
if (a[i] == x)
idx = i;

return idx;
}

int findLast2(int a[], int n, int x)


{
for (int i = n - 1; i >= 0; i--)
if (a[i] == x)
return i;

return -1;
}

int main()
{
// interchange sort

int a[MAX];
int n;

inputArray(a, n);
outputArray(a, n);

cout << "Sum = " << calSum(a, n) << endl;


cout << "Positive max = " << findPositiveMax(a, n) << endl;
cout << "Prime min = " << findPrimeMin(a, n) << endl;

int x;
cout << "Enter x = ";
cin >> x;
cout << "Position = " << findFirstX(a, n, x) << endl;
cout << "Last Position = " << findLast1(a, n, x) << endl;

return 0;
}
Hội thảo chia sẻ tri thức cslt
Bài 1
#include <iostream>
using namespace std;
int main() {

int n;
int i;
cout << "Nhap n = ";
cin >> n; //i ? n%i == 0 [1,n], Gia su: n = 6
i = 1;
while (i <= n) {
if (n % i == 0)
cout << i << " ";
i = i + 1;
}
return 0;
}
Bài 2:
#include <iostream>

using namespace std;

int main() {

int a, b;

cout << "Nhap a = ";


cin >> a;

cout << "Nhap b = ";

cin >> b;

//Xu ly tim UCLN Vi du: a = 4 (1, 2, 4), b = 6 (1, 2, 3, 6)

//Cach 1

int Max = a > b ? a : b; //Max = 6

int Min = a < b ? a : b; //Min = 4

for (int i = Min; i >= 1; i--) {//Note: con TH toi uu

if (a % i == 0 && b % i == 0) {

cout << i << endl;

break;

//Cach 2

while (a != b) {

if (a > b)

a = a - b;

else //Bo if(b>a)

b = b - a;

cout << a << endl; //hoac b

return 0;

}
Bài 3
#include <iostream>

using namespace std;

int main() {

int tongCanTim = 200000;


//In tat ca truong hop

/*for (int i = 0; i <= 10; i++) {

for (int j = 0; j <= 20; j++) {

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

int tinhTong = i * 20000 + j * 10000 + k * 5000;

if (tinhTong == tongCanTim)

cout << i << " x 20.000 + " << j << " x 10.000 + " << k << " x
5000 = " << tongCanTim << endl;

}*/

//Tim cach doi tien nao co so to it nhat

int Min = 1000;

int h, m, n;

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

for (int j = 0; j <= 20; j++) {

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

int tinhTong = i * 20000 + j * 10000 + k * 5000;

if (tinhTong == tongCanTim && (i + j + k) < Min){

h = i;

m = j;

n = k;

Min = i + j + k;

cout << h << " x 20.000 + " << m << " x 10.000 + " << n << " x 5000 = " << tongCanTim << endl;

return 0;
}
Bài 4
#include <iostream>

using namespace std;

int main() {

int chon;//luu lua chon cua nguoi dung

int chieuDai, chieuRong;

int n;//Chieu cao tam giac

do {

cout << "============MENU===============\n";

cout << "1. Hinh chu nhat dac\n";

cout << "2. Hinh chu nhat rong\n";

cout << "3. Hinh tam can dac\n";

cout << "4. Hinh tam giac can rong\n";

cout << "5. Hinh vuong can dac\n";

cout << "6. Hinh vuong can rong\n";

cout << "0. Thoat chuong trinh\n";

cout << "Moi ban chon: ";

cin >> chon;

system("cls");

switch (chon)

case 1:

//Cho nguoi dung nhap tu ban phim

//Gan gia tri

chieuDai = 15;

chieuRong = 5;
for (int i = 0; i < chieuRong; i++) {

for (int j = 0; j < chieuDai; j++) {

cout << "*";

cout << endl;

break;

case 2:

chieuDai = 15;

chieuRong = 5;

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

for (int j = 0; j < chieuDai; j++) {

if (i == 0 || j == 0 || i == chieuRong - 1 || j == chieuDai - 1)

cout << "*";

else

cout << " ";

cout << endl;

break;

case 3:

cout << "Nhap chieu cao tam giac: ";

cin >> n;

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

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

if (j < n - i - 1) {

cout << " ";

else

cout << "*";


}

cout << endl;

break;

case 4:

cout << "Nhap chieu cao tam giac: ";

cin >> n;

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

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

if (j == (n - i - 1) || j == (n + i - 1) || i == (n - 1)) {

cout << "*";

else

cout << " ";

cout << endl;

break;

case 5:

cout << "Nhap chieu cao tam giac: ";

cin >> n;

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

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

if (j <= i) {

cout << "*";

else

cout << " ";

}
cout << endl;

break;

case 6:

cout << "Nhap chieu cao tam giac: ";

cin >> n;

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

for (int j = 0; j < n; j++) {

if (j == i || i == n - 1 || j == 0) {

cout << "*";

else

cout << " ";

cout << endl;

break;

case 0:

cout << "Thoat chuong trinh thanh cong!\n";

break;

default:
cout << "Chuc nang khong nam trong he thong!\n";
break;

} while (chon != 0);


return 0;}

You might also like