zoukankan      html  css  js  c++  java
  • 大数阶乘(c++实现)

    #include <iostream>
    using namespace std;
    #define N 1000
    int BigNumFactorial(int Num[], int n);
    void Print(int Num[], int Index, int n);
    int main(void)
    {
        int n;
        int Num [N] = {1, 0};          //将第一位的数字初始化为1, 其余的都初始化为0
        cout << "你需要计算哪个数字的阶乘?" << endl;
        cin >> n;
        int Index = BigNumFactorial(Num, n);
        Print(Num, Index, n);
        return 0;
    }
    int BigNumFactorial(int Num[], int n)      //大数阶乘
    {
        int i, j, sum = 1, temp;
        int Index = 0;              //用于表示数组中的有效位置数
        for(i = 2; i <= n; i++)
        {
            temp = 0;
            for(j = 0; j <= Index; j++)
            {
                sum = i * Num[j] + temp;
                if(sum / 10 != 0)       //说明sum不止一位数
                {
                    Num[j] = sum % 10;
                    temp = sum / 10;        //所谓的‘进位’
                }
                else
                {
                    Num[j] = sum;
                    temp = 0;
                }
            }
            if(temp != 0)       //如果‘进位’还没有为0
            {
                Index = j - 1;
                while(temp)
                {
                    Index++;
                    Num[Index] = temp % 10;
                    temp /= 10;
                }
            }
        }
        return Index;
    }
    void Print(int Num[], int Index, int n)
    {
        int i;
        cout << n << "阶的结果为: ";
        for(i = Index; i >= 0; i--)
        {
            cout << Num[i];
        }
        cout << endl;
    }

  • 相关阅读:
    兼容Android 和 ios JavaScript copy paste
    hello-循环神经网络(RNN)原理
    236. Lowest Common Ancestor of a Binary Tree
    Tensorflow 之模型内容可视化
    CUDA,cudnn一些常见版本问题
    MongoDB国内学术研究(部分)
    Unsupported major.minor version 52.0
    WebService的两种方式SOAP和REST比较
    廖雪峰老师的git在线教程
    MAVEN ERROR : Dynamic Web Module 3.0 requires Java 1.6 or newer
  • 原文地址:https://www.cnblogs.com/ReturnOfTheKing/p/11228042.html
Copyright © 2011-2022 走看看