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 ...
随机推荐
- Python之日志操作(logging)
import logging 1.自定义日志级别,日志格式,输出位置 logging.basicConfig( level=logging.DEBUG, format='%(asctime)s | ...
- Join an instance to my AWS Directory Service domain
https://amazonaws-china.com/cn/premiumsupport/knowledge-center/ec2-systems-manager-dx-domain/ https: ...
- python最简单发送邮件
#!/usr/bin/env python #coding:utf8 #Author:lsp #Date:下午5:51:13 #Version:0.1 #Function: #导入smtplib和MI ...
- 数据结构基础---Binary Search Tree
/// Binary Search Tree - Implemenation in C++ /// Simple program to create a BST of integers and sea ...
- Python爬虫学习 - day1 - 爬取图片
利用Python完成简单的图片爬取 最近学习到了爬虫,瞬时觉得很高大上,想取什么就取什么,感觉要上天.这里分享一个简单的爬取汽车之家文章列表的图片教程,供大家学习. 需要的知识点储备 本次爬虫脚本依赖 ...
- POJ3468(线段树区间增加,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 81519 ...
- kvm的vmcall
这几个接口的区别在于参数个数的不用,本质是一样的.挑个参数最多的看下: static inline long kvm_hypercall4(unsigned int nr, unsigned long ...
- Oracle基础 07 参数文件 pfile/spfile
--查看数据库运行模式(spfile还是pfile)select decode(count(*),1,'spfile','pfile') from v$spparameterwhere rownum= ...
- SpringMVC与Spring的父子容器关系
问题: 在整合框架的时候有人也许会产生一个问题:能不能只配置一个扫描包加载实现类的扫描驱动,即在根目录下扫描所有的注解(@Controller.@Service.@Repository.@Compne ...
- [ 手记 ] Oracle 11g安装过程
安装环境: 操作系统:Centos6.4 Desktop 主机名:oracle 内存:2G 安装前准备: 修改主机名: [root@oracle ~]# vim /etc ...