Some C/C ++ algorithm applets

Source: Internet
Author: User

1. Print ASCII characters:

# Include <iostream>
# Include <iomanip>
Using namespace STD;

Class table
{
Public:
Table (int p): I (p ){}
Void ASCII ();
Protected:
Int I;
};

Void table: ASCII ()
{
Int K = 1;
For (; I <= 128; I ++)
{
Cout <SETW (4) <I <''<(char) I;
If (! (K % 12 ))
Cout <Endl;
K ++;
}
Cout <Endl;
}

Void main ()
{
Table tbl1 (1 );
Tbl1.ascii ();
Cout <Endl;

Table tbl2 ('A ');
Tbl2.ascii ();
}


2.

Factorial of n (n !) Quick estimation of the number of zeros at the end

Generally, with the increase of the value n, the number of zeros at the end of the factorial N increases, and the number of zeros at the end can be accurately calculated by N. However, this computation is usually too complex, this article provides a way to quickly estimate n! The zero-number method at the end is easy to use and has a small error.

Example: 30! = 265252859812191058636308480000000

Count, and there are seven zeros at the end.

Another example: 100! =
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

Count, with 24 zeros at the end.


So let's ask, n! How many zeros are there at the end?

Answer: You can use (n-1)/4 and take an integer to estimate (or simply use N/4 to estimate ).

As shown in the above 30! There are (30-1)/4 = 7 zeros at the end, and 100! There are (100-1)/4 = 24 zeros at the end.


Imagine: first obtain 100! And then count the number of zeros at the end. As a matter of fact, it is impossible for a computer to represent a limited range of integers.
To solve this problem, we must first analyze it in mathematics at 100! The condition that produces zero at the end of the result value. It is not hard to see: if an integer contains a factor 5, it will inevitably be 100! Generate a zero value. Therefore, the question is how many factors 5 are contained in the 100 integers from 1 to 100. If integer N can be divisible by 25, N contains 2 factors 5. If integer N can be divisible by 5, N contains 1 Factor 5.
*ProgramDescription and comment
# Include <stdio. h>
Int
Main ()
{
Int A, Count = 0;
For (A = 5; A <= 100; A + = 5)
// The cycle starts from 5 and takes a multiple of 5 as the step to evaluate the integer
{
+ + Count; // if it is a multiple of 5, add 1 to the counter
If (! (A % 25) + + count;
// If it is a multiple of 25, add 1 to the counter
}
Printf ("the number of 0 in the end of 100! Is:
% D./N ", count); // print the result
Return 0;
}

* Running result
The number of 0 in the end of 100! Is: 24.

* Further Discussion

The solution to this question is correct, but there are obvious disadvantages. The method used in the program to determine the number of factors 5 contained in integer N is related to the 100 in the program. If the 100 in the question is changed to 1000, it is necessary to modify the number of Factor 5 in the program.Algorithm.

3. How to quickly find the number of 1 in an 8 bits number requires the algorithm to be as efficient as possible!

# Include
# Define byte unsigned char
/* Define a search table */
Byte numtable [256] =
{
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3,
3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 4, 4, 5, 2, 3, 3,
4, 3, 4, 4, 5, 3, 4, 4, 5, 5, 5, 6, 1, 2, 2, 3, 2, 3, 4, 2, 3, 3, 4,
3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 5, 6, 2, 3, 3, 4, 3,
4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6,
6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4,
5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 6, 7, 2, 3, 4, 3, 4, 4, 5, 3,
4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 3, 4,
4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6,
7, 6, 7, 7, 8
};
Int main (INT argc, char * argv [])
{
Int I, num = 0;
Byte a = 0;
/* Receive user input */
Printf ("/nplease input a byte (0 ~ 255 ):");
Scanf ("% d", & );
/* Calculate the number of 1 */
/* Use byte directly as the subscript of the array to retrieve the number of 1! */
Printf ("/nthe num of 1 in the byte is % d", checknum [a]);
Return 0;
}



Int main ()
{
Int X;
Cin> X;
If (x> 255)
{
Cout <"error! "<Endl;
Return-1;
}
Int CNT = 0;
If (X & 128) = 128)
CNT ++;
If (X & 64) = 64)
CNT ++;
If (X & 32) = 32)
CNT ++;
If (X & 16) = 16)
CNT ++;
If (X & 8) = 8)
CNT ++;
If (X & 4) = 4)
CNT ++;
If (X & 2) = 2)
CNT ++;
If (X & 1) = 1)
CNT ++;
Cout <CNT <Endl;
Return 0;
};



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.