Classical, practical, and interesting programming examples in C/C ++ (3)

Source: Internet
Author: User
Tags mul
Number of BITs

 

If n is a four-digit number, it is 9 times its inverse number, and N is obtained. The inverse number is an integer formed by the number of integers. For example, the arc number of 1234 is 4321.

* Problem analysis and Algorithm Design
It can be set to thousands, hundreds, and ten of integers. Each digit is I, J, K, and L. The values are 0 ~ 9, the relationship is met:
(I * 103 + J * 102 + 10 * k + l) * 9 = (L * 103 + K * 102 + 10 * j + I)
I, J, K, and l constitute n.

* Program description and comment
# Include <stdio. h>
Int main ()
{
Int I;
For (I = 1002; I <1111; I ++)/* the possible value of the four-digit enumeration */
If (I % 10*1000 + I/10% 10*100 + I/100% 10*10 + I/1000 = I * 9)
/* Determine whether the ordinal number is 9 times the original integer */
Printf ("the number satisfied stats condition is: % d/N", I );
/* Output if yes */
}

* Running result
The number satisfied States condition is: 1089

22. Speed

A car with a fixed speed, the driver saw at ten o'clock A.M. that the reading on the ODPS table was a symmetric number (that is, the number reads from left to right and from right to left completely the same), 95859. Two hours later, a new symmetric number appeared on the ODPS table. How fast is the car? What is the new symmetric number?

* Problem analysis and Algorithm Design
Based on the question, set the symmetric number to I, and the initial value to 95589, and increment the value in sequence. After each bit of the I value is decomposed, it is compared with the number at the symmetric position, if the number at each symmetric position is equal, you can determine that I is the symmetric number.

* Program description and comment
# Include <stdio. h>
Int main ()
{
Int t, a [5];/* array a stores the decomposed digits */
Long int K, I;
For (I = 95860; I ++)/* uses 95860 as the initial value and cyclically tests */
{
For (t = 0, K = 100000; k> = 10; t ++)/* Each digit of the I value obtained from high to low decomposition */
{/* Characters, which are sequentially stored in a [0] ~ A [5] in progress */
A [T] = (I % K)/(K/10 );
K/= 10;
}
If (A [0] = A [4]) & (A [1] = A [3])
{
Printf ("the New Parameter rical number kelometers is: % d/N ",
A [0], a [1], a [2], a [3], a [4]);
Printf ("the velocity of the car is: %. 2f/N", (i-95859)/2.0 );
Break;
}
}
}

* Running result
The new parameter rical number kelometers is: 95959.
The velocity of the car is 50.00

* Questions
The new number obtained by reversing the number is called the reverse sequence number of the original number. If a number is equal to its reverse order number, it is called a symmetric number. Calculate the maximum number of binary symmetry values not greater than 1993.

 

23. Two square three-digit numbers are used to obtain three square two-digit numbers.

 

It is known that two square three digits are ABC and xyz, where A, B, C, X, Y, and Z may not be different, while ax, by, and CZ are three square two digits. Calculate the three-digit ABC and XYZ by programming.

* Problem analysis and Algorithm Design
Take two square three-digit numbers N and N1, resolve N from high to low to A, B, and C, and split N1 from high to low to x, y, and z. Determine whether ax, by, and CZ are all full records.

* Program description and comment
# Include <stdio. h>
# Include <math. h>
Void F (int n, float * s );
Int main ()
{
Int I, T;
Float a [3], B [3];
Print ("the possible perfect squares combinations are:/N ");
For (I = 11; I <= 31; ++ I) // The value range of the three-digit number in the left-side navigation pane.
For (t = 11; t <= 31; ++ T)
{
F (I * I, a); // everyone who splits the Square three digits, each of which is stored in an array.
F (t * T, B );
If (SQRT (A [0] * 10 + B [0]) = (INT) SQRT (A [0] * 10 + B [0])
& SQRT (A [1] * 10 + B [1]) = (INT) SQRT (A [1] * 10 + B [1])
& SQRT (A [2] * 10 + B [2]) = (INT) SQRT (A [2] * 10 + B [2])
{
Printf ("% d and % d/N, I * I, T * t"); // if all three new numbers are full records, the output
}
}
}

 

/*----------------
Break down the three-digit n numbers, and store the numbers from high to low to the array pointed to by the pointer S.
----------------*/

Void F (int n, float * s)
{
Int K;
For (k = 1000; k> = 10; ++ S)
{
* S = (N % K)/(K/10 );
K/= 10;
}
}

* Running result
The possible perfect squares combinations are:
400 and 900
841 and 196

* Questions

Calculate such a three-digit number. The three-digit number is equal to the sum of the factorial values of each digit.
ABC =! + B! + C!
(Correct results: 145 = 1! + 4! + 5 !)

 

24. Armstrong count

 

If a positive integer is equal to the Cube sum of each number, it is called the number of armlands (also known as the number of narcissism ).
For example, 407 = 43 + 03 + 73 is a number of Armstrong. Try programming to calculate the number of all Armstrong numbers within 1000.

* Problem analysis and Algorithm Design
The exhaustive method can be used to calculate and determine the numbers (set as I) of less than 1000. After the numbers of I are decomposed, the calculation and determination are performed based on the nature of the numbers.

* Program description and comment
# Include <stdio. h>
Int main ()
{
Int I, T, K, a [3];
Printf ("there are follwing Armstrong number smaller than 1000:/N ");
For (I = 2; I <1000; I ++)/* The value range of the number I to be determined is 2 ~ 1000 */
{
For (t = 0, K = 1000; k> = 10; t ++)/* intercept integer I (from high to low )*/
{
A [T] = (I % K)/(K/10);/* assigned to a [0] ~ A [2 }*/
K/= 10;
}
If (A [0] * A [0] * A [0] + A [1] * A [1] * A [1] + A [2] * A [2] * A [2] = I)
/* Determine if I is an integer x */
Printf ("% 5d", I);/* if the conditions are met, output */

}
Printf ("/N ");
}

* Running result
There are following Armstrong number smaller than 1000:
153 370 371 407

25. Full count

 

If a number is equal to the sum of its factors, the number is called "full number ".

* Problem analysis and Algorithm Design
Based on the definition of the complete number, calculate the selected integer A (the value of A is 1 ~ 1000), and accumulate each factor in M. If M is equal to a, it can be confirmed that A is a full number.

* Program description and comment
# Include <stdio. h>
Int main ()
{
Int A, I, m;
Printf ("there are following perfect numbers smaller than 1000:/N ");
For (A = 1; A <1000; A ++)/* select 1 for loop control ~ Determine the numbers in 1000 */
{
For (m = 0, I = 1; I <= A/2; I ++)/* calculates the factor of A, and sums the factors M =, then a is a full number output */
If (! (A % I) m + = I;
If (M =)
Printf ("% 4D", );
}
Printf ("/N ");
}

* Running result
TThere are following perfect numbers smaller than 1000:
6 28 496

 

26. Intimacy

If the sum of all factors of integer a (including 1, excluding a itself) is equal to B, and the sum of all factors of integer B (including 1, excluding B itself) is equal to, the integers A and B are called intimacy numbers. Calculate the number of all intimacy values within 3000.

* Problem analysis and Algorithm Design
According to the definition of intimacy number, to determine whether there is intimacy number A, as long as the sum of all the factors of A is calculated as B, then the sum of all the factors of B is calculated as N, if n is equal to a, it can be determined that a and B are intimate numbers. Algorithm for calculating the various factors of number:
Use a to perform I (I = 1 ~ A/2) Perform a modulo operation. If the modulo operation result is equal to 0, I is a factor of a; otherwise, I is not a factor of.

* Program description and comment
# Include <stdio. h>
Int main ()
{
Int A, I, B, N;
Printf ("there are following friendly-numbers pair smaller than 3000:/N ");
For (A = 1; A <3000; A ++)/* All integers less than 1000 x */
{
For (B = 0, I = 1; I <= A/2; I ++)/* calculate each factor of number A, and the sum of each factor is stored in B */
If (! (A % I) B + = I;/* calculate each factor of B, and the sum of each factor is stored in N */
For (n = 0, I = 1; I <= B/2; I ++)
If (! (B % I) N + = I;
If (n = A & A <B)
Printf ("% 4D .. % 4D", a, B);/* If n = A, A and B are a pair of intimacy numbers and output */
}
}

* Running result
There are following friendly-numbers pair smaller than 3000:
220 .. 284 1184 .. 1210 2620 .. 2924

27. Self-defense

The number of self-defense means that the ending number of the square of a number is equal to the natural number of The number. For example:
252 = 625 762 = 5776 93762 = 87909376
Number of self-defense requests out of 200000

* Problem analysis and Algorithm Design
If you use the "obtain the square of a number and then extract the last corresponding digits" method, it is obviously not desirable, because the computer cannot represent an excessively large integer.
Analyze the calculation process of the integer square (multiplication) in the manual mode. Take 376 as an example:
376 Multiplier
X 376 Multiplier
----
2256 first part product = reciprocal first of the multiplier *
2632 product of the Second Part = multiplier * the second to the last of the Multiplier
1128 third part product = multiplier * the last third digit of the Multiplier
----
141376 Product
This issue is concerned with the last three digits of the product. By analyzing the process of generating the last three digits of the product, we can see that in each part of the product, not every digit of the product has an impact on the last three digits of the product. The sum-up rule can be obtained: in the three-digit multiplication, the credits that affect the last three digits of the product are as follows:
In the first part of the accumulation process: the last three digits of the multiplier * the reciprocal of the Multiplier
The second part is in accumulation: the last two digits of the multiplier * the second to the last of the Multiplier
Third part: accumulation: the last digit of the multiplier * the last third digit of the Multiplier
After the sum of the last three digits of the product is obtained, the last three digits are the last three digits of the product of the three digits. Such a rule can be extended to the product of different digits for the same problem.
You can design algorithms to write programs based on the manual calculation process.

* Program description and comment
# Include <stdio. h>
Int main ()
{
Long Mul, number, K, ll, KK;
Printf ("It exists following automorphic nmbers small than 200000:/N ");
For (number = 0; number <200000; number ++)
{
For (Mul = number, k = 1; (Mul/= 10)> 0; K * = 10 );
/* The factor K when the number is intercepted by the number of digits for multiplication */
Kk = K * 10;/* KK is the coefficient of partial product truncation */
Mul = 0;/* The last n digits of the product */
LL = 10;/* ll is the coefficient when the corresponding digit of the truncation multiplier */
While (k> 0)
{
Mul = (Mul + (Number % (K * 10) * (Number % ll-Number % (LL/10) % KK;
/* (Partial product + truncates the last n digits of the multiplier * truncates the MTH digit of the multiplier), % KK intercepts the partial product */
K/= 10;/* k is the factor when the multiplier is intercepted */
Ll * = 10;
}
If (number = Mul)/* output if the value is self-defense */
Printf ("% lD", number );
}
}

* Running result
It exsts following automorphic numbners smaller than 200000:
0 1 5 6 25 76 376 625 9376 90625

28. number of replies

Print all symmetric numbers (also called the return number) of the square of n (n <256 ).

* Problem analysis and Algorithm Design
For the number n to be judged, calculate its square value (stored in a) and separate each bit of, then, restore a to a number K in the ascending order of A (for example, n = 13, A = 169 and K = 961 ), if a is equal to K, N can be determined as the number of return records.

* Program description and comment

The original program seems to be wrong and confusing. The modifications based on the original program are as follows (if the reader still finds an error, please submit it ):

# Include <stdio. h>
Int main (void)
{
Int M [16], n, I, T, Count = 0;
Long unsigned A, K;
Printf ("No. Number it's square (palindrome)/n ");
For (n = 1; n <256; n ++)/* The value range of exhaustive N */
{
K = 0; t = 1; A = N * n;/* calculate the square of N */

For (I = 0;! = 0; I ++)/* Each digit of A is saved in the array M [0] ~ M [16] */
{
M [I] = A % 10; // This is the bit for getting a, and the entire loop can get each bit
A/= 10;
}

Int J = 0;
For (I-; j <I; j ++, I-) // because all bits of N exist in the array, the following checks whether they are symmetric.
If (M [J]! = M [I]) break; // if one of them is not symmetric, it means it is not symmetric and you can exit.

// If all bits are symmetric, it means they are symmetric, so that the result can be printed.
If (j> = I) printf ("% 2D % 10d % 10d/N", ++ count, N, N * n );

}

Return 0;
}

* Running result
No. Number it's square (palindrome)
1 1 1
2 2 4
3 3 9
4 11 121
5 22 484
6 26 676
7 101 10201
8 111 12321
9 121 14641
10 202 40804
11 212 44944

// The following program is original, wrong, and puzzling
# Include <stdio. h>
Int main (void)
{
Int M [16], n, I, T, Count = 0;
Long unsigned A, K;
Printf ("No. Number it's square (palindrome)/n ");
For (n = 1; n <256; n ++)/* The value range of exhaustive N */
{
K = 0; t = 1; A = N * n;/* calculate the square of N */

For (I = 1;! = 0; I ++)/* Each digit of A is saved in the array M [1] ~ M [16] */
{
M [I] = A % 10; // Security Note: This is a single bit of A, and each bit can be obtained by combining the entire loop, and coexist in the array, in order to determine whether symmetric
A/= 10;
}

For (; I> 1; I -)
{
K + = m [I-1] * t;
T * = 10;
}
If (k = N * n)
Printf ("% 2D % 10d % 10d/N", ++ count, N, N * n );
}

Return 0;
}

 

* Running result
No. Number it's square (palindrome)
1 1 1
2 2 4
3 3 9
4 11 121
5 22 484
6 26 676
7 101 10201
8 111 12321
9 121 14641

29. Find four digits with ABCD = (AB + CD) 2.

The number 3025 has a unique nature: divide it into two segments, namely 30 and 25, and sum it to calculate the square, that is, (30 + 25) 2, exactly equal to 3025 itself. The request is issued with all four digits of this nature.

* Problem analysis and Algorithm Design
The four-digit data has no distribution rule. You can use the exhaustive method to determine all the four-digit data, so as to filter out the four-digit data that meets this rule. For specific Algorithm Implementation, you can take a four-digit number and cut it into two parts. The first two parts are a and the last two parts are B. Then, use the formula to calculate and judge.

* Program description and comment
# Include <stdio. h>
Int main ()
{
Int N, A, B;
Printf ("there are following number with 4 digits satisfied condition/N ");
For (n = 1000; n <10000; n ++)/* The value range of 4-digit N is 1000 ~ 9999 */
{
A = N/100;/* truncate the first two digits of N to */
B = n % 100;/* the last two digits of N are saved in B */
If (a + B) * (a + B) = N)/* determines whether N is the four-digit number that matches the nature specified by the question */
Printf ("% d", N );
}
}

* Running result
There are following numbers with 4 digits satisfied condition:
2025 3025 9801

30. Evaluate the prime number

Evaluate 1 ~ In the prime number table ~ All prime numbers between 1000

* Problem analysis and Algorithm Design
A prime number is an integer that can only divide the split 1 and itself. To determine whether an integer N is a prime number, it is to determine whether integer N can be any integer except 1 and itself. If integer n cannot be divisible, then n is a prime number.
During program design, I can start from 2 to 1/2 of the integer N, and use I to remove the Integers to be determined in sequence, as long as there is a situation where the number can be divisible, you can confirm that the integer to be judged is not a prime number; otherwise, it is a prime number.

* Program description and comment
# Include <stdio. h>
Int main ()
{
Int N1, nm, I, j, flag, Count = 0;
Do {
Printf ("input start and end =? ");
Scanf ("% d", & N1, & nm);/* input the range of prime numbers */
} While (! (N1> 0 & N1 <Nm);/* enter the correct range */
Printf ("........... Prime table (% d-% d )............ /N ", N1, nm );
If (n1 = 1 | n1 = 2)/* process Prime Number 2 */
{
Printf ("% 4D", 2 );
N1 = 3; count ++;
}
For (I = N1; I <= Nm; I ++)/* determines whether the integer in the specified range is a prime number */
{
If (! (I % 2) continue;
For (flag = 1, j = 3; flag & J <I/2; j + = 2)
/* Determine whether it can be divisible by a certain number in the half of the integer from 3 */
If (! (I % J) Flag = 0;/* If divisible, it is not a prime number */
If (FLAG) printf (++ count % 15? "% 4D": "% 4D/N", I );
}
}

* Questions
Please find out the ten smallest continuous natural numbers, all of which are union numbers (non-prime numbers)

 

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.