Small art of C language Circulation

Source: Internet
Author: User
Tags integer division

1. Prime Number judgment

For this, many people may directly write as follows:

 

 

View plaincopy to clipboardprint? Int isPrime (int n) // The function returns 1 to indicate the prime number, and 0 to indicate that it is not the prime number.
{
Int I;
For (I = 2; I <n; I ++)
If (n % I = 0)
Break;
Return I> = n;
}
Int isPrime (int n) // The function returns 1 to indicate the prime number, and 0 to indicate that it is not the prime number.
{
Int I;
For (I = 2; I <n; I ++)
If (n % I = 0)
Break;
Return I> = n;
}

 

Or, some people know the square root optimization:


View plaincopy to clipboardprint? Int isPrime (int n)
{
Int I, s = (int) (sqrt (double) n) + 0.01 );
For (I = 2; I <= s; I ++)
If (n % I = 0)
Break;
Return I> s;
}
Int isPrime (int n)
{
Int I, s = (int) (sqrt (double) n) + 0.01 );
For (I = 2; I <= s; I ++)
If (n % I = 0)
Break;
Return I> s;
}
Or, remove the even number:
View plaincopy to clipboardprint? Int isPrime (int n)
{
Int I, s = (int) (sqrt (double) n) + 0.01 );
If (n <= 3) return 1;
If (n % 2 = 0) return 0;
For (I = 3; I <= s; I + = 2)
If (n % I = 0)
Break;
Return I> s;
}
Int isPrime (int n)
{
Int I, s = (int) (sqrt (double) n) + 0.01 );
If (n <= 3) return 1;
If (n % 2 = 0) return 0;
For (I = 3; I <= s; I + = 2)
If (n % I = 0)
Break;
Return I> s;
}
Of course, if this is not enough, we can consider this fact:
For all prime numbers greater than 4, the remainder divided by 6 can only be 1 or 5
For example, the following 5, 7, 11, 13, 17, and 19 meet

Therefore, we can specifically Judge 2 and 3 first.
However, the problem arises later, because it is not a simple increment, starting from 5 is + 2, + 4, + 2, + 4 ,...
In this case, how should we write a loop?

First, we define a step variable step. The loop is like this (I = 5; I <= s; I + = step)
In this case, each cycle changes the step from 2 to 4 or from 4 to 2.
Therefore, you can write as follows:
View plaincopy to clipboardprint? # Include <stdio. h>
# Include <math. h>
 
Int isPrime (int n)
{
Int I, s = (int) (sqrt (double) n) + 0.01), step = 4;
If (n <= 3) return 1;
If (n % 2 = 0) return 0;
If (n % 3 = 0) return 0;
For (I = 5; I <= s; I + = step)
{
If (n % I = 0)
Break;
Step ^ = 6;
}
Return I> s;
}
 
Int main ()
{
Int n;
For (n = 2; n <100; ++ n) // locate the 2-100 prime number and Output
{
If (isPrime (n) printf ("% d,", n );
}
Getchar ();
Return 0;
}
# Include <stdio. h>
# Include <math. h>

Int isPrime (int n)
{
Int I, s = (int) (sqrt (double) n) + 0.01), step = 4;
If (n <= 3) return 1;
If (n % 2 = 0) return 0;
If (n % 3 = 0) return 0;
For (I = 5; I <= s; I + = step)
{
If (n % I = 0)
Break;
Step ^ = 6;
}
Return I> s;
}

Int main ()
{
Int n;
For (n = 2; n <100; ++ n) // locate the 2-100 prime number and Output
{
If (isPrime (n) printf ("% d,", n );
}
Getchar ();
Return 0;
}
In the above Code, a step ^ = 6 completes step conversion between 2 and 4 (this ^ symbol is an exclusive or operation in C)
The reason is that the binary value of 2 is 010,4 is 100,6 is 110, SO 2 is different or 4 gets 6:
2 ^ 4 => 6
6 ^ 2 => 4
6 ^ 4 => 2

So we can construct a loop in which the step size changes back and forth between two values by using an exclusive or
Question: What we mentioned above is a dual-value loop. How can we construct a three-value or four-value loop?

 

2. Diamond Printing

Many people, the idea of printing a diamond on the console is to split the diamond up and down and print it in two very similar codes,
In fact, this code is not good-looking and hard to read.
We know that the pattern to be printed is like this:
*
***
*****
***
*

Can you create a dual loop that is also symmetric?
It's easy. First, we need to leave our habitual thinking behind. The for loop does not have to start or end at 0.
We can make the cycle from-c to c. Isn't it easy to generate a symmetric one? (Take an absolute value)
We regard the center of the diamond as the coordinate 0, 0. Then, the coordinate of the asterisk is output, which is the point of | x | + | y | <= c.

Therefore
View plaincopy to clipboardprint? # Include <stdio. h>
# Define IABS (x)> = 0? (X):-(x) // defines a macro for calculating the absolute value.
Void print (int size) // size is the radius of the diamond, and the diameter will be size * 2 + 1
{
Int x, y;
For (y =-size; y <= size; y ++)
{
For (x =-size; x <= size; x ++)
{
If (IABS (x) + IABS (y) <= size) // sum of the absolute values of x and y, that is, | x | + | y | <= size
Putchar ('*');
Else
Putchar ('');
}
Putchar ('\ n ');
}
}
 
Int main ()
{
Print (5); // output a diamond with a radius of 5
Getchar ();
Return 0;
}
# Include <stdio. h>
# Define IABS (x)> = 0? (X):-(x) // defines a macro for calculating the absolute value.
Void print (int size) // size is the radius of the diamond, and the diameter will be size * 2 + 1
{
Int x, y;
For (y =-size; y <= size; y ++)
{
For (x =-size; x <= size; x ++)
{
If (IABS (x) + IABS (y) <= size) // sum of the absolute values of x and y, that is, | x | + | y | <= size
Putchar ('*');
Else
Putchar ('');
}
Putchar ('\ n ');
}
}

Int main ()
{
Print (5); // output a diamond with a radius of 5
Getchar ();
Return 0;
}
What if I need a hollow diamond? It is very simple because the points on the boundary of the diamond meet the following requirements: | x | + | y | = c
Therefore, we only need to change the if less than or equal to sign to the double equal sign =.

Similarly, if I don't want the "*" character, I want the outermost layer to be the letter A, and then the second layer to be the "B" character? That is:
A
ABA
ABCBA
ABA
A

Then, we only need to perform a Character Calculation in putchar:
View plaincopy to clipboardprint? Void print (int size) // size is the radius of the diamond, and the diameter will be size * 2 + 1
{
Int x, y;
For (y =-size; y <= size; y ++)
{
For (x =-size; x <= size; x ++)
{
If (IABS (x) + IABS (y) <= size) // sum of the absolute values of x and y, that is, | x | + | y | <= size
Putchar ('A' + (size-IABS (x)-IABS (y); // pay attention to the calculation method here.
Else
Putchar ('');
}
Putchar ('\ n ');
}
}
Void print (int size) // size is the radius of the diamond, and the diameter will be size * 2 + 1
{
Int x, y;
For (y =-size; y <= size; y ++)
{
For (x =-size; x <= size; x ++)
{
If (IABS (x) + IABS (y) <= size) // sum of the absolute values of x and y, that is, | x | + | y | <= size
Putchar ('A' + (size-IABS (x)-IABS (y); // pay attention to the calculation method here.
Else
Putchar ('');
}
Putchar ('\ n ');
}
}
Similarly, if we want to print X:
**
**
*
**
**
You can also use this idea to complete this question.

 

3. Odd-order magic
The so-called Magic Square (the most basic one) is a digital square matrix with the sum of numbers on the diagonal and equal to a constant.
4 3 8
9 5 1
2 7 6

What are the rules of the above figure? Is it easy to write code?

Let's copy the image five times to the right and three times down to expand it:


4 3 8 4 3 8 4 3 8 4 3 8
9 5 1 9 5 1 9 5 1 9 5 1 9 5 1
2 7 6 2 7 6 2 7 6 2 7 6 7 6
4 3 8 4 3 8 4 3 8 4 3 8
9 5 1 9 5 1 9 5 1 9 5 1 9 5 1
2 7 6 2 7 6 2 7 6 2 7 6 7 6
4 3 8 4 3 8 4 3 8 4 3 8
9 5 1 9 5 1 9 5 1 9 5 1 9 5 1
2 7 6 2 7 6 2 7 6 2 7 6 7 6

Note the direction of the blue number
How about now?
It seems a lot more regular now, but do you think the cycle is still not easy to write?
How do we know the coordinates of a given n directly?
It is not difficult to look at the rule and you can find that there are any numbers n + 1 (in the upper left corner, the coordinates are 0, 0 ):
X = 2 + n/3;
Y = 1 + n-n/3;

In fact, this rule can be simply extended to any odd magic square (the following size is an odd number ):
X = size/2 + 1 + n/size; (Note that the division here is the integer division method without decimals)
Y = size/2 + n-n/size;

In this way, we can simplify the original complex loop into a simple loop.

So there is a program:
View plaincopy to clipboardprint? # Include <stdio. h>
# Define SIZE 5 // defines the magic order number, which can only be an odd number
Int main ()
{
Int x, y, I, sqSize, hSize;
Int sqMap [SIZE] [SIZE];
SqSize = SIZE * SIZE;
HSize = SIZE/2;
// Calculate the position from 1 to SIZE * SIZE and record
For (I = 0; I <sqSize; I ++)
{
X = hSize + 1 + I/SIZE;
Y = hSize + I-I/SIZE;
SqMap [y % SIZE] [x % SIZE] = I + 1;
}
// The output is as follows:
For (y = 0; y <SIZE; y ++)
{
For (x = 0; x <SIZE; x ++)
Printf ("% 4d", sqMap [y] [x]);
Puts ("");
}
Return 0;
}
# Include <stdio. h>
# Define SIZE 5 // defines the magic order number, which can only be an odd number
Int main ()
{
Int x, y, I, sqSize, hSize;
Int sqMap [SIZE] [SIZE];
SqSize = SIZE * SIZE;
HSize = SIZE/2;
// Calculate the position from 1 to SIZE * SIZE and record
For (I = 0; I <sqSize; I ++)
{
X = hSize + 1 + I/SIZE;
Y = hSize + I-I/SIZE;
SqMap [y % SIZE] [x % SIZE] = I + 1;
}
// The output is as follows:
For (y = 0; y <SIZE; y ++)
{
For (x = 0; x <SIZE; x ++)
Printf ("% 4d", sqMap [y] [x]);
Puts ("");
}
Return 0;
}
This is much shorter than the code that you can find on the Internet to find an odd-order magic party (but it is called cube matrix on the internet, I don't know why)

 

4. Circular character string Movement

Question: give you a string that requires the loop to shift n places left
For example, if the "abcdefg" loop shifts two places to the left, we need to get "cdefgab"

Additional conditions. Continuous auxiliary space (including Dynamic Allocation) cannot be used, and only a few individual variables (I .e., O (1) space) can be used)

First, we know that reversing a string operation ("abcd" to "dcba") does not require additional array assistance, as long as the first and last data is exchanged.

However, you may not know that using string inversion alone can realize the character string cyclic shift:

 

 

View plaincopy to clipboardprint? // Reverse the string to reverse the content in the center to which the st and ed point (including the st does not contain ed)
Void str_rev (char * st, char * ed)
{
For (-- ed; st <ed; ++ st, -- ed)
{
Char c;
C = * st; * st = * ed; * ed = c;
}
}
// Reverse the string to reverse the content in the center to which the st and ed point (including the st does not contain ed)
Void str_rev (char * st, char * ed)
{
For (-- ed; st <ed; ++ st, -- ed)
{
Char c;
C = * st; * st = * ed; * ed = c;
}
}
View plaincopy to clipboardprint? // Use a three-way inversion to shift the string to the left (between st and ed, including content that st does not contain ed)
Char * str_shl (char * st, char * ed, int n)
{
Str_rev (st, & st [n]);
Str_rev (& st [n], ed );
Str_rev (st, ed );
Return st;
}
// Use a three-way inversion to shift the string to the left (between st and ed, including content that st does not contain ed)
Char * str_shl (char * st, char * ed, int n)
{
Str_rev (st, & st [n]);
Str_rev (& st [n], ed );
Str_rev (st, ed );
Return st;
}

View plaincopy to clipboardprint? # Include <stdio. h>
# Include <string. h>
Int main ()
{
Char str [] = "abcdefghijklmnopqrstuvwxyz ";
Puts (str_shl (str, str + strlen (str), 6 ));
Getchar ();
Return 0;
}
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Char str [] = "abcdefghijklmnopqrstuvwxyz ";
Puts (str_shl (str, str + strlen (str), 6 ));
Getchar ();
Return 0;
}
Here, if you want to shift n places to the left in a loop, you only need to divide the original string into two segments, the first n characters, and other characters

The two sections are reversed separately, and finally the whole is reversed, so the cycle is shifted to the left (if the whole part is first shifted to the right)

Author: "column of Csdn_zc"

Related Article

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.