Professional Documents
Culture Documents
ÔN TẬP C
ÔN TẬP C
ÔN TẬP C
cout << x << " mu " << n << " = " << result << cout << x << " mu " << n << " = " << result << endl;
endl; return 0;
return 0; }
}
Tổ #include <iostream> #include <iostream> #include <iostream>
hợp n using namespace std; using namespace std; using namespace std;
chập k
int main() int combination(int n, int k) { int tohop(int n, int k) {
{ int numerator = 1, denominator = 1; if (k == 0 || k == n) {
int n, k; for (int i = n; i >= n - k + 1; i--) { return 1;
cout << "Nhap n: "; numerator *= i; }
cin >> n; } return tohop(n - 1, k - 1) + tohop(n - 1, k);
cout << "Nhap k: "; for (int i = 1; i <= k; i++) { }
cin >> k; denominator *= i;
} int main()
int numerator = 1, denominator = 1; return numerator / denominator; {
for (int i = n; i >= n - k + 1; i--) { } int n, k;
numerator *= i; cout << "Nhap n: ";
} int main() { cin >> n;
for (int i = k; i >= 1; i--) { int n = 5, k = 2; cout << "Nhap k: ";
denominator *= i; int result = combination(n, k); cin >> k;
} cout << "Combination of " << n << " choose " <<
k << " is " << result << endl; int C = tohop(n, k);
int C = numerator / denominator; return 0;
} cout << "To hop " << n << " chap " << k << " la: " <<
cout << "To hop " << n << " chap " << k << " la: C << endl;
" << C << endl; return 0;
return 0; }
}
Tìm số #include <iostream> #include <iostream> #include <iostream>
nguyên tố using namespace std; using namespace std; using namespace std;
int main() { bool isPrime(int n) { bool isPrime(int n, int i = 2) {
int n; if (n <= 1) { if (n <= 2) {
bool flag; return false; return (n == 2) ? true : false;
cout << "Nhap vao so n: "; } }
cin >> n; for (int i = 2; i <= n / 2; i++) { if (n % i == 0) {
cout << "Cac so nguyen to nho hon hoac bang " if (n % i == 0) { return false;
<< n << " la: "; return false; }
for (int i = 2; i <= n; i++) { } if (i * i > n) {
flag = true; } return true;
for (int j = 2; j <= i / 2; j++) { return true; }
if (i % j == 0) { } return isPrime(n, i + 1);
flag = false; int main() { }
break; int n; void printPrimes(int n) {
} cout << "Nhap vao so n: "; if (n >= 2) {
} cin >> n; printPrimes(n - 1);
if (flag == true) { cout << "Cac so nguyen to nho hon hoac bang " if (isPrime(n)) {
cout << i << " "; << n << " la: "; cout << n << " ";
} for (int i = 2; i <= n; i++) { }
} if (isPrime(i)) { }
return 0; cout << i << " "; }
} } int main() {
} int n;
return 0; cout << "Nhap vao so n: ";
} cin >> n;
cout << "Cac so nguyen to nho hon hoac bang " << n
<< " la: ";
printPrimes(n);
return 0;
}
In ra số nguyên tố #include <iostream> Số đảo ngược:
using namespace std; #include <iostream>
using namespace std;
bool isPrime(int number) {
if(number < 2) { // Số nguyên tố phải lớn hơn 1 int main() {
return false; int n, dao = 0, rem, temp;
} cout << "Nhap vao so nguyen: ";
for(int i=2; i<=number/2; i++) { // Lặp từ 2 đến cin >> n;
n/2 temp = n;
if(number%i == 0) { // Nếu n chia hết cho i thì n while (temp != 0) {
không phải số nguyên tố rem = temp % 10;
return false; dao = dao * 10 + rem;
} temp /= 10;
} }
return true;
if (dao == n)
}
cout << n << " la so doi xung";
int main() { else
int number; cout << n << " khong doi xung";
cout << "Nhap mot so: "; return 0;
cin >> number; }
if(isPrime(number)) { Rem đảo Tem
cout << number << " la so nguyen to.";
} else {
cout << number << " khong phai la so nguyen
to.";
}
return 0;
}
Xóa phần tử X trong mang A(Với x được nhập từ Xắp xếp các phần tử trong mảng A Tìm vị trí thứ i của X trong mảng.
bàn phím ) void TimKiem(int A[], int n, int x){
void SapXep(int A[], int n){ int i = 0;
int XoaX(int A[], int &n, int x){ for (int i = 0; i < n-1; i++){ while (i < n && A[i] != x) {
int dem=0; for (int j = i+1; j < n; j++){ i++;
for( int i = 0; i < n; i++){ if (A[j] < A[i]){ }
if(A[i]==x){ swap(A[i], A[j]); if (i == n) {
for (int j = i; j < n-1; j++){ } cout << "Khong tim thay " << x << endl;
A[j]=A[j+1]; } } else {
} } cout << x << " co vi tri la " << i << endl;
n--; } }
dem++; }
i--; // adjust loop index to
recheck current position after shift
}
}
if (dem == 0) {
cout << "Khong tim thay phan tu
de xoa" << endl;
} else {
cout << "Da xoa " << dem << "
phan tu " << x << " khoi mang." << endl;
}
return n;
}
Chèn phần tử X vào vị trí k trong mảng A
void ChenPhanTu(int A[], int &n, int x, int k){
if (k < 0 || k >= n) {
cout << "Vi tri k khong hop le. Nhap lai: ";
cin >> k;
}
n++;
for (int i = n-1; i > k; i--) {
A[i] = A[i-1];
}
A[k] = x;
}
Để thay đổi tham số truyền vào khi gọi ta them dấu & trước tham số call.
Mảng là một dãy các phần tử, lưu trữ kế tiếp nhau trong bộ nhớ.
Int A[10];
Như vậy:
Cuối chữ số n – 1
Chú ý: Chỉ sử dụng bằng hoặc nhỏ hơn số phần tử khai báo.
0 1 2 3 ................................... 9
-A[3]=9
STRUCK
-Kiểu cấu trúc dùng để mô tả các đổi tượng có những trường dữ liệu thương khác nhau.
-Ví dụ một mô tả kiểu dữ liệu sinh viên mô tả mọt điểm trong trung gian R^n,
Struct Tenkieu{
Kiểu dữ liệu: Tên trường 1;
Kiểu dữ liệu: Tên trường 2;
......
TenKieu TenBien;
Muốn truy cập ào biến kiểu struct ta sử dụng tên biến.tên trường
Đề thi:
3. Số nguyên tố
4. Số chính phương
#include <iostream>
#include <string.h>
#include <ctype.h>
struct SinhVien{
char masv[30];
char tensv[30];
double diem;
char XepLoai[30];
};
SinhVien Lop[max];
int siso;
}if (X.diem>=6.5){
}if (X.diem>=5){
//-----------------------------------------------------//
int index=-1;
if (strcmp(Lop[i].masv,masv)==0){
index=1;
break;
if ( index = 1){
}else {
Lop[i]=Lop[i+1];
siso --;
//------------------------------------------------------//
if (strcmp(Lop[i].masv, masv) == 0) {
Lop[i].diem = diem_moi;
XepLoai(Lop[i]);
tim_thay = true;
break;
if (!tim_thay) {
cout << "Khong tim thay sinh vien co ma " << masv << "\n";
//------------------------------------------------------//
SinhVien Input(){
SinhVien X;
Label:
if (strcmp(Lop[i].masv,X.masv)==0){
goto Label;
}
cout<<"Nhap ten sinh vien: ";
fflush(stdin);
gets(X.tensv);
fflush(stdin);
XepLoai(X);
cout<<"---------------------";
return X;
//----------------------------------------------//
cout<<"Diem: "<<X.diem;
Lop[i]=Input();
}
void InDS( SinhVien Lop[], int siso){
Output(Lop[i]);
//----------------------------------------------//
swap(Lop[j], Lop[j-1]);
int main() {
while(siso<=0){
TaoLop(Lop, siso);
InDS(Lop, siso);
cout << endl << "Danh sach sau khi sap xep: "<< endl;
printf("*-----------------*\n");
Sort(Lop, siso);
InDS(Lop, siso);
char masv[30];
int choice;
do {
cout << "Nhap 1: Xoa sinh vien, Nhap 2: Nhap ma sinh vien can cap nhat , 0 de ket thuc: " << endl;
switch(choice) {
case 1:
InDS(Lop, siso);
break;
case 2:
cout << "Nhap ma sinh vien can cap nhat diem: ";
double diem_moi;
CNDIEM(Lop,siso,masv,diem_moi);
InDS(Lop, siso);
Sort(Lop, siso);
InDS(Lop, siso);
break;
case 0:
break;
default:
cout << "Nhap sai, vui long nhap lai!" << endl;
break;
} while(choice != 0);
File là một kiểu cấu trúc dữ liệu lưu trữ ở bộ nhớ ngoài
FILE*<tenbienfile>
Vd:
File*fptr;
Khi tạo ra một file để ghi, sẽ tạo ra file trắng nếu như trùng tên đã ghi.
Kieeur con tro la mot kieu du lieu ma noi dung cua no chua dia chi cua mot bien khac
KieuDL*Tenbiencontro