LTNC Thi Code Mau
LTNC Thi Code Mau
bool kt_ngto_cung_nhau (int a, int b){ //tu be den lon, tang dan
for (int i=2; i<=a; i++){ Void sapxep (int A[]){
if ((a%i==0)&&(b%i==0)){ for(int i=0; i<n-1; i++){
return false; for(int j=i+1; j<n; j++){
break; if(a[i]>a[j]){
} temp=a[i];
} a[i]=a[j];
return true; a[j]=temp;
} }
Gọi hàm }
if(kt_ngto_cung_nhau(a, b)==true) //a va b la so nguyen to }
cung nhau }
else //a va bkhong phai la so nguyen to cung nhau Gọi hàm
SỐ NGUYÊN TỐ Sapxep(A);
bool kiemtra(int n){ VỊ TRÍ NHỎ NHẤT TRONG DAY
if(n<2){ int pos_min_a (int n, int A[]){
return false; int min=A[0], pos=0;
}else{ for(int i=1; i<=n; i++){
for(int i=2; i<n; i++){ if (i<min){
if(n%i==0){ min = A[i];
return false; pos = i;
} }
} }
return true; return pos;
} }
} Gọi hàm
Gọi hàm pos_min_a(n, A) //xuất ra vị tri bé nhất
if(kiemtra(n)==true) //n là số nguyên tố
else //n không phải là số nguyên tố
SINHCHUỖI NGẪU NHIÊN MỘT MẢNG A CÓ N PHẦN TỬ, THAY THẾ CÁC PHẦN TỬ X
#include<stdlib.h>
void rand_chuoi (int n, int X[]){ THUỘC MẢNG A =Y
for(int i=0; i<n; i++){ #include<stdlib.h>
X[i]=rand()%100+1; void thaythe (int N , int A[]){
cout<<X[i]<<" "; int x, y, dem=0;
} cout<<"Nhap gia tri can thay the (x): ";
} cin>>x;
Gọi hàm cout<<"Nhap gia tri thay the (y): ";
rand_chuoi(n, X); cin>>y;
SINH CHUỖI 0-1 CÓ N BIT for (int i=0; i<N; i++){
#include<stdlib.h> if( A[i]==x ) {
using namespace std; A[i]=y;
void out_put(int N){ dem++;
for (int i=0; i<N; i++){ }
int x=0+rand()%2; }
cout<<x<<"\t"; if (dem>0){
} cout<<"Day sau khi thay doi: "<<endl;
} for (int i=0; i<N; i++){
Gọi hàm cout<<A[i]<<" ";
out_put(N) }
}else{
cout<<"Khong co so "<<x<<" trong day";
}
}
Gọi hàm
thaythe (n, a);
TÌM TỪ DÀI NHẤT
CHUYỂN ĐỔI SANG DẠNG NHỊ PHÂN #include<string>
#include<cmath> #include<sstream>
void bin (int n){ void wordmax (string s){
int temp1=0, output=0; stringstream ss(s);
for( int i=n; i>0; i=i/2){ string word;
temp1=temp1+1; string temp;
output=output+(i%2)*pow(10, temp1-1); int max=0;
} while(ss >> word){
cout<<"Ket qua chuyen doi so "<<n<<" sang nhi phan: if(word.size()>max){
"<<output; max=word.size();
} temp=word;
Gọi hàm }
bin (N); }
TỔ HỢP CHẬP K CỦA N cout<<"Tu dai nhat: "<<temp;
long double gt (int a){ }
if (a==1) return 1; Gọi hàm
return gt(a-1)*a; wordmax(s);
}
int main(){
int n, k;
cout<<"Nhap N: ";
cin>>n;
cout<<"Nhap k: ";
cin>>k;
int temp=n-k;
long double output=gt(n)/(gt(temp)*gt(k));
cout<<"To hop chap "<<k<<" cua "<<n<<": "<<output;
return 0;
}
SỐ TỪ TRONG CÂU TÌM VỊ TRÍ LỚN HƠN
#include<string> int solonhon(float a[], int n, float d){
#include<sstream> int temp=-1;
void wordmax (string s){ for (int i=0; i<n; i++){
stringstream ss(s); if(a[i]>d){
string word; temp=i;break;
string temp; }
int max=0; }
while(ss >> word){ if (temp>=0) return temp;
if(word.size()>max){ else return -1;
max=word.size(); }
temp=word;
} Gọi hàm
} Vi tri cua phan tu dau tien trong mang lon hon d
cout<<"Tu dai nhat: "<<temp; solonhon(a, n, d);
}
Gọi hàm
wordmax(s);
ĐẾM SỐ LỚN HƠN D
int lonhon (int n, float d, float a[]){
int dem=0;
for (int i=0; i<n; i++){
if (a[i]>d) dem++;
}
return dem;
}
Gọi hàm
lonhon (n, d, a);
TÌM SỐ ĐỐI XỨNG int main(){
#include<cmath> for(int i=2;i<8;i++){
bool kiemtradoixung(int n){ cout<<"So doi xung lon nhat cua tich 2 so co "<<i<<" chu so
int temp=n, dao_n = 0; la: "<<timsodoixung(i)<<endl;
while(temp!=0){ }
dao_n = dao_n*10 + temp%10; return 0;
temp /=10; }
}
if(n == dao_n){
return true;
}
return false;
}