51Nod 1003 1004 1009

1003 阶乘后面0的数量

基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题

n的阶乘后面有多少个0?

6的阶乘 = 1*2*3*4*5*6 = 720,720后面有1个0。

Input

一个数N(1 <= N <= 10^9)

Output

输出0的数量

Input

5

Output示例

1

比较有意思的一个题

 1 #include<cstdio>
 2 using namespace std;
 3 int n,ans(0);
 4 int main()
 5 {
 6     scanf("%d",&n);
 7     while(n){
 8         ans+=n/5;
 9         n/=5;
10     }
11     printf("%d
",ans);
12     return 0;
13 }

1004 n^n的末位数字

题目来源: Author Ignatius.L (Hdu 1061)

基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题

给出一个整数N,输出N^N(N的N次方)的十进制表示的末位数字。

Input

一个数N(1 <= N <= 10^9)

Output

输出N^N的末位数字

Input示例

13

Output示例

3

 快速幂取模

 1 #include<iostream>
 2 using namespace std;
 3 int n;
 4 long long FastPower(int a,int b,int mo){
 5     long long ans=1;a%=mo;
 6     while(b){
 7         if(b & 1)ans=(ans*a)%mo;
 8         b>>=1;
 9         a=(a*a)%mo;
10     }
11     return ans;
12 }
13 int main()
14 {
15     cin>>n;
16     cout<<FastPower(n,n,10);
17     return 0;
18  } 

1009 数字1的数量

基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题

给定一个十进制正整数N,写下从1开始,到N的所有正数,计算出其中出现所有1的个数。

例如:n = 12,包含了5个1。1,10,12共包含3个1,11包含2个1,总共5个1。

Input

输入N(1 <= N <= 10^9)

Output

输出包含1的个数

Input示例

12

Output示例

5

 1 #include<iostream>  
 2 #include<cstdio>  
 3 #include<map>  
 4 #include<cstring>  
 5 #include<string>  
 6 #include<algorithm>  
 7 #include<queue>  
 8 #include<vector>  
 9 #include<stack>  
10 #include<cstdlib>  
11 #include<cctype>  
12 #include<cmath>  
13 #define LL long long  
14 using namespace std;  
15 int CountOne(int n){  
16     int cnt=0;  
17     int i=1;  
18     int current=0,after=0,before=0;  
19     while((n/i)!=0){  
20         current=(n/i)%10;  
21         before=n/(i*10);  
22         after=n-(n/i)*i;  
23         if(current>1)  
24             cnt=cnt+(before+1)*i;  
25         else if(current==0)  
26             cnt=cnt+before*i;  
27         else if(current==1)  
28             cnt=cnt+before*i+after+1;  
29             i=i*10;  
30     }  
31     return cnt;  
32 }  
33 int main()  
34 {  
35     int n;  
36     while(cin>>n){  
37         int res=CountOne(n);  
38         cout<<res<<endl;  
39     }  
40     return 0;  
41 }  
原文地址:https://www.cnblogs.com/suishiguang/p/6382752.html