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 }
原文地址:https://www.cnblogs.com/caiyishuai/p/8448257.html