Share 450 bài tập về lập trình c++

danggodsmack

New member
### 450 C ++ Bài tập lập trình

#C ++ #Programming #exercise #tutorial

** 1.Viết một chương trình để in Hello World! **

`` `C ++
#include <Istream>

int main () {
std :: cout << "Xin chào thế giới!"<< std :: endl;
trả lại 0;
}
`` `

** 2.Viết một chương trình để thêm hai số **

`` `C ++
#include <Istream>

int main () {
int a, b;
std :: cin >> a >> b;
int sum = a + b;
std :: cout << "sum =" << sum << std :: endl;
trả lại 0;
}
`` `

** 3.Viết một chương trình để tìm khu vực của một vòng tròn **

`` `C ++
#include <Istream>

const Double Pi = 3.14159;

int main () {
Bán kính gấp đôi;
std :: cin >> bán kính;
Double diện tích = pi * bán kính * bán kính;
std :: cout << "khu vực =" << khu vực << std :: endl;
trả lại 0;
}
`` `

**4.Viết một chương trình để sắp xếp một loạt các số nguyên **

`` `C ++
#include <Istream>
#include <Thistithm>

int main () {
int n;
std :: cin >> n;
int mảng [n];
for (int i = 0; i <n; i ++) {
std :: cin >> mảng ;
}
std :: sort (mảng, mảng + n);
for (int i = 0; i <n; i ++) {
std :: cout << mảng << "";
}
std :: cout << std :: endl;
trả lại 0;
}
`` `

** 5.Viết một chương trình để thực hiện tìm kiếm nhị phân **

`` `C ++
#include <Istream>

int nhị phân nghiên cứu (int mảng [], int n, int x) {
int low = 0;
int cao = n - 1;
while (thấp <= cao) {
int mid = (thấp + cao) / 2;
if (mảng [mid] == x) {
trở lại giữa;
} if if (mảng [mid] <x) {
Thấp = Mid + 1;
} khác {
cao = giữa - 1;
}
}
trả lại -1;
}

int main () {
int n;
std :: cin >> n;
int mảng [n];
for (int i = 0; i <n; i ++) {
std :: cin >> mảng ;
}
int x;
std :: cin >> x;
int index = BinarySearch (mảng, n, x);
if (index == -1) {
std :: cout << "phần tử không tìm thấy" << std :: endl;
} khác {
std :: cout << "phần tử được tìm thấy tại index" << index << std :: endl;
}
trả lại 0;
}
`` `

### Đọc thêm

* [Hướng dẫn lập trình C ++] (C++ Tutorial)
* [Bài tập lập trình C ++] (Learn C++ | Codecademy)
* [Các vấn đề lập trình C ++] (Problems - LeetCode)
=======================================
### 450 C++ Programming Exercises

#C++ #Programming #exercises #tutorial

**1. Write a program to print Hello World!**

```c++
#include <iostream>

int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}
```

**2. Write a program to add two numbers**

```c++
#include <iostream>

int main() {
int a, b;
std::cin >> a >> b;
int sum = a + b;
std::cout << "Sum = " << sum << std::endl;
return 0;
}
```

**3. Write a program to find the area of a circle**

```c++
#include <iostream>

const double pi = 3.14159;

int main() {
double radius;
std::cin >> radius;
double area = pi * radius * radius;
std::cout << "Area = " << area << std::endl;
return 0;
}
```

**4. Write a program to sort an array of integers**

```c++
#include <iostream>
#include <algorithm>

int main() {
int n;
std::cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
std::cin >> arr;
}
std::sort(arr, arr + n);
for (int i = 0; i < n; i++) {
std::cout << arr << " ";
}
std::cout << std::endl;
return 0;
}
```

**5. Write a program to implement a binary search**

```c++
#include <iostream>

int binarySearch(int arr[], int n, int x) {
int low = 0;
int high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == x) {
return mid;
} else if (arr[mid] < x) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}

int main() {
int n;
std::cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
std::cin >> arr;
}
int x;
std::cin >> x;
int index = binarySearch(arr, n, x);
if (index == -1) {
std::cout << "Element not found" << std::endl;
} else {
std::cout << "Element found at index " << index << std::endl;
}
return 0;
}
```

### Further Reading

* [C++ Programming Tutorial](https://www.tutorialspoint.com/cplusplus/index.htm)
* [C++ Programming Exercises](https://www.codecademy.com/learn/learn-c-plus-plus)
* [C++ Programming Problems](https://leetcode.com/problemset/all/?topicSlugs=c%2B%2B)
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top