#include<stdio.h>
int main() {
void judge(int x);
int n=3;
scanf_s("%d", n);
judge(n);
return 0;
}
void judge(int x) {
for (int i = 2; i < x; i++) {
if (x % i == 0)printf("不是素数");
else if (i < x)printf("不是素数");
else printf("%d是素数", x);
}
}
函数的调用出什么问题了
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
关注 素数的判断之前回答过类似的问题。
你的代码需要稍微改进一下会更好。
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。 // #define _CRT_SECURE_NO_WARNINGS #include "stdafx.h" #include <Windows.h> #include <iostream> #include <vector> #include <iostream> #include <cstdlib> #include <ctime> #include <cstring> #include <iostream> #include <cctype> #include <vector> #include <algorithm> #include <math.h> using namespace std; void judge(int x) { int isPrime = 1; // 从 2 到 x 的平方根检查可除性 for (int i = 2; i <= sqrt(x); i++) { if (x % i == 0) { isPrime = 0; break; } } if (isPrime) { printf("%d是素数", x); } else { printf("不是素数"); } } int _tmain(int argc, _TCHAR* argv[]) { int n; scanf_s("%d", &n); judge(n); system("pause"); return 0; }
运行后判断是否为素数;
解决 无用评论 打赏 举报 编辑记录
悬赏问题
- ¥30 电脑误删了手机的照片怎么恢复?
- ¥15 (标签-python|关键词-char)
- ¥15 python+selenium,在新增时弹出了一个输入框
- ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。
- ¥20 学生成绩管理系统设计
- ¥15 来一个cc穿盾脚本开发者
- ¥15 CST2023安装报错
- ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办
- ¥15 有人懂怎么做大模型的客服系统吗?卡住了卡住了
- ¥20 firefly-rk3399上启动卡住了