<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div>
<input type="text" id="ip" />
<button id="btn">判断是不是水仙花</button>
<input type="text" id="ip1" />
</div>
<script src="1.js"></script>
<script>
$("btn").onclick = function () {
$("ip").value = suijishu(100, 999);//产生随机数的赋值操作给表格输入框里面去
$("ip1").value = sxh(suijishu(100, 999));//调用的封装函数里面判断是不是水仙花数的判断工具对产生的随机数字进行判断
};
document.write(sxh(153));
// $("btn").onclick = function () {
// $("ip").value = suijishu(100, 999);
// $("ip1").value = sx(suijishu(100, 999));
// };
// document.wirte(sx(153));
</script>
</body>
</html>
function sxh(num) {
var a = num;
var b = parseInt(num / 100);
var c = parseInt(num % 100 / 10);
var d = parseInt(num % 10);
var sum = b * b * b + c * c * c + d * d * d;
var flag = false;
while (sum == a) {
if (a % sum == 0) {
flag = true;
break;
}
a++;
}
return flag ? ‘是‘ : ‘不是‘;
}
sxh()
function sx() {
for (var num = 100; num < 1000; num++) {
var b = parseInt(num / 100);
var c = parseInt((num % 100) / 10);
var d = parseInt(num % 10);
var sum = b * b * b + c * c * c + d * d * d;///这里还可以用到数学函数var sum=math.pow(b,3)+math.pow(c,3)+math(d,3)条件关系是进行表达。
if (num == sum) {
break;
}
}
return num == sum ? ‘不是‘ : ‘是‘;
}
sx()