【编程之美】2.2不要被阶乘吓到
题目: 1:给定一个整数N,那么N的阶乘N!末尾有多少个0?例如N = 10,N!= 3628800,末尾有两个0. 2:求N!的二进制表示中最低位1的位置。 问题一: 题目解析: 这道题如果直接求N!的话也可以,不过万一溢出了怎么办?即使定义longlong类型的也不合适。那
题目:
1:给定一个整数N,那么N的阶乘N!末尾有多少个0?例如N = 10,N!= 3628800,末尾有两个0.
2:求N!的二进制表示中最低位1的位置。
问题一:
题目解析:
这道题如果直接求N!的话也可以,不过万一溢出了怎么办?即使定义longlong类型的也不合适。那么就要找寻其中的规律,一般这类题目都可以通过分析,找到一个很简单的方法。
思路一:
我们想想0是怎么来的?乘以10就增加一个0,而10可以通过2*5的来。好了,我们将N!表达式表达出来,看能获得多少个2*5。N! = 2^x * 3^y * 5^z... 由于2比5小,所以x比z要大。所以看N!中有多少个5就可以了。
int Count(int n) { int num = 0; for(int i = 1;i <br> 思路二: <p><span>我们可以利用公式Z = [N/5] + [N/(5^2)] + [N/(5^3)] +....</span></p> <p><span>这个公式表达什么意思呢?N/5表示从1-N中有多少个数是5的倍数,那么这些数,每一个都贡献一个5;好了但是对于25会贡献两个,在除以5的时候,已经算进去1个,那么N/(5^2)的时候,看看有多少是25的倍数,也算一下,这时将25中的另一个5给算进去了;同理对于75,当我们N/75的时候,正好把三个5全算进去……通过这个方法,更简化程序的实现。</span></p> <p></p><pre class="brush:php;toolbar:false">int Count1(int n) { int num = 0; while(n){ num += n/5; //这种方法更简洁,避免了附设变量 n = n/5; } return num; }
问题二:
说白了,问题2跟问题1是一样的,求N!2的倍数。
思路一:
根据上题的情况,写出如下表达式求表达式N! = 2^x * 3^y * 5^z... 我们要求x的值为多少。也可以通过遍历1-N一个一个求解
int Count2(int n) { int num = 0; for(int i =0;i > 1; } } return num; }
思路二:
类似问题一中的公式,我们也可以写出Z = [N/2] + [N/(2^2)] + [N/(2^3)] +....
int Count3(int n) { int num = 0; while(n){ n = n >> 1; num += n; //这句话写在下面,更好 } return num; }
思路三:
N!含有质因数2的个数,还等于N减去N的二进制表示中1的数目。——这是一种巧妙的方法,是根据Z = [N/2] + [N/(2^2)] + [N/(2^3)] +....运算得到的,因为除以2,相当于右移一位,对于11011我们有:
Z = 1101 + 110 + 11 + 1 = (1000 + 100 + 1) + (100 + 10) + (10 +1) + 1
= 1111 + 111 + 1 = (10000 - 1) + (1000 - 1) + (10 - 1) + (1 - 1) = 11011 - (N二进制中1的个数)
相关题目:
给定整数n,判断它是否为2的方幂。
(2的方幂为2^X。所以表示为二进制的时候,只有一位为1,那么利用判断二进制的个数的方法来判断: n>0 && ((n && (n-1)) == 0))

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to remove duplicate values from PHP array using regular expressions: Use regular expression /(.*)(.+)/i to match and replace duplicates. Iterate through the array elements and check for matches using preg_match. If it matches, skip the value; otherwise, add it to a new array with no duplicate values.

1. Programming can be used to develop various software and applications, including websites, mobile applications, games, and data analysis tools. Its application fields are very wide, covering almost all industries, including scientific research, health care, finance, education, entertainment, etc. 2. Learning programming can help us improve our problem-solving skills and logical thinking skills. During programming, we need to analyze and understand problems, find solutions, and translate them into code. This way of thinking can cultivate our analytical and abstract abilities and improve our ability to solve practical problems.

Build browser-based applications with Golang Golang combines with JavaScript to build dynamic front-end experiences. Install Golang: Visit https://golang.org/doc/install. Set up a Golang project: Create a file called main.go. Using GorillaWebToolkit: Add GorillaWebToolkit code to handle HTTP requests. Create HTML template: Create index.html in the templates subdirectory, which is the main template.

Python is an ideal programming introduction language for beginners through its ease of learning and powerful features. Its basics include: Variables: used to store data (numbers, strings, lists, etc.). Data type: Defines the type of data in the variable (integer, floating point, etc.). Operators: used for mathematical operations and comparisons. Control flow: Control the flow of code execution (conditional statements, loops).

C++ programming puzzles cover algorithm and data structure concepts such as Fibonacci sequence, factorial, Hamming distance, maximum and minimum values of arrays, etc. By solving these puzzles, you can consolidate C++ knowledge and improve algorithm understanding and programming skills.

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

C is an ideal choice for beginners to learn system programming. It contains the following components: header files, functions and main functions. A simple C program that can print "HelloWorld" needs a header file containing the standard input/output function declaration and uses the printf function in the main function to print. C programs can be compiled and run by using the GCC compiler. After you master the basics, you can move on to topics such as data types, functions, arrays, and file handling to become a proficient C programmer.

Through GoGet, you can quickly and easily obtain Go modules. The steps are as follows: Run in the terminal: goget[module-path], where module-path is the module path. GoGet automatically downloads the module and its dependencies. The location of the installation is specified by the GOPATH environment variable.
