Run ID | 作者 | 问题 | 语言 | 测评结果 | Time | Memory | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|
113298 | 彭士宝 | 14判断素数III | C++ | Accepted | 1 MS | 268 KB | 749 | 2025-03-15 16:38:53 |
#include <iostream> #include <cmath> // 用于sqrt函数 using namespace std; int main() { int n; cin >> n; // 输入一个整数 // 特殊情况处理 if (n <= 1) { cout << "NO" << endl; // 1以及更小的数不是素数 return 0; } // 判断是否为素数 bool isPrime = true; // 假设n是素数 for (int i = 2; i <= sqrt(n); ++i) { // 只需检查到sqrt(n) if (n % i == 0) { // 如果n能被i整除,则不是素数 isPrime = false; break; // 使用break语句退出循环 } } // 输出结果 if (isPrime) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; }