求100的阶乘

int _tmain(int argc, _TCHAR* argv[])
{
	vector <int> vec;
	vec.push_back(1);
	for(int i = 1; i <= 100; i++)
	{
		int len = vec.size();
		for(int j = 0; j < len; j++)
		{
			//cout << "len = " << len << endl;//结果应该是158位的不知道为啥这里只有len = 156
			vec[j] *= i;
		}
		for(int j = 0; j < len - 1; j++)
		{
			if(vec[j] >= 10)
			{
				vec[j + 1] += vec[j] / 10;
				vec[j] %= 10;
			}
		}
		if(vec[len - 1] >= 10)
		{
			int carry = vec[len - 1] / 10;
			vec[len - 1] %= 10;
			do
			{
				vec.push_back(carry % 10);
				carry /= 10;
			}
			while(carry > 0);
		}
	}

	int len = vec.size();
	for(int i = len - 1; i >= 0; i--)
	{
		cout << vec[i];
	}
	cout << endl;
	return 0;
}


结果:9332621544394415268169923885626670049071596826438162146859296389521759999322991

    5608941463976156518286253697920827223758251185210916864000000000000000000000000

158位数

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页