6

#yyds干货盘点# 名企真题专题:末尾0的个数

 1 year ago
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.
neoserver,ios ssh client

#yyds干货盘点# 名企真题专题:末尾0的个数

精选 原创

97的风 2022-12-28 14:26:03 博主文章分类:面试题 ©著作权

文章标签 System 代码实现 文章分类 Java 编程语言 阅读数245

1.简述:

描述

输入一个正整数n,求n!(即阶乘)末尾有多少个0? 比如: n = 10; n! = 3628800,所以答案为2

输入描述:

输入为一行,n(1 ≤ n ≤ 1000)

输出描述:

输出一个整数,即题目所求

示例1

2.代码实现:

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()));
}
}
}
  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK