51Nod 1003 1004 1009

时间:2023-03-10 06:36:32
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

比较有意思的一个题

 #include<cstdio>
using namespace std;
int n,ans();
int main()
{
scanf("%d",&n);
while(n){
ans+=n/;
n/=;
}
printf("%d\n",ans);
return ;
}

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

快速幂取模

 #include<iostream>
using namespace std;
int n;
long long FastPower(int a,int b,int mo){
long long ans=;a%=mo;
while(b){
if(b & )ans=(ans*a)%mo;
b>>=;
a=(a*a)%mo;
}
return ans;
}
int main()
{
cin>>n;
cout<<FastPower(n,n,);
return ;
}

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

 #include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<cstdlib>
#include<cctype>
#include<cmath>
#define LL long long
using namespace std;
int CountOne(int n){
int cnt=;
int i=;
int current=,after=,before=;
while((n/i)!=){
current=(n/i)%;
before=n/(i*);
after=n-(n/i)*i;
if(current>)
cnt=cnt+(before+)*i;
else if(current==)
cnt=cnt+before*i;
else if(current==)
cnt=cnt+before*i+after+;
i=i*;
}
return cnt;
}
int main()
{
int n;
while(cin>>n){
int res=CountOne(n);
cout<<res<<endl;
}
return ;
}