关于大数乘法的程序

Posted on 2007-04-10 15:26 UniK 阅读(287) 评论(0)  编辑 收藏 引用 所属分类: IT技术

#include <iostream>
using namespace std;

const int iSize = 10000;

int main() {
    int iResult[iSize];
    int iCount = 1;
    int iCarry = 0;
    int iTemp = 0;
    int n;

    iResult[0] = 1;
    cout << "Input a number:";
    cin >> n;
    for (int i = 2; i <= n; i++) {
        for (int j = 0; j < iCount; j++) {
            iTemp = iResult[j] * i + iCarry;
            iResult[j] = iTemp % 10;
            iCarry = iTemp / 10;
        }
        while (iCarry > 0) {
            iResult[iCount] = iCarry % 10;
            iCount++;
            iCarry = iCarry / 10;
        }
    }
    for (int i = iCount - 1; i >= 0; i--) {
        cout << iResult[i];
    }
    cout << endl;
    return 0;
}

只有注册用户登录后才能发表评论。