zoukankan      html  css  js  c++  java
  • zufeoj 1018 阶乘第k位是多少(sprintf)

    题目描述

        n的阶乘定义为n!=1*2*3*……*n  如3!=6     n!通常最后会有很多0,如5!=120  最后有一个0,现在统计n!去除末尾的0后,最后k位是多少

    输入

        第一行包括两个数n,k

    输出

        如果n!不止k位,则输出最后k位,如果不足k位,则将剩下的全部输出

    样例输入

    7 2
    

    样例输出

    04
    

    提示

    7!为5040,去除末尾的0为504,最后两位为04

    100%满足1< =n< =20  1< =k< =9

     sprintf(str,"%lld",s);将整型s转化为了char型放到了str数组中
     1 #include <iostream>
     2 #include <string>
     3 #include <cstring>
     4 //#include <algorithm>
     5 using namespace std;
     6 long long n,k,s=1;
     7 char str[110];
     8 int main()
     9 {
    10     cin>>n>>k;
    11     int i;
    12     for(i=1;i<=n;i++)
    13     {
    14         s=s*i;
    15         while(s%10==0) s=s/10;
    16         s=s%10000000000;
    17    
    18     }
    19     sprintf(str,"%lld",s);
    20     int len=strlen(str)-1;
    21      i=len-k+1;
    22      if(i<0) i=0;
    23     for(;i<=len;i++)
    24     {
    25         cout<<str[i];
    26     }
    27     cout<<endl;
    28     return 0;
    29 }
  • 相关阅读:
    springmvc 处理静态资源
    springmvc jsp 获取 上下文
    springmvc 如何设置首页
    servlet-mapping url-pattern / 和 /*区别
    nginx支持php
    openresty 变量
    git tag用法
    python 导入模块,__init__.py 的作用
    python 转码问题
    python装饰器
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271214.html
Copyright © 2011-2022 走看看