局部变量和子函数的运用

输出[m,n]间的所有素数,并且每5个换行,如果区间内不存在素数,输出0

输入


输出


样例输入

3 17

样例输出

3 5 7 11 13
17
#include <math.h>
#include <stdio.h>
char isprime(int n){
 if (n == 1){ return 0; }
 if (n == 2){ return 1; }
 if ((n&1) == 0){ return 0; }
 int i, q;
 q = (int) sqrt(n);
 for (i=3; i<=q; i+=2){
   if (n%i == 0){
    return 0;
   }
 }
 return 1;
}
int main(){
 int x, y, i, j, b;
 while (scanf("%d %d", &x, &y) == 2){
   b = 0; j = 0;
   for (i=x; i<=y; ++i){
    if (isprime(i)){
     b = 1;
     ++j;
     if (j > 1){
      printf(" ");
     }
     printf("%d", i);
     if (j == 5){
      printf("
");
      j = 0;
     }
    }
   }
   if (b == 0){
    printf("0
");
   }
   else{
    if (j != 5){ printf("
"); }
   }
 }
 return 0;
}
原文地址:https://www.cnblogs.com/Lazy-Cat/p/9838508.html