Viết chương trình để giải quyết các bài toán sau
Bài 1: S(n) = 1+2+3+ … + n.
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
long n, s=0,i;
cin >> n;
for (i=1; i<=n; i++)
s=s+i;
cout << s;
system("pause");
return 0;
}
Bài 5: S(n) = 1+1/3+1/5+…..+1/(2n+1).
//16521215_Bai5.cpp
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
long n,i;
float s=0;
cin >> n;
for (i=0; i<=n; i++)
s=s+((float)1/(2*i+1));
cout << s;
system("pause");
return 0;
}
Bài 12: Tính S(n) = x + x^2 + x^3 + … x^n.
// 16521215_Bai12.cpp
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
long long s=0,x, tmp=1;
long i, n;
cout << "Nhap x, n : ";
cin >> x >> n;
for (i=1; i<=n; i++)
{
tmp=tmp*x;
s=s+tmp;
}
cout << s;
system("pause");
return 0;
}
Bài 20: Liệt kê tất cả “ước số” của số nguyên dương N
//16521215_Bai20.cpp
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
long n,i;
cin >> n;
for (i=1; i<=n/2; i++)
if (n%i==0) cout << i << " ";
cout <<n;
system("pause");
return 0;
}
Bài 25: Tính tổng tất cả “ước số chẵn” của số nguyên dương N.
//16521215_Bai25.cpp
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
long n, i,s=0;
cin >> n;
for (i = 2; i <= n / 2; i++)
if ((n%i == 0) && (i % 2 == 0))
s = s + i;
if (n % 2 == 0)
s=s+n;
cout << s;
system("pause");
return 0;
}
Bài 28: Cho số nguyên dương N. Tính tổng các ước số nhỏ hơn chính nó.
//16521215_Bai28.cpp
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
long n, i,s=0;
cin >> n;
for (i = 1; i <= n / 2; i++)
if (n%i == 0)
s = s + i;
cout << s;
system("pause");
return 0;
}
Bài 31: cho số nguyên dương N. Kiểm tra số nguyên dương N có phải là số nguyên tố hay không?
//16521215_Bai31.cpp
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
long checknt(long n)
{
if (n <= 1) return 0;
long i;
for (i = 2; i <= sqrt(n); i++)
if (n%i == 0)
return 0;
return 1;
}
int main()
{
long n;
cin >> n;
cout << (checknt(n) ? "La SNT\n" : "Khong la SNT\n");
system("pause");
return 0;
}
Bài 32: cho số nguyên dương N. Kiểm tra số nguyên dương N có phải là số chính phương hay không?
//16521215_Bai32.cpp
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
long n;
cin >> n;
if (n == pow(trunc(sqrt(n)),2))
cout << n << " la so chinh phuong\n";
else
cout << n << " khong la so chinh phuong\n";
system("pause");
return 0;
}
Bài 33: Tính S(n) = Căn (2 + Căn (2 +….. Căn (2 +căn(2) ))). có n dấu căn
//16521215_Bai33.cpp
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
long n,i;
float s;
cin >> n;
s = 0;
for (i = 1; i <= n; i++)
s = sqrt(2 + s);
cout << s;
system("pause");
return 0;
}
Bài 35: Tính S(n) = Căn (1 + Căn (2 +….. Căn (n-1 +căn(n) ))). có n dấu căn
//16521215_Bai35.cpp
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
long n, i;
float s;
cin >> n;
s = 0;
for (i = n; i >= 1; i--)
s = sqrt(i+s);
cout << s;
system("pause");
return 0;
}Bài 39
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
long n, i, gt = 1;
float s = 0;
cin >> n;
for (i = 1; i <= n; i++)
{
gt = gt*i;
s = pow(gt + s, (float)1 / (i + 1));
}
cout << s;
system("pause");
return 0;
}
Bài 40: Tính S(n) = Căn (x^n + Căn (x^(n-1) +….. Căn (x^2 +căn(x) ))) có n dấu căn
//16521215_Bai40.cpp
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
long n, i;
long long tmp=1,x;
float s;
cout << "Nhap lan luot x, n : ";
cin >> x >> n;
s = 0;
for (i = 1; i <= n; i++)
{
tmp=tmp*x;
s = sqrt(tmp + s);
}
cout << s;
system("pause");
return 0;
}
Bài 41: Tính S(n) = 1/(1+1/(1+…1+1/(1+1)) ) có N dấu phân số
//16521215_Bai41.cpp
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
long n, i;
float s, tmp = 1;
cout << "Nhap n = ";
cin >> n;
s = 1;
for (i = 1; i <= n; i++)
{
s = 1 / (1 + s);
}
cout << s;
system("pause");
return 0;
}
Bài 43: Đếm số lượng chữ số của số nguyên dương N
//16521215_Bai43.cpp
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
long n, x = 0,tmp;
cin >> n;
tmp = n;
while (n != 0)
{
n=n/10;
x++;
}
cout << tmp << " Co " << x << " chu so\n";
system("pause");
return 0;
}
Bài 50: tìm số nghịch đảo của số nguyên dương N
//16521215_Bai50.cpp
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
long n, x = 0;
cin >> n;
while (n != 0)
{
x = x*10 + n%10;
n = n/10;
}
cout << x << "\n";
system("pause");
return 0;
}
Bài 51: tìm chữ số lớn nhất của số nguyên dương N
//16521215_Bai51.cpp
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
long n, x = 0,m=-1;
cin >> n;
while (n != 0)
{
m = max(m, n % 10);
n = n / 10;
}
cout <<"Chu so lon nhat la : "<< m << "\n";
system("pause");
return 0;
}
Bài 60: Kiểm tra các chữ số của số nguyên dương N có tăng dần từ trái sang phải không?
//16521215_Bai60.cpp
#include <stdio.h>
#include <iostream>
using namespace std;
long check(long n)
{
long tmp = 10;
while (n != 0)
{
if (n % 10 >= tmp)
return 0;
tmp = n % 10;
n = n / 10;
}
return 1;
}
int main()
{
long n;
cout << "Nhap N = ";
cin >> n;
cout << (check(n)?"N Tang dan":"N Khong tang dan")<<endl;
system("pause");
return 0;
}
Bài 62: cho hai số nguyên dương a, b. tìm ước chung lớn nhất của 2 số.
//16521215_Bai62.cpp
#include <stdio.h>
#include <iostream>
using namespace std;
long ucln(long a, long b)
{
long r;
while (a%b != 0)
{
r = a%b;
a = b;
b = r;
}
return b;
}
int main()
{
long a, b;
cin >> a >> b;
cout << ucln(a,b)<<endl;
system("pause");
return 0;
}
Bài 64: giải phương trình ax+b=0.
//16521215_Bai64.cpp
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
cout << "Nhap vao lan luot a, b : ";
long a, b;
cin >> a >> b;
if (a == 0)
cout << "phuong trinh vo nghiem \n";
else
cout << "x = " << (-b/(float)a) << endl;
system("pause");
return 0;
}
Bài 65: giải phương trình ax^2+bx+c=0
//16521215_Bai65.cpp
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
cout << "Nhap vao lan luot a, b, c : ";
float a, b, c;
cin >> a >> b >> c;
if (a == 0)
{
if (b == 0)
{
if (c == 0)
cout << "phuong trinh co vo so nghiem \n";
else
cout << "phuong trinh vo nghiem \n";
}
else
cout << "x = " << -c/b << endl;
}
else
{
float delta = b*b - 4 * a*c;
if (delta<0)
cout << "phuong trinh vo nghiem \n";
if (delta == 0)
cout << "x = " << -b / (2 * a) << endl;
if (delta > 0)
{
cout << "x1 = " << (-b +sqrt(delta))/ (2 * a) << endl;
cout << "x2 = " << (-b -sqrt(delta)) / (2 * a) << endl;
}
}
system("pause");
return 0;
}Bài 66 giải phương trình trùng phương
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
float a, b, c, delta, t, t1, t2;
cout << "Nhap lan luot a, b, c: ";
cin >> a >> b >> c;
if (a == 0)
{
if (b == 0)
{
if (c == 0)
cout << "vo so nghiem" << endl;
else
cout << "Vo nghiem" << endl;
}
else
{
t = -c / b;
if (t < 0)
cout << "vo nghiem" << endl;
else
cout << "x1 = -" << sqrt(t) << "\nx2 = " << sqrt(t);
}
}
else
{
delta = (b*b) - 4 * a*c;
if (delta < 0)
cout << "vo nghiem" << endl;
else
if (delta == 0)
{
t = -b / 2 / a;
if (t<0)
cout << " vo nghiem" << endl;
else
cout << "x1 = -" << sqrt(t) << endl << "x2 = " << sqrt(t);
}
else
{
t1 = (-b - sqrt(delta)) / 2 / a;
t2 = (-b + sqrt(delta)) / 2 / a;
if (t1 < 0 && t2 < 0)
cout << "vo nghiem" << endl;
else
if (t1 >= 0 && t2 < 0)
cout << "x1 = -" << sqrt(t1) << endl << "x2 = " << sqrt(t1) << endl;
else
if (t1 < 0 && t2 >= 0)
cout << "x1 = -" << sqrt(t2) << endl << "x2 = " << sqrt(t2) << endl;
else
cout << "x1 = -" << sqrt(t1) << endl << "x2 = " << sqrt(t1) << endl << "x3 = -" << sqrt(t2) << endl << "x4 = " << sqrt(t2) << endl;
}
}
system("pause");
return 0;
}Bài 72: Tính S(x,n) = -x + x^2/2! – x^3/3! +…+(-1)^n*x^n/n!.
//16521215_Bai72.cpp
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
long i,x,n,tmpx=1,gt=1,mul;
float s = 0;
cout << "nhap lan luot x, n :";
cin >> x >> n;
for (i = 1; i <= n; i++)
{
if (i % 2 == 0)
mul = 1;
else
mul = -1;
tmpx=tmpx*x;
gt = gt*i;
s = s + (mul*tmpx) / (float)gt;
}
cout << s;
system("pause");
return 0;
}
Bài 76: Xác định số nhập từ bàn phím có dạng 3^k hay không?
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
int check(long n)
{
if (n < 0) return 0;
while (n != 1)
{
if (n % 3 != 0)
return 0;
n = n / 3;
}
return 1;
}
int main()
{
long n;
cin >> n;
cout << (check(n) ? "N la so 3^k" : "N ko la so 3^k");
system("pause");
return 0;
}
Đặng Minh Tiến – UIT K11