poj 1604 Just the Facts

来源:互联网 发布:matlab 矩阵符号运算 编辑:程序博客网 时间:2024/04/29 19:01
//通过打表就OK,求阶乘的末尾不为0的第一位数字! #include <iostream>#include <cstdio>#include <memory.h>using namespace std;int ans[10010];void solve(){     int i, mult;     memset(ans, 0, sizeof(ans));     ans[1] = 1;     mult = 1;     for (i = 2; i <= 10005; i++){         mult *= i;         //将阶乘得出的结果去掉末尾的0          while (mult % 10 == 0){               mult /= 10;         }         mult %= 100000;//对结果取模,是为了在求阶乘的时候产生溢出!          ans[i] = mult % 10;//得出结果      }}int main(){    int num;    solve();    while (cin >> num){          printf("%5d -> %d\n", num, ans[num]);    }        system("pause");}

原创粉丝点击