考题:求1到n之间能被5整除的所有数的乘积可以用递归

rategy 2007-05-17 03:18:53
从键盘输入一个数n,求1到n之间能被5整除的所有数的乘积可以用递归。

第一个答对的就给分。
...全文
1058 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
brooksychen 2007-05-18
  • 打赏
  • 举报
回复
两个建议:
1、不要用递归,能被5整除的只能是个位数为0或5的数,在循环里算就可以
2、用BigInteger类,因为像这种类似于阶乘的连乘,很容易越界
sss_free 2007-05-18
  • 打赏
  • 举报
回复
两个建议:
1、不要用递归,能被5整除的只能是个位数为0或5的数,在循环里算就可以
楼主要求的
2、用BigInteger类,因为像这种类似于阶乘的连乘,很容易越界
好建议!
======================
rategy 2007-05-18
  • 打赏
  • 举报
回复
感谢各位。
小作业大道理,结贴。
yuyunliuhen 2007-05-17
  • 打赏
  • 举报
回复
OU 来看看
sharpfire 2007-05-17
  • 打赏
  • 举报
回复
int val = 1;
for(int i=1;i<=n;i++){
if(i%5 == 0){
val = val * i;
}
}
System.out.print(val);
esidemayi 2007-05-17
  • 打赏
  • 举报
回复
long n=30;
long i=1;
long count=0;
long val=1;
while(count<=n&&(count=i*5)<=n){
val*=count;
i++;
}
System.out.println(val);
wuhailiang1983 2007-05-17
  • 打赏
  • 举报
回复
import java.util.*;
public class Plus
{
public static void main(String[] args)
{
System.out.print("请输入一个数N:");
long n=0;
while(true)
{
try
{
Scanner is1 = new Scanner(System.in);
n = is1.nextLong();
}
catch(Exception e)
{
System.out.print("输入错误");
}

if(n<5)
System.out.print("输入太小");
else
System.out.print(cal(n));
}
}

private static long cal(long n){
if(n<1)
return 1;
else if(n%5==0)
return cal(n-1)* n;
else
return cal(n-1);
}



}
knight_qmh 2007-05-17
  • 打赏
  • 举报
回复
int f(int n)
{
return (n<5)?0:fn(n,5);
}
int fn(int n,int i)
{
if(n<i) return 1;
if(n==i) return i;
return i*fn(n,i+5);
}
wangkm 2007-05-17
  • 打赏
  • 举报
回复
答案都发过了,顶一下
sss_free 2007-05-17
  • 打赏
  • 举报
回复
n=5 5
n=6 5
n=7 5
n=8 5
n=9 5
n=10 50
n=11 50
n=12 50
n=13 50
n=14 50
n=15 750
n=16 750
n=17 750
n=18 750
n=19 750
n=20 15000
n=21 15000
n=22 15000
n=23 15000
n=24 15000
n=25 375000
n=26 375000
n=27 375000
n=28 375000
n=29 375000
n=30 11250000
n=31 11250000
n=32 11250000
n=33 11250000
n=34 11250000
n=35 393750000
n=36 393750000
n=37 393750000
n=38 393750000
n=39 393750000
对不起只能算到39
sss_free 2007-05-17
  • 打赏
  • 举报
回复
int f(int n)
{
if(n==0) return 1;
if(n%5==0) return n*f(n-1);
return 1*f(n-1);
}
likgui 2007-05-17
  • 打赏
  • 举报
回复
关注
CrazyGou 2007-05-17
  • 打赏
  • 举报
回复
作业贴
li_d_s 2007-05-17
  • 打赏
  • 举报
回复
这种题目也值100分,楼主分数太多了吧?
清风徐徐 2007-05-17
  • 打赏
  • 举报
回复
package jdsTutorial;

import java.util.Scanner;

public class T03 {


public static void main(String[] args) {

System.out.print("请输入一个数N:");
long n = 0;

Scanner is1 = new Scanner(System.in);
try{
n = is1.nextLong();
}
catch(java.util.InputMismatchException e)
{
System.out.print("输入错误");
return;
}

if(n<5)
System.out.print("输入太小");
else
System.out.print(calc(n));
}

private static long calc(long n){
if(n<1)
return 1;
else if(n%5==0)
return calc(n-1)* n;
else
return calc(n-1);
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧