本文主要是介绍1013 数素数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
solution1
直接判断
#include<stdio.h>
#include<math.h>
int main(){int n, m, count = 0, flag, c = 0;scanf("%d%d", &m, &n);for(int i = 2; count < n; i++){flag = 0;for(int j = 2; j <= sqrt(i * 1.0); j++){if(i % j == 0){flag = 1; break;}}if(!flag){count++;if(count >= m){c++;printf("%d", i);if(c % 10 != 0 && c != n - m + 1) printf(" ");if(c % 10 == 0) printf("\n");}}}return 0;
}
solution2 埃氏筛法
#include<stdio.h>
const int maxN = 1000001;
int main(){int hash[maxN] = {0}, p[maxN], pos = 0, n, m, count = 0;scanf("%d%d", &m, &n);for(int i = 2; i < maxN; i++){if(!hash[i]){p[pos++] = i;if(pos >= n) break;for(int j = 2 * i; j < maxN; j += i) hash[j] = 1;}}for(int i = m - 1; i < n; i++){count++;printf("%d", p[i]);if(count % 10 != 0 && count != n - m + 1) printf(" ");if(count % 10 == 0) printf("\n");}return 0;
}
这篇关于1013 数素数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!