【发布时间】:2016-04-27 21:08:07
【问题描述】:
假设有一个给定的数字,我们应该测试它是否是四个连续数字的乘积?
所以如果y 是我们给定的数字,我们应该测试y = x(x+1)(x+2)(x+3) 是否为任意x?
如何为这个问题设计算法?
我是这样做的:
import java.util.*;
public class Product
{
public static int product(int i)
{
return i * (i+1) * (i+2) * (i+3);
}
public static void main(String[] args)
{
Scanner scnr = new Scanner(System.in);
int x = scnr.nextInt();
for (int i = 0; i < x/2; i++)
{
if (product(i) == x)
{
System.out.println("number is product of 4 consecutive numbers");
break;
}
}
}
}
【问题讨论】:
-
解决
x? SO 系列网站中有没有 dividebyzero.com? -
@Paul R - 很明显只允许整数解;除非您在谈论整数,否则“连续”一词并不完全适用。
-
@bta:这将很快在 mathoverflow.net 上关闭——对于专业数学家的听众来说太初级了。
-
初级,亲爱的 Watson,用于 mathoverflow.net,而不是 stackoverflow.com :)
标签: algorithm