The difficulty lies in: 1. You have to understand the question; 2. You have to deal with tedious control. Where to start output, where to end, spaces before each number, empty lines after each row, and so on
Question: Enter n and c. N is the upper limit of possible prime numbers, and the number of output prime numbers depends on the number of prime numbers in 1 --- n. If the number of prime numbers is an even number, 2 * c numbers are output; otherwise, 2 * C-1 numbers are output. And, from 1 --- n
The middle of a prime number is extended to both sides. In other words, the two ends are equal to the number of the output prime numbers as much as possible. If not, the first one is less than the second one. In addition, there is a special case, that is, if the number of prime numbers to be output is greater
The total number of prime numbers in 1 --- n is all output.
Note: In this question, 1 is also a prime number!
AC code:
[Cpp]
# Include <iostream>
Using namespace std;
Bool prime [1001];
Void judge () // find the prime number
{
Memset (prime, false, sizeof (prime ));
Bool flag;
Int tmp;
For (int I = 1; I <= 1000; I ++)
{
Flag = true;
Tmp = (I + 1)/2;
For (int j = 2; j <= tmp; j ++)
{
If (I % j = 0)
{
Flag = false;
Break;
}
}
If (flag)
Prime [I] = true;
}
}
Int main ()
{
Judge ();
Int n, c, I, count, tmp, rem;
While (cin> n> c)
{
Rem = c;
Count = 0;
For (I = 1; I <= n; I ++) // counts the number of prime numbers in 1-n.
{
If (prime [I])
Count ++;
}
C * = 2;
If (count % 2! = 0) // determine the number of outputs. If the number of prime numbers between 1 and N is odd, print 2 * C-1
C --;
Cout <n <"<rem <": "; // format
If (c> = count)
{// This bracket is not specified, and the output result is inexplicable !!!
For (I = 1; I <= n; I ++)
If (prime [I])
Cout <"" <I;
}
Else
{
Rem = 0;
Tmp = (count-c)/2; // find all prime numbers that cannot be printed
For (I = 1; I <= n; I ++)
{
If (prime [I])
{
Rem ++;
If (rem = tmp)
{
Rem = I; // record the last unprintable prime position
Break;
}
}
}
Tmp = 0;
For (I = rem + 1; I <= n; I ++) // locate the prime number from the next position
If (prime [I])
{
Tmp ++;
If (tmp <= c) // print c
Cout <"" <I;
Else
Break;
}
}
Cout <endl; // There is a blank line after each row output
}
Return 0;
}
From ON THE WAY