微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c ++-数字100的阶乘

如何解决c ++-数字100的阶乘

我正在尝试计算和显示数字 100 的阶乘,但此代码为更高的数字提供 0。

int fact(int n){
int res=1;
for(int i=2; i<=n; i++){
    res=res*i;
}
return res;

}

解决方法

100 的阶乘是 9.332622e+157 可以存储在数组中并显示数组元素。 你可以通过这个链接 - https://www.geeksforgeeks.org/factorial-large-number/

#define MAX 200
void factorial(int n){
    int res[MAX];

    res[0] = 1;
    int res_size = 1;

    // Apply simple factorial formula n! = 1 * 2 * 3 * 4...*n
    for (int x=2; x<=n; x++)
        res_size = multiply(x,res,res_size);

    cout << "Factorial of given number is \n";
    for (int i=res_size-1; i>=0; i--)
        cout << res[i];
    }
}

// This function multiplies x with the number
// represented by res[].
// res_size is size of res[] or number of digits in the
// number represented by res[]. This function uses simple
// school mathematics for multiplication.
// This function may value of res_size and returns the
// new value of res_size
int multiply(int x,int res[],int res_size)
{
    int carry = 0;  // Initialize carry

    // One by one multiply n with individual digits of res[]
    for (int i=0; i<res_size; i++)
    {
        int prod = res[i] * x + carry;

        // Store last digit of 'prod' in res[] 
        res[i] = prod % 10; 

        // Put rest in carry
        carry  = prod/10;   
    }

    // Put carry in res and increase result size
    while (carry)
    {
        res[res_size] = carry%10;
        carry = carry/10;
        res_size++;
    }
    return res_size;
}
,

100!太大而无法存储在 Int 中。

在 C++ 中,library(shiny) library(shinydashboard) options(shiny.maxRequestSize = 30*1024^2) if (interactive()) { shinyServer(function(input,output){ table_content_GetObjectsSWMM<-eventReactive(input$GetObjectsSWMMbtn,{ file_to_read_SWMM_Out=input$file9 read_Output<- readBin(input$file9$datapath,n = 1,size = 4,what = "rb") read_Output2<-paste('"',read_Output,'"',sep="") BinaryFile<- readBin( read_Output2,what="rb") Status ={} seek(BinaryFile,1*4,"start") return(Status) }) output$GetObjectsSWMM_P <-renderText({ req(table_content_GetObjectsSWMM()) table_content_GetObjectsSWMM() Status }) })} 的大小是机器架构上一个字的大小。

在 32 位架构中,字长为 32 位。

使用有符号的 32 位 int 可以表示的最大数字是 2,147,483,647,比 100 小得多!

即使您使用了无符号 64 位整数 (uint64_t),100!会太大而不适合。

,

正如评论中提到的,值 100!太大而无法放入本机整数类型。

一种解决方案是使用诸如 boost::multiprecision 之类的库。

这是一个示例程序,使用您的代码,但使用了 int 代替 boost::multiprecision::cpp_int

#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>

using Int = boost::multiprecision::cpp_int;
 
Int fact(int n)
{
  Int res = 1;
  for(int i=2;  i<= n; i++)
    res = res * i;
  return res; 
}

int main() 
{
    std::cout << fact(100);
}

输出:

93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

这是用法的 live example,包括其他阶乘值。

,

最大整数是 2147483647,我认为这个数字是溢出的。 我试过了,这个函数在para是17以下运行良好,之后,结果超过2147483647。 你可以google一下电脑是如何存储正数、负数等等的,一定更清楚。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。