

Hãy luôn nhớ cảm ơn và vote 5*
nếu câu trả lời hữu ích nhé!
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
bool isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
int toNumber(string s) {
int res = 0;
for (char c : s) {
res = res * 10 + (c - '0');
}
return res;
}
void solve(string T) {
int count = 0;
int maxPrime = 0;
string num = "";
for (char c : T) {
if (isdigit(c)) {
count++;
num += c;
} else {
if (!num.empty()) {
int n = toNumber(num); //
if (isPrime(n) && n > maxPrime) {
maxPrime = n;
}
num = "";
}
}
}
if (!num.empty()) {
int n = toNumber(num);
if (isPrime(n) && n > maxPrime) {
maxPrime = n;
}
}
// In kết quả ra file NTMAX.OUT
ofstream fout("NTMAX.OUT");
fout << count << endl;
fout << maxPrime << endl;
fout.close();
}
int main() {
string T;
ifstream fin("NTMAX.INP"); // Đọc dữ liệu từ file NTMAX.INP
fin >> T;
fin.close();
solve(T);
}
Hãy giúp mọi người biết câu trả lời này thế nào?
Bảng tin