基础 - 算法

条件

window.onload = function () {
var kWeWeZhang = prompt("请输入数值");
if(kWeWeZhang%5 && kWeWeZhang%6) {
window.console.log("不能被5 也 不能被6");
}else if(kWeWeZhang%6) {
window.console.log("能被5 不能被6");
}else if(kWeWeZhang%5) {
window.console.log("不能被5 能被6");
}else{
window.console.log("能被5 也 能被6");
}
}

循环

window.onload = function () {
for(var i = 0;i<100;i++) {
/*控制台打印输出*/
window.console.log(i);
}
}

求因子
window.onload = function () {
var kWeWeZhang = prompt("请输入数值");
for(var i=1;i<=kWeWeZhang;i++) {
if(!(kWeWeZhang%i)) {
window.console.log(i);
}
}
}
求质数
window.onload = function () {
/*因子的个数*/
function kYueShuCount(kNumber) {
if (kNumber<1) return;
var kCount = 0;
for(var i=1;i<=kNumber;i++) {
if(!(kNumber%i))kCount++;
}
return kCount;
}
/*1到某数之间的所有质数*/
function kChaXunZhiShu(kNumber) {
if (kNumber<1) return;
window.console.log(1);
for(var i=1;i<=kNumber;i++) {
if (kYueShuCount(i)==2) window.console.log(i);
}
}

kChaXunZhiShu(100);
}


去掉7的倍数或7结尾的数
window.onload = function () {
var kWeWeZhang = prompt("请输入数值");
for(var i=1;i<=kWeWeZhang;i++) {
if(i%7 && i%10%7) {
window.console.log(i);
}
}
}
水仙花数
每一位上的数字的立方和等于该数

window.onload = function () {
for(var i=100;i<=999;i++) {
var ge = parseInt(i%10);
var shi = parseInt(i/10%10);
var bai = parseInt(i/100%10);
var result = Math.pow(ge,3)+Math.pow(shi,3)+Math.pow(bai,3);
if (i==result) window.console.log(i);
}
}
阶乘
window.onload = function () {
var kWeWeZhang = prompt("");
var resoult = 1;
for(var i=1;i<=parseInt(kWeWeZhang);i++) {
resoult*=i;
}
window.console.log(resoult);
}

斐波那契数列
0 1 1 2 3 5 8 13 21 。。。
window.onload = function () {
/*斐波那契数*/
function kFeiBoNaQi(kIndex) {
if (kIndex < 1) return 0;
if (kIndex == 1) {
return 0;
} else if (kIndex == 2) {
return 1;
} else {
return kFeiBoNaQi(kIndex - 1) + kFeiBoNaQi(kIndex - 2);
}
}

for (var i = 1; i<=20; i++) {
window.console.log(kFeiBoNaQi(i));
}
}


青蛙掉进10米深的井里,每次跳3米,下滑2米,问几次可以跳出井?
  var nResult = 0;
    var nCount = 0;
    while(true) {
        nCount++;
        nResult+=3;
        if(nResult>=10) break;
        nResult-=2;
    }
    console.log(nCount);

妈妈给小明100元买鸡,公鸡3元 母鸡5元 小鸡1元三只 ,如何用100元买到100
只鸡 每种鸡都要买到


    for (var countMu = 1;countMu<100/5;countMu++){
        for (var countGong = 0;countGong<(100-5*countMu)/3;countGong++){
            var countXiao = (100-5*countMu-3*countGong)*3;
            if (countMu+countGong+countXiao==100){
                console.log(countMu+"~"+countGong+"~"+countXiao);
            }
        }
    }

  

posted @ 2016-07-24 22:34  WeWeZhang  阅读(156)  评论(0编辑  收藏  举报