Question requirements: Input two positive integers a and b, find the prime numbers between [a, b], and output from small to large.
(2 <= a <= b <=1000000),
Output the prime numbers between [a,b], separate the prime numbers with a space, and each output occupies one line.
It is equivalent to outputting data of indeterminate length, the prime numbers are separated by spaces, and there is no space at the end.
I don't know what method you used when you first encountered this problem.
What I thought of is: the first input without spaces, directly printed in the form of %d, and only output in this form once.
Every time after that is printed as space + %d so that there is no space at the end of the output.
Code directly below:
#include <stdio.h>
#include <math.h>
int main()
{
int m, n;
while (~scanf("%d %d", &m, &n))
{
int k = 1;
for (m; m <= n; m++)
{
int b = (int)sqrt(m);
int flag = 1;
for (int a = 2; a <= b; a++)
{
if ((m % a) == 0)
{
flag = 0;
break;
}
}
if (flag == 1)
{
if (k == 1)
{
printf("%d", m);//只输入一次
k = 0;
}
else
printf(" %d", m);
}
}
printf("\n");
getchar();
}
return 0;
}
This way there are no spaces at the end.
I hope this article is helpful to you, if you have any questions, please feel free to correct me.