Hãy nhập câu hỏi của bạn vào đây, nếu là tài khoản VIP, bạn sẽ được ưu tiên trả lời.
#include<bits/stdc++.h>
using namespace std;
bool isPrime(int n) {
if(n <= 1) return false;
for(int i = 2; i <= sqrt(n); i++) {
if(n % i == 0) return false;
}
return true;
}
void printPrimes(int arr[], int n) {
for(int i = 0; i < n; i++) {
if(isPrime(arr[i])) {
cout << arr[i] << " la so nguyen to" << endl;
}
}
}
int main() {
int arr[] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = sizeof(arr) / sizeof(arr[0]);
printPrimes(arr, n);
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int findSecondLargest(int arr[], int n) {
sort(arr, arr + n, greater<int>());
return arr[1];
}
int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]);
int secondLargest = findSecondLargest(arr, n);
cout << "So lon thu hai trong mang la: " << secondLargest << endl;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int sumOfOddElements(int arr[], int n) {
int sum = 0;
for(int i = 0; i < n; i++) {
if(arr[i] % 2 != 0) {
sum += arr[i];
}
}
return sum;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int sumOfOdd = sumOfOddElements(arr, n);
cout << "Tong cac phan tu le cua mang la: " << sumOfOdd << endl;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
void sortAscending(int arr[], int n) {
sort(arr, arr + n);
}
int main() {
int arr[] = {10, 5, 8, 7, 6};
int n = sizeof(arr) / sizeof(arr[0]);
sortAscending(arr, n);
for(int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
string A, B;
getline(cin, A);
getline(cin, B);
int count = 0;
for (int i = 0; i <= B.length() - A.length(); i++) {
bool found = true;
for (int j = 0; j < A.length(); j++) {
if (B[i+j] != A[j]) {
found = false;
break;
}
}
if (found) {
count++;
}
}
cout << count;
return 0;
}
#include <bits/stdc++.h>
#include <cmath>
int main() {
double x = 16.0;
double squareRoot = sqrt(x);
std::cout << "Căn bậc hai của " << x << " là: " << squareRoot << std::endl;
return 0;
}
#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
const int maxN = 1e4 + 1;
int n, k, arr[maxN];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for(int i = 0; i < n; i++) cin >> arr[i];
sort(arr, arr + n);
cout << arr[k - 1];
}
Input:
7 4
29 847 1928 45 298 918219 38129
Output: 847
#include<bits/stdc++.h>
using namespace std;
void distinctElements(int arr[], int n) {
int i, j, count = 1;
sort(arr, arr + n);
for(i = 0; i < n - 1; i++) {
if (arr[i] != arr[i + 1]) {
count++;
out << arr[i] << " ";
}
}
cout << arr[n - 1] << " ";
cout << "\nSo phan tu khac nhau cua mang la: " << count << endl;
}
int main() {
int arr[] = {1, 2, 3, 4, 4, 5, 6, 6, 7, 7, 7, 8, 9, 9};
int n = sizeof(arr) / sizeof(arr[0]);
distinctElements(arr, n);
return 0;
}
67656