取自PAT的一道题:
题意很简明,直接贴出代码:
#include <stdio.h> #include <stdlib.h> #include <assert.h> #include <math.h> int is_prime(int n) //判断是否为素数 { int i = 0; assert(n >= 2); for (i = 2; i <= sqrt(n); i++) { if (n % i == 0) return 0; } return 1; //是素数 } int main() { int n = 0, count = 0, i = 0, tmp = 1; while (scanf("%d", &n) != EOF) { for (i = 2; (i <= n); i++) { if (is_prime(i)) { if (i - tmp == 2) count++; tmp = i; } } printf("%d\n", count); } system("pause"); return 0; }
素数对猜想
最新推荐文章于 2022-02-26 16:49:43 发布