C++如何计算一个闭区间内所有整数的阶乘

img

img


这里没明白题意。


#include<bits/stdc++.h>

using namespace std;
int jc(int n){
    if(n==1)
    return 1;
    return n*jc(n-1);
}
int main() {
    int i,a,b,s=0;
    cin>>a>>b;
    for( i=a; i<=b; i++) {
        s+=jc(i);
    }
    cout<<s;
    return 0;
}
 

img

主要是这个a,b允许多大值,如果很大,阶乘数将非常大。
组合数比较简单,数量为b*(b-1)*(b-2)*...*(a+1)

组合数是啥意思


#include <stdio.h>
int  main()
{
int a,b;
int c=0,d=0,e=0,i;
 
 printf("Please input the min number:");
 scanf("%d",&a);
 printf("Please input the max number:");
 scanf("%d",&b);
  
 for(i=a;i<=b;i++)
 {
   d=i*i;
   e =e + d;
 }
 printf("The sum of the factorial of the numbers is:%d\n",e);
}