📜  以右对齐格式打印范围的阶乘

📅  最后修改于: 2021-04-27 21:59:46             🧑  作者: Mango

给定两个数字m和n,任务是查找包括m和n在内的所有数字的阶乘,然后按以下格式打印。

例子:

Input : 6 10
Output :
     720
    5040
   40320
  362880
 3628800

Input : 10 20
Output :
             3628800
            39916800
           479001600
          6227020800
         87178291200
       1307674368000
      20922789888000
     355687428096000
    6402373705728000
  121645100408832000
 2432902008176640000

我们使用boost多精度库来存储大量的阶乘,并使用setw()函数打印阶乘。
setw(int)-> setw(int)是用于结果意图的函数。

C++
// CPP Program to print format of factorial
#include 
#include 
#include 
using namespace std;
using boost::multiprecision::cpp_int;
  
vector find_factorial(int num1, int num2)
{
    // vector for store the result
    vector vec;
  
    // variable for store the
    // each number factorial
    cpp_int fac = 1;
  
    // copy of first number
    int temp = num1;
  
    // found first number factorial
    while (1) {
        if (temp == 1)
            break;
        fac *= temp;
        temp--;
    }
      
    // push the first number in result vector
    vec.push_back(fac);
  
    // incerement the first number
    num1++;
  
    // found the all reaming number 
    // factorial loop is working until
    // all required number factorial founded
    while (num1 <= num2) {
        fac *= num1;
  
        // store the result of factorial
        vec.push_back(fac);
  
        // incerement the first number
        num1++;
    }
      
    // return the result
    return (vec);
}
  
// function for print the result
void print_format(vector& result)
{
    // setw() is used for fill the blank
    // right is used for right justification of data
    int digits = result.back().str().size();
      
    for (int i = 0; i < result.size(); i++) {
         cout <<  setw(digits + 1) <<  
                    right << result[i] <<  endl;
    }
}
  
// Driver function
int main()
{
      
    // number which found the factorial
    // of between range
    int m = 10, n = 20;
  
    // store the result of factorial
    vector result_fac;
  
    // function for found factorial
    result_fac = find_factorial(m, n);
  
    // function for print format
    print_format(result_fac);
  
    return 0;
}


PHP


输出 :

3628800
            39916800
           479001600
          6227020800
         87178291200
       1307674368000
      20922789888000
     355687428096000
    6402373705728000
  121645100408832000
 2432902008176640000