C ++ Programming Exercise 1-return Prime Number

Source: Internet
Author: User

Description
If a number is the same as a number from left to right and a number from right to left, it is called the number of replies. For example, 121,1221 and 15651 are the number of replies. Given the number of digits n, find all the n-digit decimal numbers that are both the return number and the prime number. (Note: If the integer value range is exceeded ).
Input
Number of digits n, where 1 <= n <= 9.
Output
The first line outputs the number of prime numbers that meet the conditions.
The second line outputs all prime numbers that meet the conditions in ascending order. Separate them with a space.
Sample Input
1
Sample Output
4
2 3 5 7
Reference Code
View plaincopy to clipboardprint?
# Include <cstdio>
# Include <cmath>
# Include <cstdlib>
# Include <string>
Using namespace std;
Int result [5960];
Int r_s = 0;
Int digit [10];
Int a, B;
Int pm [10000];
Int pm_size = 0;
Void init ();
Class MyString: public string {
Public:
MyString (int n );
Bool isPlalindrome ();
Private:
String ds;
};
MyString: MyString (int n ){
While (n ){
Char ch = (n % 10) + '0 ';
N/= 10;
Ds. push_back (ch );
}
}
Bool MyString: isPlalindrome (){
Int len = ds. length ();
Int mid = len/2;
For (int I = 0; I <mid; ++ I ){
If (ds [I]! = Ds [len-I-1]) {
Return false;
}
}
Return true;
}
Void work (int c)
{
Int p = (c-1)> 1, I, j, s = 1, r, t1, t2;
If (c = 1)
{
If (a <= 2 & B> = 2)
Result [r_s ++] = 2;
If (a <= 3 & B> = 3)
Result [r_s ++] = 3;
If (a <= 5 & B> = 5)
Result [r_s ++] = 5;
If (a <= 7 & B> = 7)
Result [r_s ++] = 7;
Digit [c] = 4;
Return;
}
For (I = 0; I <p; I ++)
S * = 10;
For (I = 1; I <10; I + = 2)
{
For (j = 0; j <s; j ++)
{
R = I * s + j;
T1 = j/10;
T2 = p-1;
While (t2)
{
R = r * 10 + (t1% 10 );
T1/= 10;
T2 --;
}
R = r * 10 + I;
If (r <)
Continue;
If (r> B ){
Return;
}
MyString s (r );
If (s. isPlalindrome ()){
Bool bl = true;
For (int p = 0; p <pm_size & pm [p] <r; ++ p ){
If (r % pm [p] = 0 ){
Bl = false;
Break;
}
}
If (bl ){
Result [r_s ++] = r;
Digit [c] ++;
}
}
}
}
}
 
Int main ()
{
Int I, p, c;
A = 2; B = 1000000000;
P = 1;
C = 0;
Init ();
While (p <)
{
C ++;
P * = 10;
}
P/= 10;
While (p <B)
{
If (c = 2)
{
Digit [c] = 1;
Result [r_s ++] = 11;
}
If (c & 1)
Work (c );
C ++;
P * = 10;
}
Int n, start, end;
Scanf ("% d", & n );
Printf ("% d \ n", digit [n]);
Start = (int) pow (10.0, n-1 );
End = (int) pow (10.0, n );
For (I = 0; I <r_s; ++ I ){
If (result [I]> = start & result [I] <= end ){
Printf ("% d", result [I]);
}
}
Printf ("\ n ");
Return 0;
}
Void init (){
For (int p = 2; p <= 100000; ++ p ){
Bool bl = true;
For (int k = 2; k <= sqrt (1.0 * p); ++ k ){
If (p % k = 0 ){
Bl = false;
Break;
}
}
If (bl ){
Pm [pm_size ++] = p;
}
}
For (int I = 0; I <10; ++ I ){
Digit [I] = 0;
}
}
# Include <cstdio>
# Include <cmath>
# Include <cstdlib>
# Include <string>
Using namespace std;
Int result [5960];
Int r_s = 0;
Int digit [10];
Int a, B;
Int pm [10000];
Int pm_size = 0;
Void init ();
Class MyString: public string {
Public:
MyString (int n );
Bool isPlalindrome ();
Private:
String ds;
};
MyString: MyString (int n ){
While (n ){
Char ch = (n % 10) + '0 ';
N/= 10;
Ds. push_back (ch );
}
}
Bool MyString: isPlalindrome (){
Int len = ds. length ();
Int mid = len/2;
For (int I = 0; I <mid; ++ I ){
If (ds [I]! = Ds [len-I-1]) {
Return false;
}
}
Return true;
}
Void work (int c)
{
Int p = (c-1)> 1, I, j, s = 1, r, t1, t2;
If (c = 1)
{
If (a <= 2 & B> = 2)
Result [r_s ++] = 2;
If (a <= 3 & B> = 3)
Result [r_s ++] = 3;
If (a <= 5 & B> = 5)
Result [r_s ++] = 5;
If (a <= 7 & B> = 7)
Result [r_s ++] = 7;
Digit [c] = 4;
Return;
}
For (I = 0; I <p; I ++)
S * = 10;
For (I = 1; I <10; I + = 2)
{
For (j = 0; j <s; j ++)
{
R = I * s + j;
T1 = j/10;
T2 = p-1;
While (t2)
{
R = r * 10 + (t1% 10 );
T1/= 10;
T2 --;
}
R = r * 10 + I;
If (r <)
Continue;
If (r> B ){
Return;
}
MyString s (r );
If (s. isPlalindrome ()){
Bool bl = true;
For (int p = 0; p <pm_size & pm [p] <r; ++ p ){
If (r % pm [p] = 0 ){
Bl = false;
Break;
}
}
If (bl ){
Result [r_s ++] = r;
Digit [c] ++;
}
}
}
}
}

Int main ()
{
Int I, p, c;
A = 2; B = 1000000000;
P = 1;
C = 0;
Init ();
While (p <)
{
C ++;
P * = 10;
}
P/= 10;
While (p <B)
{
If (c = 2)
{
Digit [c] = 1;
Result [r_s ++] = 11;
}
If (c & 1)
Work (c );
C ++;
P * = 10;
}
Int n, start, end;
Scanf ("% d", & n );
Printf ("% d \ n", digit [n]);
Start = (int) pow (10.0, n-1 );
End = (int) pow (10.0, n );
For (I = 0; I <r_s; ++ I ){
If (result [I]> = start & result [I] <= end ){
Printf ("% d", result [I]);
}
}
Printf ("\ n ");
Return 0;
}
Void init (){
For (int p = 2; p <= 100000; ++ p ){
Bool bl = true;
For (int k = 2; k <= sqrt (1.0 * p); ++ k ){
If (p % k = 0 ){
Bl = false;
Break;
}
}
If (bl ){
Pm [pm_size ++] = p;
}
}
For (int I = 0; I <10; ++ I ){
Digit [I] = 0;
}
}

Author: "ice is cold: read more, write more, and sleep more ."

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.