zoukankan      html  css  js  c++  java
  • JustOj 2043: N!

    题目描述

    输出N的阶乘。(注意时间限制150ms&&注意不能打表后输出,赛后我们会检查代码,如有发现,该位同学总分记0分处理)

    打表的定义:在本地主机预先计算出了每个值对应的答案,并把输入和输出的映射直接写入所提交的代码。

    输入

    多组输入到文件结尾
    每组输入一个整数n,(0<=n<=23)。

    输出

    每组测试数据输出一行,为n!。

    样例输入
    1
    2
    样例输出
    1
    2

    题解:有两种办法,一个是用c++的高精度来写,第二种是用long long int去存乘积,但是不要全部都存,
    把计算过程中所有的因子10提取出来(PS:sum只存去除后缀0的数),就是说例如如果遇到了5,就把sum除以2,
    把标志末尾有多少个0的计数器++,最后吧sum输出,在循环输出全部的0
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <vector>
     6 #include <cstdlib>
     7 #include <iomanip>
     8 #include <cmath>
     9 #include <ctime>
    10 #include <map>
    11 #include <set>
    12 #include <queue>
    13 using namespace std;
    14 #define lowbit(x) (x&(-x))
    15 #define max(x,y) (x>y?x:y)
    16 #define min(x,y) (x<y?x:y)
    17 #define MAX 100000000000000000
    18 #define MOD 1000000007
    19 #define pi acos(-1.0)
    20 #define ei exp(1)
    21 #define PI 3.141592653589793238462
    22 #define INF 0x3f3f3f3f3f
    23 #define mem(a) (memset(a,0,sizeof(a)))
    24 typedef long long ll;
    25 ll gcd(ll a,ll b){
    26     return b?gcd(b,a%b):a;
    27 }
    28 const int N=1e6+10;
    29 const int mod=1e9+7;
    30 int main()
    31 {
    32     ll n;
    33     while(cin>>n){
    34         ll pre=1;
    35         string s="";
    36         for(int i=2;i<=n;i++){
    37             int x=i;
    38             while(x%5==0){
    39                 if(x%2==0)
    40                     x/=2;
    41                 else
    42                     pre/=2;
    43                 x/=5;
    44                 s+='0';
    45             }
    46             pre*=x;
    47         }
    48         cout<<pre<<s<<endl;
    49     }
    50     return 0;
    51 }
  • 相关阅读:
    语音识别系列之区分性训练和LF-MMI【转】
    node、npm安装与升级
    Vue3.0 新特性以及使用经验总结
    div垂直居中的方法
    前端性能优化
    大型网站设计总结
    前端SEO
    前端埋点总结
    jenkins自动构建、自动部署
    Python常见whl文件集合
  • 原文地址:https://www.cnblogs.com/wydxry/p/7273899.html
Copyright © 2011-2022 走看看