华科14_1
来源:互联网 发布:淘宝金牌卖家入口 编辑:程序博客网 时间:2024/05/20 18:19
问题:
编程找出所有的4位超级素数。每行输出6个数,两个数之间空格分隔。
超级素数:一个n位超级素数是指一个n位正整数,它的前1位、前2位、、、前n位都数。如2333是一个4位超级素数,因为2,23,233,2333都是素数。
分析:
利用素数筛选法先筛选出4位数以内的所有的素数,然后在其中寻找满足4位超级素数的数来。
代码:
#include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <vector> #include <queue> #include <stack> #include <map> #include <string> #include <algorithm> #include <iomanip>#define MAX 10000using namespace std;/* run this program using the console pauser or add your own getch, system("pause") or input loop */int prime[MAX];bool mark[MAX];int length = 0;void init() {memset(mark,false,sizeof(mark));for(int i=2;i<MAX;i++){if(mark[i] == true){continue;}mark[i] = true;prime[length++] = i;for(int j=i*i;j>= 0 && j<MAX;j+=i){mark[j] = true;}}}bool isPrime(int x){for(int i=0;i<length;i++){if(x == prime[i]){return true;}}return false;}int main(int argc, char** argv) {//1.预处理 先筛选出一定范围内的素数init();//2.在筛选出的素数中找到所有4位素数,判断是否满足条件进行输出 int count=0;for(int i=0;i<length;i++){if(prime[i] / 1000 == 0){continue; //不够4位 }if(isPrime(prime[i]/1000) && isPrime(prime[i]/100) && isPrime(prime[i]/10) && isPrime(prime[i])){if(count == 6){printf("%d",prime[i]);}else{printf("%d ",prime[i]);}count++;}if(count == 6){count=0;printf("\n");}} return 0;}
阅读全文
0 0
- 华科14_1
- 华科15_1
- 14_1阅读程序
- 【Java基础14_1】正则表达式
- 14_1到1000之间的完全数
- java _1
- js_trim()_1
- c++_1
- 经典语录_1
- 传知_1
- 练习_1
- 杂记_1
- 4_1
- 3_1
- 11_1
- 8583_1
- 创业_1
- 软件工程_1
- 冒泡排序
- 算法题
- 日记day1
- leetcode-8-字符串转数字atoi
- poj3450 Corporate Identity(后缀数组+二分答案)
- 华科14_1
- git入门---30分钟学会使用git
- 蓝牙BLE LINK LAYER剖析(一) -- status and channel
- Fzu 2261 浪里个浪【思维+最短路】
- 封装
- iOS QQ中未读气泡拖拽消失的实现分析(KittenYang)
- 正向代理和反向代理
- 黑莓9930/9900开启电信4g网络,电信卡+3g上网
- 【时间管理】如何记高质量的笔记