Đề cương tin kì 2
Trang 1Câu 1 Lập chương trình giải phương trình bậc nhất ax+b=0.
#include "stdafx.h"
#include <iostream>
using namespace std;
void ptb1(double a, double b)
{
if(a==0)
{
if(b==0) cout<<"PTB1 vo so nghiem !"<<endl;
else cout<<"PTB1 vo nghiem !"<<endl;
}
else
{
cout<<"PTB1 co nghiem X="<<-b/a<<endl;
}
}
void main()
{
double a,b;
cout<<"Cho a=";
cin>>a;
cout<<"Cho b=";
cin>>b;
ptb1(a,b);
}
Câu 2 Lập chương trình giải PTB2
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
void ptb2(double a, double b, double c)
{
double x1,x2,d;
d=b*b-4*a*c;
if(d<0) cout<<"PTB2 vo nghiem !"<<endl;
else if (d==0) cout<<"PTB2 co nghiem kep X="<<-b/2/a<<endl; else
{
x1=(-b+sqrt(d))/2/a;
x2=(-b-sqrt(d))/2/a;
cout<<"PTB2 co 2 nghiem phan biet :"<<endl;
cout<<"X1="<<x1<<" va X2="<<x2<<endl;
}
}
void main()
{
double a,b,c;
cout<<"Cho he so a=";
cin>>a;
Trang 2{
cout<<"Nhap lai a="; cin>>a;
}
cout<<"cho he so b=";
cin>>b;
cout<<"cho he so c=";
cin>>c;
ptb2(a,b,c);
}
Câu 3,4 Lập chương trình tìm ước chung lớn nhất của 2 số nguyên m và n;
#include "stdafx.h"
#include <iostream>
using namespace std;
int ucln(int a, int b)
{
if(b==0) return a;
else return ucln(b,a%b);
}
void main()
{
int m,n,temp;
cout<<"Cho a=";
cin>>m;
cout<<"cho b=";
cin>>n;
temp=m*n;
cout<<"UCLN cua "<<m<<" va "<<n<<" la: "<<ucln(m,n)<<endl;
cout<<"BCNN cua "<<m<<" va "<<n<<" la: "<<temp/ucln(m,n)<<endl; }
Câu 5 Lập chương trình in ra tát cả các số nguyên tố nhỏ hơn n
#include "stdafx.h"
#include <iostream>
using namespace std;
void in_so_nt(int n)
{
int i,j;
for(i=2;i<n;i++)
{
j=2;
while(i%j>0 && j*j<=i) j++;
if(j*j>i) cout<<i<<" "; }
cout<<endl;
Trang 3void main()
{
int n;
cout<<"Cho n=";
cin>>n;
in_so_nt(n);
}
Câu 6 In ra các số chính phuơng nhỏ hơn n
#include "stdafx.h"
#include <iostream>
using namespace std;
void in_scp(int n)
{
for(int i=1;i*i<n;i++)
{
cout<<i*i<<" "; }
cout<<endl;
}
void main()
{
int n;
cout<<"cho n=";
cin>>n;
in_scp(n);
}
Câu 7 Giải bất phương trình bậc nhất ax+b>0
#include "stdafx.h"
#include <iostream>
using namespace std;
void bptb1(double a, double b)
{
if(a==0)
{
if(b>0) cout<<"BPTB1 vo so nghiem !"<<endl; else cout<<"BPTB1 vo nghiem !"<<endl;
}
else if(a>0) cout<<"BPTB1 co nghiem X>"<<-b/a<<endl; else cout<<"BPTB1 co nghiem X<"<<-b/a<<endl;
}
void main()
{
Trang 4double a,b;
cout<<"Cho he so a=";
cin>>a;
cout<<"cho he so b=";
cin>>b;
bptb1(a,b);
}
Câu 8 Giải bất PT bậc 2 >0
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
void bptb2(double a, double b ,double c)
{
double x1,x2,d;
d=b*b-4*a*c;
if(d<0)
{
if(a>0) cout<<"BPTB2 co nghiem voi moi x !"<<endl;
else cout<<"BPTB2 vo nghiem !"<<endl;
}
else if(d==0)
{
if(a>0) cout<<"BPTB2 co nghiem voi moi x!="<<-b/2/a<<endl; else cout<<"BPTB2 vo nghiem !"<<endl;
}
else
{
x1=(-b+sqrt(d))/2/a;
x2=(-b-sqrt(d))/2/a;
if(a>0) cout<<"BPTB2 co nghiem X>"<<x1<<" hoac X<"<<x2<<endl; else
{
cout<<"BPTB2 co nghiem "<<x1<<"< X <"<<x2<<endl;
} }
}
void main()
{
double a,b,c;
cout<<"Cho he so a=";
cin>>a;
while(a==0)
{
cout<<"Nhap lai a="; cin>>a;
}
cout<<"Cho he so b=";
cin>>b;
cout<<"Cho he so c=";
cin>>c;
Trang 5}
Câu 9 Sắp xếp một dãy theo thứ tự tăng dần
#include "stdafx.h"
#include <iostream>
using namespace std;
double a[100];
int n,i,j;
void nhap()
{
for(i=1;i<=n;i++)
{
cout<<"A["<<i<<"]:"; cin>>a[i];
}
}
void sort()
{
double temp;
for(i=1;i<=n-1;i++)
for(j=i+1;j<=n;j++) {
if(a[i]>a[j]) {
temp=a[i];
a[i]=a[j];
a[j]=temp;
} }
}
void print()
{
for(i=1;i<=n;i++)
cout<<a[i]<<" "; cout<<endl;
}
void main()
{
cout<<"Cho so phan tu cua mang n=";
cin>>n;
nhap();
cout<<"Truoc khi sap xep :"<<endl;
print();
sort();
cout<<"Sau khi sap xep tang dan :"<<endl; print();
}
Câu 11 Tìm phần tử lớn nhất của dãy số
#include "stdafx.h"
Trang 6#include <iostream>
using namespace std;
double a[100];
int n,i;
void nhap()
{
for(i=1;i<=n;i++)
{
cout<<"A["<<i<<"]:"; cin>>a[i];
}
}
void tim_max_min()
{
double max,min;
max=min=a[1];
for(i=1;i<=n;i++)
{
if(max<a[i]) max=a[i];
if(min>a[i]) min=a[i];
}
cout<<"MAX = "<<max<<endl;
cout<<"MIN = "<<min<<endl;
cout<<endl;
}
void main()
{
cout<<"Cho so phan tu cua mang n=";
cin>>n;
nhap();
tim_max_min();
}
Câu 13 Phân tích một số nguyên ra thừa số nguyên tố
#include "stdafx.h"
#include <iostream>
using namespace std;
void phantich(int n)
{
int i;
for(;;)
{
i=2;
while(n%i>0) i++;
cout<<i;
if(n==i) break;
n=n/i;
cout<<"*"; }
cout<<endl;
}
void main()
{
Trang 7int n;
cout<<"Cho so n=";
cin>>n;
cout<<n<<"=";
phantich(n);
}
Cách 2:
#include "stdafx.h"
#include <iostream>
using namespace std;
void main()
{
int n;
cout<<"Cho so n=";
cin>>n;
cout<<n<<"=";
int i=1;
while(i<n-1)
{
i++;
if(n%i==0) {
cout<<i<<"*"; n=n/i;
i ;
} }
cout<<n<<endl;
}
Câu 14 Giải hệ PT tuyến tính 2 ẩn
#include "stdafx.h"
#include <iostream>
using namespace std;
double dt(double a1, double b1, double a2, double b2) {
return a1*b2-a2*b1;
}
void main()
{
double a1,b1,c1,a2,b2,c2,d,dx,dy;
cout<<"Nhap lan luot cac he so a1,b1,c1:"; cin>>a1>>b1>>c1;
cout<<"Nhap lan luot cac he so a2,b1,c2:"; cin>>a2>>b2>>c2;
d=dt(a1,b1,a2,b2);
dx=dt(c1,b1,c2,b2);
dy=dt(a1,c1,a2,c2);
if(d!=0)
{
Trang 8cout<<"HPT bac 1 hai an co nghiem duy nhat !"<<endl;
cout<<"x="<<dx/d<<" va y="<<dy/d<<endl;
}
else if(dx==0) cout<<"He PTB1 hai an vo so nghiem !"<<endl;
else cout<<"He PT bac 1 hai an vo nghiem !"<<endl;
}
Câu 15 Đọc vào tên đệm và sau đó tách ra tên, họ và đệm của người đó
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void main()
{
int i,n,j;
string name,lname,midname,fname;
cout<<"Nhap ho ten:";
getline(cin,name);
n=name.size();
i=n-1;
while(name[i]!=' ') i ;
fname.assign(name,i,n-i);
cout<<"Ten:"<<fname<<endl;
j=0;
while(name[j]!=' ') j++;
lname.assign(name,0,j);
cout<<"Ho:"<<lname<<endl;
midname.assign(name,j,i-j);
cout<<"Ten dem:"<<midname<<endl;
}
Câu 16 Đọc vào một dãy số nguyên đến khi gặp số 0 Tính tổng các số chẵn và tổng các số lẻ
#include "stdafx.h"
#include <iostream>
using namespace std;
void main()
{
int n,i;
int sc,sl;
i=1;
sc=sl=0;
do
{
cout<<"Cho so thu "<<i<<":"; cin>>n;
i++;
if(n%2==0) sc+=n;
else sl+=n;
}while(n!=0);
Trang 9cout<<"Tong cac so chan da doc S="<<sc<<endl; cout<<"Tong cac so le da doc S="<<sl<<endl; }
Câu 17 Sắp xếp tên hàng theo thứ tự ABC
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
string ds[100],temp;
void main()
{
int n,i,j;
cout<<"Nhap so luong ten hang:";
cin>>n;
for(i=0;i<n;i++)
{
fflush(stdin);
cout<<"Nhap ten hang:"; getline(cin,ds[i]);
}
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++) {
if(ds[i]>ds[j]) {
temp=ds[i];
ds[i]=ds[j];
ds[j]=temp;
} }
for(i=0;i<n;i++) cout<<ds[i]<<endl;
}
Câu 21.In ra xâu dạng chuẩn !
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void main()
{
string st,st1=" ";
cout<<"Nhap mot xau bat ki:";
getline(cin,st);
int i,n;
while(st.find(st1,0)!=-1)
{
i=st.find(st1,0);
Trang 10st.replace(i,2," ");
}
if(st[0]==' ') st.replace(0,1,"");
n=st.size();
if(st[n-1]==' ') st.replace(n-1,1,"");
cout<<"Xau dang chuan la:"<<st<<"."<<endl;
}
Câu 22 Đọc vào một xâu và in ra số chữ số trong xâu đã đọc
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void main()
{
string st;
int i,n,d=0;
cout<<"Cho mot xau:";
getline(cin,st);
n=st.size();
for(i=0;i<n;i++)
{
if(toupper(st[i])>='0' && toupper(st[i])<='9') d++;
}
cout<<"So chu so dung trong xau la:"<<d<<endl;
}
}
Câu 23 Đọc vào một xâu và in ra số chữ cái trong xâu đã đọc
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void main()
{
string st;
int i,n,d=0;
cout<<"Cho mot xau:";
getline(cin,st);
n=st.size();
for(i=0;i<n;i++)
{
if(toupper(st[i])>='A' && toupper(st[i])<='Z') d++;
}
cout<<"So chu cai dung trong xau la:"<<d<<endl;
}
Câu 23 Đọc vào một xâu và in ra số từ, biết rằng cách nhau bởi dấu cách
#include "stdafx.h"
#include <iostream>
Trang 11#include <string>
using namespace std;
//coi như nhập xâu chuẩn
void main()
{
string st;
int i,n,d=1;
cout<<"Cho mot xau:";
getline(cin,st);
n=st.size();
for(i=0;i<n;i++)
{
if(st[i]==' ') d++;
}
cout<<"So tu dung trong xau la:"<<d<<endl;
}
Câu 27 Đọc vào một xâu và in ra số chữ cái đứng đầu của mỗi từ là chữ hoa còn lại là thường
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void main()
{
string st;
int i,n;
cout<<"Cho mot xau:";
getline(cin,st);
n=st.size();
for(i=0;i<n;i++)
{
if(toupper(st[i])>='A' && toupper(st[i])<='Z') st[i]=tolower(st[i]);
}
if(st[0]>='a' && st[0]<='z') st[0]=toupper(st[0]);
for(i=1;i<n;i++)
{
if(st[i]==' ') {
if(st[i+1]>='a' && st[i+1]<='z') st[i+1]=toupper(st[i+1]); }
}
cout<<"Sau khi bien doi :"<<endl;
cout<<st<<"."<<endl;
}
Câu 28 Đọc vào một xâu và đếm số kí tự khác nhau
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
Trang 12void main()
{
string st,st1;
st1="";
int i,n;
cout<<"Cho mot xau:";
getline(cin,st);
n=st.size();
for(i=0;i<n;i++)
{
if(st1.find(st[i],0)==-1) st1+=st[i];
}
cout<<"So ki tu khac nhau dung trong xau la: "<<st1.size()<<endl;
}
Câu 29 Đọc 2 số nguyên duơng m,n Sau đó đọc vào 2 ma trân A,B và tính tổng
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
int i,j;
void doc_so(int &a, int &b)
{
cout<<"Cho so hang : "; cin>>a;
cout<<"Cho so cot : "; cin>>b;
}
void nhap(double a[50][50], int m, int n, char ten)
{
cout<<"Nhap so lieu cho ma tran "<<ten<<":"<<endl;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++) {
cout<<ten<<i<<j<<":"; cin>>a[i][j];
} cout<<endl;
}
void in_matran(double a[50][50], int m, int n, char ten)
{
cout<<"Ma tran "<<ten<<":"<<endl;
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++) {
printf("%6.1f",a[i][j]);
} cout<<endl;
}
}
void tong(double a[50][50], double b[50][50], int m, int n, char ten)
{
double c[50][50];
Trang 13for(j=1;j<=n;j++) {
c[i][j]=a[i][j]+b[i][j];
} cout<<"Tong cua hai ma tran la:"<<endl;
in_matran(c,m,n,ten);
}
void main()
{
int m,n;
double a[50][50],b[50][50];
doc_so(m,n);
nhap(a,m,n,'A');
in_matran(a,m,n,'A');
nhap(b,m,n,'B');
in_matran(b,m,n,'B');
tong(a,b,m,n,'C');
}
Câu 30 Tính tích ma trận
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
int i,j;
void doc_so(int &m, int &n,int &p)
{
cout<<"Cho m="; cin>>m;
cout<<"Cho p="; cin>>p;
cout<<"cho n="; cin>>n;
}
void nhap(double a[50][50], int m, int n, char ten) {
cout<<"Nhap so lieu cho ma tran "<<ten<<":"<<endl; for(i=1;i<=m;i++)
for(j=1;j<=n;j++) {
cout<<ten<<i<<j<<":"; cin>>a[i][j];
} cout<<endl;
}
void in_matran(double a[50][50], int m, int n, char ten) {
cout<<"Ma tran "<<ten<<":"<<endl;
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++) {
printf("%6.1f",a[i][j]);
}
Trang 14}
}
void tich(double a[50][50], double b[50][50], int m, int p, int n, char ten) {
double c[50][50];
int k;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++) {
c[i][j]=0;
for(k=1;k<=p;k++) c[i][j]+=a[i][k]*b[k][j];
} cout<<"Tich cua hai ma tran la:"<<endl;
in_matran(c,m,n,ten);
}
void main()
{
int m,n,p;
double a[50][50],b[50][50];
doc_so(m,n,p);
nhap(a,m,p,'A');
in_matran(a,m,p,'A');
nhap(b,p,n,'B');
in_matran(b,p,n,'B');
tich(a,b,m,n,p,'C');
}
Câu 31 Lập chuơng trình giải gần đúng PT theo 4 phương pháp.( ví dụ
f(x)=3*x*x-10=0
-Phương pháp chia đôi
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
double f(double x)
{
return 3*x*x-10;
}
void main()
{
double a=1.0,b=2.0,c,ep=0.0001;
do
{
c=(a+b)/2;
if(f(a)*f(c)<0) b=c;
else a=c;
Trang 15cout<<"Nghiem gan dung cua PT la X="<<c<<endl; printf("%0.10f",f(c));
}
Câu 33 Tính sinx cosx và e mũ x
-Tính sin x:
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
void main()
{
double x,g,h,s,n,ep=0.00001;
s=0;
n=0;
cout<<"Cho goc can tinh (theo do):";
cin>>g;
x=g*3.141529/180;
h=x;
while(fabs(h)>ep)
{
s+=h;
n++;
h=-h*x*x/(2*n)/(2*n+1);
}
cout<<"Sin["<<g<<"]="<<s<<endl;
}
-Tính cos x:
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
void main()
{
double x,g,h,s,n,ep=0.00001;
s=0;
n=0;
h=1;
cout<<"Cho goc can tinh (theo do):";
cin>>g;
x=g*3.141529/180;
while(fabs(h)>ep)
{
s+=h;
n++;
h=-h*x*x/(2*n-1)/(2*n);
}