美文网首页程序员
FCTRL2 - Small factorials

FCTRL2 - Small factorials

作者: waaagh | 来源:发表于2020-04-30 09:27 被阅读0次

其实spoj的题还是挺有意思的。
题目大意:很简单,求n!,你最大100
思路:100!得有一百多位,LL也不行,只能用字符串模拟乘法。
实现:注意进位可能有多次
代码:

//FCTRL2 - Small factorials
#include<iostream>
using namespace std;
#define MAXN    1000
#define LL  long long

int n, t;

int main() {
    scanf("%d", &t);
    while(t--) {
       scanf("%d", &n);
       if(n==0) {
           cout << 1 << endl;
       }else {
            char num[MAXN];
            int len = 1;
            num[0] = '1'; num[len] = 0;
            for(int i=2; i<=n; i++) {
                int carry = 0;
                int j = 0;
                while(j<len) {
                    int mul = (num[j]-'0')*i + carry;
                    num[j] = mul%10 + '0';
                    carry = mul/10;
                    j++;
                }
//              printf("carry = %d\n", carry);
                while(carry){
                    num[len++] = carry%10+'0';
                    num[len] = 0;
                    carry /=10;
                }
//              printf("i=%d, num=%s\n", i,num);
            }

            for(int i=len-1; i>=0; i--) {
                putchar(num[i]);
            }
            putchar('\n');
       }
    }
    return 0;
}

相关文章

  • FCTRL2 - Small factorials

    其实spoj的题还是挺有意思的。题目大意:很简单,求n!,你最大100思路:100!得有一百多位,LL也不行,只能...

  • small small bird

    有时候我觉得自己 像一只小小鸟 想要飞却怎么样也飞不高 也许有一天我栖上了枝头 却成为猎人的目标 我飞上了青天才发...

  • 英语口语每日精进

    small size 小号,小码;小尺寸 small town 小城镇,集镇 small amount 小额;小批...

  • CSS文字样式

    文字大小: |xx-small|x-small|small|medium|large|x-large|xx-lag...

  • Think Small, Do Small

    For all the time, I've been thinking about what is the be...

  • day8 CSS属性下

    1.文字排版 字号大小:font-size: 可选参数值:xx-small | x-small | small |...

  • Big fish

    Kept in a small bowl, the goldfish will remain small. Wit...

  • A Small Dining Table

    A Small Dining Table By Kuan Sun I bought a small dining ...

  • small

    每听到你的歌声我都会驻足倾听,但那往往都不是你。

  • small

    第一次遇见你,在大学的第一天,没有任何印象。 外貌协会的我在人群里完全注意不到你。 你留给我的第一印象大概是痘痘很...

网友评论

    本文标题:FCTRL2 - Small factorials

    本文链接:https://www.haomeiwen.com/subject/nwwawhtx.html