2025年西北大学计算机考研复试机试真题

2025年西北大学计算机考研复试上机真题

历年西北大学计算机考研复试上机真题

历年西北大学计算机考研复试机试真题

更多学校题目开源地址:https://gitcode.com/verticallimit1/noobdream

N 诺 DreamJudge 题库:输入 “学校名称” 即可筛选该校历年机试真题,题目均在考纲范围内,按难度自动排序。还可搭配《计算机考研机试攻略》刷题,书中题目可通过题号直接在题库中查找。

是不是素数 - 2

题目描述

Time Limit: 1000 ms
Memory Limit: 256 mb

给定一个正整数,请判断其是否为素数(除了1和本身,没有其他约数的数称为素数)。

输入输出格式
输入描述:

输入数据只包含一个整数N(1<N<10000)

输出描述:

如果N是素数请输出yes,否则输出no。

输入输出样例
输入样例#:
5
输出样例#:
yes

代码一

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main(){
  4. int n;cin>>n;
  5. if(n==2){cout<<"yes";return 0;}
  6. bool flag=0;
  7. if(n>2) {
  8. for(int i=2;i<sqrt(n);++i)
  9. {
  10. if(n%i==0)flag=1;continue;
  11. }
  12. }if(flag==1)cout<<"no";
  13. else cout<<"yes";
  14. return 0;
  15. }

代码二

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. bool isPrime(int n){
  4. if(n == 1) return false;
  5. for(int i = 2; i<n; i++){
  6. if(n%i == 0) return false;
  7. }
  8. return true;
  9. }
  10. int main(){
  11. int n;
  12. cin>>n;
  13. if(isPrime(n)) cout<<"yes"<<endl;
  14. else cout<<"no"<<endl;
  15. return 0;
  16. }

代码三

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. bool is_su(int x){
  5. if(x==1) return false;
  6. for(int i=2; i<=sqrt(x); i++)
  7. if(x%i==0) return false;
  8. return true;
  9. }
  10. int main(){
  11. int n;
  12. cin>>n;
  13. if(is_su(n))
  14. cout<<"yes"<<endl;
  15. else
  16. cout<<"no"<<endl;
  17. }
Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐