zoukankan      html  css  js  c++  java
  • HDU-1042.N!(大数与小数相乘的乘法模拟)

      本题大意:给定一个10000以内的整数n,让你求出n!并输出。

      本题思路:先初始化一个存放答案的数组ans,初始ans[0] = 1,并初始化其剩下的元素为0,接着就从2开始依次与ans数组内的每一个数相乘,具体乘法过程见代码,需要注意的就是求divisor时自身此时的值也是需要加上的,还有就是注意当存在余数并且当前位数已经不够用的情况下才将总位数加一否则不加,答案里会出现很多前导零。

      

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstring>
     4 using namespace std;
     5 
     6 const int maxn = 2e5 + 5;
     7 int n, tot, now, divisor, t;
     8 int ans[maxn];
     9 
    10 int main () {
    11     while(~scanf("%d", &n)) {
    12         memset(ans, 0, sizeof(ans));
    13         ans[0] = 1, tot = 1;
    14         for(int i = 2; i <= n; i ++) {
    15             divisor = 0, now = 0;
    16             while(now < tot) {
    17                 t = ans[now] * i + divisor;
    18                 ans[now ++] = t % 10;
    19                 divisor = t / 10;
    20                 if(divisor && now == tot) tot ++;
    21             }
    22         }
    23         for(int i = tot - 1; i >= 0; i --)
    24             printf("%d", ans[i]);
    25         printf("
    ");
    26     }
    27     return 0;
    28 }
    View Code
  • 相关阅读:
    python之Selenium
    Python常用集锦(upgrading...)
    豆瓣爬虫
    poj 2299 Ultra-QuickSort(求逆序对)
    nyoj117 求逆序数
    codeforces 644A Parliament of Berland
    codeforces 659A Round House
    poj 3264 Balanced Lineup(RMQ裸题)
    nyoj 119 士兵杀敌(三)(RMQ)
    hdu 5655 CA Loves Stick
  • 原文地址:https://www.cnblogs.com/bianjunting/p/10502734.html
Copyright © 2011-2022 走看看