13test02:阶乘
//假设32位int型变量y是表示最大人数的x的阶乘,即y=x!,当x最大值取什么时,y取最大值
//,且乘法不溢出。
#include<iostream>
using namespace std;
unsigned int Fun(unsigned int);
int main()
{
unsigned int max_person_num=0;
max_person_num=~max_person_num;
cout<<max_person_num<<endl;
for(unsigned int i=2;Fun(i)<=max_person_num&&Fun(i)>Fun(i-1);i++)
//C++中数据溢出时类似汽车里程表,达到最大值是立即归零.
//即如果int型最大值为10,int i=10;则i+=1之后,i=0而非11.所以在for循环的测试条件中
//加入了Fun(i)>Fun(i-1)用来测试返回值是否有溢出。因为整数的阶乘是不断增大的,所以
//一旦溢出则其阶乘比小于前一个数的阶乘.
cout<<"i="<<i<<" y="<<Fun(i)<<endl;
cout<<"x最大为:"<<i-1<<" y最大为:"<<Fun(i-1)<<endl;
return 1;
}
unsigned int Fun(unsigned int x)//x阶乘函数.
{
if(1==x) return 1;
else return x*Fun(x-1);
}
13test02:阶乘的更多相关文章
- C语言 · 阶乘计算 · 基础练习
问题描述 输入一个正整数n,输出n!的值. 其中n!=1*2*3*-*n. 算法描述 n!可能很大,而计算机能表示的整数范围有限,需要使用高精度计算的方法.使用一个数组A来表示一个大整数a,A[0]表 ...
- Java 计算N阶乘末尾0的个数-LeetCode 172 Factorial Trailing Zeroes
题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...
- 关于for循环的几个小练习,例如奇数偶数,阶乘,求和等
1 .100以内的奇数和偶数 var js = ""; var os = ""; for(var i=1;i<101;i++) { if(i%2 == 0 ...
- [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- 求单链表L各结点的阶乘之和(c语言)
链表需要用到指针 阶乘需要用到递归 链表中的注意事项: 1.链表L是否等于NULL ----------是循环结束的条件 2.链表L->Data ---------取链表L中各个结点的值 3.L ...
- 洛谷 P2726 阶乘 Factorials Label:Water
题目背景 N的阶乘写作N!,表示小于等于N的所有正整数的乘积. 题目描述 阶乘会变大得很快,如13!就必须用32位整数类型来存储,到了70!即使用浮点数也存不下了. 你的任务是找到阶乘最前面的非零位. ...
- js实现阶乘
//while循环实现function calNum(n) { var product = 1; while(n > 1){//1*5*4*3*2,1*n*(n-1)*(n-2)*...*2 p ...
- 洛谷P1134 阶乘问题[数论]
题目描述 也许你早就知道阶乘的含义,N阶乘是由1到N相乘而产生,如: 12! = 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x 10 x 11 x 12 = 479,001, ...
- Random随机类(11选5彩票)BigInteger大数据类(华为面试题1000的阶乘)
先上Java Web图 为了简化叙述,只写Java代码,然后控制台输出 使用[Random类]取得随机数 import java.util.Random; public class Fir { pub ...
随机推荐
- hadoop之HDFS与MapReduce
Hadoop历史 雏形开始于2002年的Apache的Nutch,Nutch是一个开源Java 实现的搜索引擎.它提供了我们运行自己的搜索引擎所需的全部工具.包括全文搜索和Web爬虫. 随后在2003 ...
- linux内存条排查
已发现2个内存错误,应用名称(kernel:),日志内容(hangzhou-jishuan-DDS0248 kernel: sbridge: HANDLING MCE MEMORY ERROR han ...
- 【数据结构】bzoj3747Kinoman
Description 共有m部电影,编号为1~m,第i部电影的好看值为w[i]. 在n天之中(从1~n编号)每天会放映一部电影,第i天放映的是第f[i]部. 你可以选择l,r(1<=l< ...
- [Leetcode Week6]Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/best-time-to-buy-and ...
- [Leetcode Week5]Word Ladder
Word Ladder题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder/description/ Description Give ...
- System and method for parallel execution of memory transactions using multiple memory models, including SSO, TSO, PSO and RMO
A data processor supports the use of multiple memory models by computer programs. At a device extern ...
- 千万不要使用xfce和KDE版Manjaro Linux--之荒谬言论
Manjaro Linux 使用经验: ①千万不要使用xfce版,虽然性能上廉价,但是吃亏,调声音80%几率卡死调不了,托盘图标很容易不响应!关机的Beep声,分分钟吓死人!按照网上各种方法弄,下次开 ...
- Tarjan 联通图 Kuangbin 带你飞 联通图题目及部分联通图题目
Tarjan算法就不说了 想学看这 https://www.byvoid.com/blog/scc-tarjan/ https://www.byvoid.com/blog/biconnect/ 下面是 ...
- 【转】java中的集合和数组
转载自:http://www.cnblogs.com/summers/p/4094260.html 数组Array和集合的区别: (1)数组是大小固定的,并且同一个数组只能存放类型一样的数据(基本类型 ...
- selenium+python自动化82-只截某个元素的图【转载】
前言 selenium截取全图小伙伴们都知道,曾经去面试的时候,面试官问:如何截图某个元素的图?不要全部的,只要某个元素...小编一下子傻眼了,苦心人,天不负,终于找到解决办法了. selenium截 ...