0% found this document useful (0 votes)
20 views14 pages

LTNC Thi Code Mau

Uploaded by

mtsen1900
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)
20 views14 pages

LTNC Thi Code Mau

Uploaded by

mtsen1900
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/ 14

SỐ NGUYÊN TỐ CÙNG NHAU SẮP XẾP

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;
}

int timsodoixung(int n){


int b=0, max_n = pow(10,n)-1;
for(int i=max_n;i>=0;i--){
for(int j=max_n;j>=0;j--){
int a=i*j;
if(a <= b){
break;
}
if(kiemtradoixung(a) && a>b){
b = a;
}
}
}
return b;
}
THỪA SỐ NGUYÊN TỐ for (int b=2; b<c; b++){
void abc ( int n){ for (int a=1; a<b; a++){
int max=0, dem=0; if (((pow(a, 2)+pow(b, 2))==pow(c,
cout<<n<<"="; 2))&&((a+b+c)==n)) {
for(int i=2; i<=n; i++){ cout<<a<<" "<<b<<" "<<c;
dem = 0; dem++;
while((n%i)==0){ }
++dem; }
n /= i; }
} }
if(dem>=1){ if (dem==0) cout<<"Khong co";
max=i; return 0;
cout << i; }
if(dem > 1) cout <<"^"<<dem;
if(n > i){
cout <<"*";
}
}
}
cout <<"Thua so nguyen to lon nhat: "<<max;
}
Gọi hàm
abc ( n);
SỐ PYTAGO
#include<cmath>
int n, dem=0;
cout <<"Nhap so tu nhien N: ";
cin>>n;
for (int c=3; c<=n; c++){
//CHUYEN DOI HE CO SO #include<iostream>
#include<iostream> #include<fstream>
#include<cmath> #include<string>
using namespace std; using namespace std;
int main(){ int main(){
int a, b, n, outputa=0, outputb=0, dem=0; string line;
cout <<"Nhap he co so a: "; double a, b, c;
cin>>a; ofstream Duy;
cout <<"Nhap he co so b: "; Duy.open("doubles.txt");
cin>>b; cout<<"Nhap so thuc a: ";
cout <<"Nhap so tu nhien N: "; cin>>a;
cin>>n; Duy<<a<<" ";
int temp1=n; cout<<"Nhap so thuc b: ";
for (int i=n; i>0; i=i/a){ cin>>b;
int temp2=i%a; Duy<<b<<" ";
dem++; cout<<"Nhap so thuc c: ";
outputa=outputa+temp2*pow(10, dem-1); cin>>c;
} Duy<<c<<" ";
for (int i=n; i>0; i=i/b){ Duy.close();
int temp2=i%b;
dem++; ifstream Duy1;
outputb=outputb+temp2*pow(10, dem-1); Duy1.open("doubles.txt");
} while (!Duy1.eof()){
cout<<"Chuyen doi sang he co so dem "<<a<<": getline(Duy1,line);
"<<outputa<<endl; cout<<line<<endl;
cout<<"Chuyen doi sang he co so dem "<<b<<": "<<outputb; }
return 0; Duy.close();
} return 0;
return 0;
}
#include<iostream> string line;
#include<fstream> fstream Duyxinh;
#include<string> Duyxinh.open("5.txt");
using namespace std; getline (Duyxinh, line);
void nhap_mang (int n, int A[]){ int temp;
for(int i=0; i<n; i++){ while (Duyxinh>>temp){
cout<<"Nhap gia tri thu "<<i<<": "; if (temp%2==0) cout<<temp<<" ";
cin>>A[i]; }
} Duyxinh.close();
} }
void ghi_tep( int n, int A[]){ int main(){
ofstream Duyxinh; int n;
Duyxinh.open("5.txt"); cout<<"Nhap n: ";
Duyxinh<<n<<endl; cin>>n;
for(int i=0; i<n; i++){ int A[n];
Duyxinh<<A[i]<<" "; nhap_mang ( n, A);
} ghi_tep( n, A);
Duyxinh.close(); cout<<"======================"<<endl;
} in_tep();
void in_tep(){ cout<<"======================"<<endl;
string line; in_B();
ifstream Duyxinh1; return 0;
Duyxinh1.open("5.txt"); }
while (!Duyxinh1.eof()){
getline(Duyxinh1,line);
cout<<line<<endl;
}
Duyxinh1.close();
}
void in_B(){
//TIM VI TRI NHO NHAT HOẶC KO CÓ COUT NULL void saochep (int n, double *pa, double*pb){
//KHOI TAO DUNG TRO cout<<"Sao chep: ";
//SAO CHEP MANG PB TU PA, DUNG TRO for(int i=0; i<n; i++){
#include<iostream> *(pb+i)=*(pa+i);
using namespace std; cout<<*(pb+i)<" ";
void nhonhat (int A[], int n){ }
int min=A[0], pos=0, dem=0; }
for (int i=0; i<n; i++){ int main(){
if (A[i]<=min){ int n;
min=A[i]; cout<<"Nhap so phan tu: ";
pos=i; cin>>n;
dem++; int A[n];
} double *pa=new double [n];
} double *pb=new double [n];
if (dem==n)cout<<"NULL"; nhap_A (A, n);
else cout<<"Vi tri nho nhat "<<pos; cout<<endl;
} nhonhat (A, n);
void nhap_A (int A[], int n){ cout<<endl;
for (int i=0; i<n; i++){ khoitao (pa, n);
cout<<"A["<<i<<"]="; cout<<endl;
cin>>A[i]; saochep (n, pa, pb);
} return 0;
} }
void khoitao (double *pa, int n){
cout<<"Khoi tao: ";
for(int i=0; i<n; i++){
*(pa+i)=0;
cout<<*(pa+i)<" ";
}
}
CON TỰ TRỎ, LIST while(head != NULL){
#include<iostream> cout<<head->giatri<<" ";
using namespace std; head=head->next;
struct value { }
int giatri; }
struct value*next; //Dem so luong phan tu
}; int count (value *head){
//Tao mot value moi int dem=0;
value *makeValue(int x){ while(head != NULL){
value *newValue= new value(); dem=dem+1;
newValue->giatri=x; head=head->next;
newValue->next=NULL; }
return newValue; return dem;
} }
//Them mot value moi vao cuoi //Them value moi vao dau day
void pushBack(value *&head, int x){ void pushFirst (value *&head, int x){
value *temp = head; value *newValue = makeValue(x);
value *newValue = makeValue(x);//tao mot value moi newValue->next=head;
if ( head==NULL){ head=newValue;
head = newValue;return;//neu head rong thi tao mot }
value moi //Them mot value vao giua day
} void insert (value *&head, int B, int posB){
while(temp->next != NULL){ if(posB<1){
temp=temp->next; cout<<"Loi"<<endl;return;
}//dua con tro temp (head) ve cuoi cung cua chuoi lien ket }else if(posB==1){
temp->next=newValue;//tro con tro temp vao vi tri *next cua pushFirst (head, B);
value; }else if(posB>count(head)){
} pushBack(head, B);
//in day value vua nhap }else{
void in (value *head){ value *temp=head;
for(int i=1; i<=posB-2; i++){ }
temp=temp->next; value *last = temp->next;
} temp->next=NULL;
value *newValue = makeValue(B); delete last;
newValue->next=temp->next; }
temp->next=newValue; //Xoa giua
} void eraseBetween (value *&head, int pos){
} if(head==NULL||pos<1||pos>count(head)){
//Xoa dau cout<<"Loi"<<endl;return;
void eraseFirst (value *&head){ }
if(head==NULL){ else if (pos==count(head)){
cout<<"Loi"<<endl;return; eraseBack (head);
} }else if(pos==1){
value *temp=head; eraseFirst (head);
head=head->next; }else{
delete temp; value *temp=head;
} for(int i=1; i<=pos-2; i++){
//Xoa cuoi temp=temp->next;
void eraseBack (value *&head){ }
if(head==NULL){ value *Xoa = temp->next;
cout<<"Loi"<<endl;return; temp->next=temp->next->next;
} delete Xoa;
value *temp=head; }
if(temp->next==NULL){ }
head=NULL; //Xoa node co giatri==k;
delete temp; void xoa_k (value *& head, value *&temp1, int k){
return; if(head==NULL){
} cout<<"Loi"<<endl;return;
while(temp->next->next != NULL){ }
temp=temp->next; int i=1, dem=0;
value *temp3=head; cout<<"---------------------------------------";
for (value *temp2=temp1; temp2 != NULL; temp2=temp2- cout<<"\nDay vua nhap: "<<endl;
>next){ in(head);
if(temp2->giatri==k){ cout<<"\nSo phan tu cua day: "<<count (head);
dem=dem+1; cout<<"\n---------------------------------------";
eraseBetween (temp3, i); cout<<"\nNhap gia tri can chen vao dau: ";
} cin>>A;
else { pushFirst (head, A);
i=i+1; cout<<"\nDay vua chen vao dau: "<<endl;
} in(head);
} cout<<"\n---------------------------------------";
if (dem==0) { cout<<"\nNhap gia tri can chen vao giua: ";
cout<<"\nKhong tim thay gia tri "<<k<<" trong day"; cin>>B;
}else{ cout<<"Nhap vi tri can chen: ";
cout<<"\nSo gia tri da xoa la: "<<dem; cin>>posB;
} insert (head, B, posB);
} cout<<"\nDay vua chen vao giua: "<<endl;
int main(){ in(head);
value *head=NULL; cout<<"\n---------------------------------------";
value *temp1=NULL; eraseFirst (head);
int n, A, B, posB, pos, k; cout<<"\nDay vua xoa gia tri dau: "<<endl;
cout<<"Nhap so gia tri can nhap: "; in(head);
cin>>n; cout<<"\n---------------------------------------";
for (int i=1; i<=n; i++){ eraseBack (head);
int x; cout<<"\nDay vua xoa gia tri cuoi: "<<endl;
cout<<"Nhap gia tri thu "<<i<<": "; in(head);
cin>>x; cout<<"\n---------------------------------------";
pushBack(head, x); cout<<"\nNhap vi tri can xoa: ";
pushBack(temp1, x); cin>>pos;
} eraseBetween (head, pos);
cout<<"\nDay vua xoa giua: "<<endl; S_P=R*R*3.14;
in(head); S_Q=C*C*3.14;
cout<<"\n---------------------------------------"; S_P_1=(AB*S_P)/(2*R);
cout<<"\nNhap gia tri can xoa: "; S_Q_1=(AB*S_Q)/(2*C);
cin>>k; out=S_P_1+S_Q_1;
xoa_k (head, temp1, k); cout<<"Dien tich phan giao nhau cua hai hinh tron:
cout<<"\nDay vua xoa gia tri "<<"k"<<": "<<endl; "<<setprecision(6)<<fixed<<out;
in(head); }
return 0; int main(){
} float R, C, xP, yP, xQ, yQ;
cout<<"Nhap toa do tam P va ban kinh R:"<<endl;
VỊ TRÍ 2 ĐƯỜNG TRÒN HỆ TOA ĐỘ cout<<"x= ";
#include<iostream> cin>>xP;
#include<cmath> cout<<"y= ";
#include<iomanip> cin>>yP;
using namespace std; cout<<"R= ";
int kiemtra (float PQ, float R, float C){ cin>>R;
if (PQ<(R+C)) return 1; cout<<"Nhap toa do tam Q va ban kinh C:"<<endl;
if (PQ>(R+C)) return 2; cout<<"x= ";
if (PQ==(R+C)) return 3; cin>>xQ;
if (((PQ+R)==C)||((PQ+C)==R)) return 4; cout<<"y= ";
if (((PQ+R)<C)||((PQ+C)<R)) return 5; cin>>yQ;
} cout<<"C= ";
void tinh_S (float PQ, float R, float C){ cin>>C;
double cos_PQA; float PQ, RC;
double BQ, AB; PQ=sqrt(pow(xP-xQ,2)+pow(yP-yQ,2));
double S_P, S_P_1, S_Q, S_Q_1, out; if (kiemtra(PQ, R, C)==2) cout<<"Hai hinh tron khong giao
cos_PQA=(pow(PQ,2)+pow(C,2)-pow(R,2))/(2*PQ*C); nhau";
BQ=cos_PQA*C; if (kiemtra(PQ, R, C)==3) cout<<"Hai hinh tron tiep xuc voi
AB=sqrt(C*C-BQ*BQ); nhau";
if (kiemtra(PQ, R, C)==4) cout<<"Hai hinh tron tiep xuc
trong";
if (kiemtra(PQ, R, C)==5) cout<<"Hinh tron nay nam trong
hinh tron kia";
if (kiemtra(PQ, R, C)==1) {
cout<<"Hai hinh tron giao nhau"<<endl;
tinh_S(PQ, R, C);
}
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