Skip to content

Instantly share code, notes, and snippets.

@lxfly2000
Created July 22, 2018 10:18
Show Gist options
  • Save lxfly2000/bcb0be6418636383c75875a5b9565fcb to your computer and use it in GitHub Desktop.
Save lxfly2000/bcb0be6418636383c75875a5b9565fcb to your computer and use it in GitHub Desktop.
大数阶乘
#include<Windows.h>
#include<iostream>
#include<string>
unsigned short c[22167];
void testmain(std::string&param)
{
bool enable_cout = true;
if (param[0] == '@')
{
enable_cout = false;
param.erase(param.begin());
}
for (char&ch : param)
{
if (!isdigit(ch))
{
puts("Not a num. (NaN)");
return;
}
}
if (atoi(param.c_str()) > 6553)
{
puts("Unacceptable num.");
return;
}
memset(&c, 0, sizeof c);
c[0] = 1;
int maxi = 1;
for (int top = atoi(param.c_str()); top > 1; top--)
{
for (int i = 0; i < maxi; i++)
c[i] *= top;
for (int i = 0; i < maxi; i++)
{
c[i + 1] += c[i] / 10;
c[i] %= 10;
if (c[i + 1] && i + 2 > maxi)
maxi = i + 2;
}
}
for (int i = maxi - 1; i != -1; i--)
{
if (c[i] > 9)
{
puts("Found num. greater than 9.");
return;
}
if (enable_cout)
std::cout << c[i];
}
std::cout << std::endl << maxi << " digits.\n";
}
int main()
{
DWORD t_start, t_end;
std::string param;
std::getline(std::cin, param);
t_start = GetTickCount();
testmain(param);
t_end = GetTickCount();
printf("%dms\n", t_end - t_start);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment