6
#yyds干货盘点# 名企真题专题:末尾0的个数
source link: https://blog.51cto.com/u_15488507/5974496
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
#yyds干货盘点# 名企真题专题:末尾0的个数
精选 原创1.简述:
描述输入一个正整数n,求n!(即阶乘)末尾有多少个0? 比如: n = 10; n! = 3628800,所以答案为2
输入描述:输入为一行,n(1 ≤ n ≤ 1000)
输出描述:输出一个整数,即题目所求
示例12.代码实现:
public class Main{
public static int test(int n){
if(n < 0){
return 0;
}
int res = 0;
while (n !=0){
res += n /5;
n = n/5;
}
return res;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
System.out.println( test(in.nextInt()));
}
}
}
public static int test(int n){
if(n < 0){
return 0;
}
int res = 0;
while (n !=0){
res += n /5;
n = n/5;
}
return res;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
System.out.println( test(in.nextInt()));
}
}
}
- 赞
- 收藏
- 评论
- 分享
- 举报
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK