Từ 2025 - Lớp 7 - 8 (tối thứ 5), 2025 Lớp Tin học 8.3 (tối thứ 5)
Thông tin
include <iostream>
using namespace std;
int main() { int a, b = 0; cin >> a; for (int i = 1; i <= a; i++) { if (a % i == 0) { bool P = true; if (i < 2) P = false; for (int j = 2; j * j <= i; j++) { if (i % j == 0) { P = false; break; } } if (P==true) { b = i; } } } cout << b; } tìm kiếm nhị phân
include <iostream>
using namespace std;
int main() { int x, n; cin >> x >> n; int a[n + 1]; for (int i = 1; i <= n; i++) { cin >> a[i]; } int l = 1, r = n, mid; while (l <= r) { mid = (l + r) / 2; if (a[mid] == x) { cout << mid;
} else if (a[mid] > x) {
r = mid - 1;
} else {
l = mid + 1;
}
}
cout << "-1";
return 0;
}