博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
冒泡排序、36选7不重复、水仙花数、九九乘法表等案例
阅读量:4939 次
发布时间:2019-06-11

本文共 4002 字,大约阅读时间需要 13 分钟。

九九乘法表

var str = '<table border=1px>';

         for(var i = 1;i <=9 ;i++ ){

                   str += '<tr>';

                   for(var j = 1;j <=9 ;j++ ){

                            // str += "<td></td>"

                            if (i>=j) {

                                     a = i*j;

                                     str = str + '<td>' + i + '*' + j + '='+ (i*j)+ '</td>' ;

                                     // document.write(a +'='+ j +'*'+ i);

                            }

                   }

                   str = str +  '</tr>';

                   document.write('<br>');

         }       

         str += '</table>';

         document.write(str);

纸的厚度

var a = 0.00001;

var sum = 0;

for (i = 1; i<=30;i++){

         // sum = sum + 2*a;

         a = 2*a;

}

document.write(a +'<br>'+'米');

 

var a = 0.00001;

var sum = 0;

for (i = 1; i<=30;i++){

         // sum = sum + 2*a;

         a = 2*a;

         if (a >=1) {

                   document.write('对折'+i+'次超过1米');

                   break;

         }

}

36个数选7个,不重复

var arr = [];

var getNum = [];//存放取出来的七个数

for(var i = 0; i < 36;i++){

         // arr[i] = i+1;

         arr.push(i+1);

}

for (i = 0; i < 7; i++){

         var index = parseInt(Math.random()*36;

                   var flag = true;//假设没有重复的数字,可以放入。

         // console.log(arr[index]);

         //不重 往里放和之前判断里面有没有相同的

         for(var k in getNum){

                   if(getNum[k] == arr[index]){

                            flag = false;

                            break;

                   }

         }

         if(flag){

                   getNum.push(arr[index]);

         }

         else{

                   i--;

         }

}

console.log(getNum);

水仙花数

for(i = 100;i < 1000;i++){

         // 百位

         a = parseInt(i/100);

         // 十位

         b = parseInt(i/10)%10;

         // 个位

         c = i%10;

 

         if (a*a*a+b*b*b+c*c*c == i) {

                   // alert(i +"是水仙花数");

                   document.write(i +"是水仙花数"+'<br>')

         }

         else{

                   // alert(i +"不是水仙花数");

         }

}

//六个整数

function sixnum(){

         var arr1 = [11,51,65,12,78,32];

         var sum = 0;

         // var con = 0;

                   for(var i = 0;i < arr1.length;i++){

                            var sum = (sum + arr1[i]);

                            // con++;

                   }

         // var avg = sum / con;

         document.write(sum/(arr1.length));

}

// 七个整数

function sevennum(){

         var arr1 = [11,51,65,12,78,32,56];

         var max = arr1[0];

         var min = arr1[0];

         var max_x = 0 ;

         var min_x = 0 ;

         for(var i = 0 ; i < arr1.length ; i++){

                   // var x = arr1[i];

                            if (arr1[i]>max) {

                                     max = arr1[i];

                                     max_x = i ;

                            }

                            if (arr1[i]<min) {

                                     min = arr1[i];

                                     min_x = i ;

                            }

         }

         document.write('最大值是'+ max + '下标是' +max_x + '<br>');

         document.write('最小值是'+ min + '下标是' +min_x);

         // arr.indexOf(max);找此数在数组中的位置。

}

// 九个整数

function ninenum(){

         var arr1 = [2,3,9,6,2,4,7,3,6];

         var sum = 0;

         var con = 0;

         for(var i = 0 ; i < arr1.length ; i++){

                   if (arr1[i]%2 != 0){

                            sum = sum + arr1[i];

                            con ++;

                   }

                   var avg = sum / con;

         }

         document.write('奇数的平均数是' + avg +'');

                  

}       

// 评委

function pingwei(){

         var arr1 =[9,8,4,6,7,1,2,3,2,5];

         var max = arr1[0];

         var min = arr1[0];

         var max_x = 0 ;

         var min_x = 0 ;

         var sum = 0;

         for(var i = 0 ; i < arr1.length ; i++){

                   // var x = arr1[i];

                   sum = sum + arr1[i] ;

                            if (arr1[i]>max) {

                                     max = arr1[i];

                                     max_x = i ;

                            }

                            if (arr1[i]<min) {

                                     min = arr1[i];

                                     min_x = i ;

                            }

         }

         // var avg = sum / 8;

         document.write('去掉的最大值是'+ max + '下标是' +max_x + '<br>');

         document.write('去掉的最小值是'+ min + '下标是' +min_x + '<br>');

         document.write('平均分是' + ((sum - max - min)/(arr1.length-2)));

}

// 四个随机数

function suijishu(){

         var str = '';

         var arr = [1,2,3,4,5,'a','b','c','d'];

         for(var i = 0; i < 4; i++){

                   var index =  parseInt(Math.random()*arr.length);

                   str += arr[index];

         }

         console.log(str);

}

//六个人学号、成绩

function classgoo(){

         var arr = [];

         var classname = ['语文成绩','数学成绩','英语成绩'];

         for ( var i = 0 ; i < 2;i++){

                   var name = prompt('请输入第'+ (i+1)+ '个名字');

                   var brr = [name]; //存放每一个学生的信息

                   for( var k in classname){  

                            brr[+k+1] = +prompt('请输入'+ name +'的'+ classname[k]);

                            // console.log(brr);

                   }

                   arr[i] = brr;

         }

         console.log(arr);

 

         for (var i in arr){

                   var sum = 0;

                   for(var k in classname){

                            sum += +arr[i][+k+1];

                            // arr[i][classname.length+1]

                            // arr[i][arr[i].length] +=

                   }

                    arr[i][arr[i].length] = sum;

                    arr[i][arr[i].length] = sum/(classname.length);

         }

         console.log(arr);

}

  // for  (k = 0 ; k < shuzu changdu ; k++)

三替换3的倍数,五替换5的倍数,三五替换既是3又是5的倍数。

for(var i = 1 ; i <=100 ; i++){

                   if (i%3 == 0 && i%5 == 0) {

                            document.write('三五' + '<br>');

                   }

                   else if (i%5 == 0) {

                            document.write('五' + '<br>');

                   }

                   else if (i%3 == 0) {

                            document.write('三' + '<br>');

                   }

                   else {

                            document.write(i + '<br>');

                   }

}

冒泡排序

function maopao(){

         var arr  = [5,8,2,9,3,7,1];

         for (var i = 0 ;i < arr.length;i ++ ){

                   for (var k = 0; k< arr.length -1 -i;k++){

                            if (arr[k] > arr[k+1]) {

                                     var temp = arr[k];

                                               arr[k] = arr[k+1];

                                               arr[k+1] = temp;

                            }

                   }

         }

         console.log(arr);

}

 

转载于:https://www.cnblogs.com/Ice-K/p/10539374.html

你可能感兴趣的文章
末日了,天是灰色的。
查看>>
Vuejs vm对象详解
查看>>
自定义RatingBar的一个问题(只显示显示一个星星)
查看>>
剑指Offer--二叉树的镜像
查看>>
PAT-BASIC-1031-查验身份证
查看>>
Python笔记5----集合set
查看>>
连连看小游戏
查看>>
js二级联动
查看>>
谜题32:循环者的诅咒
查看>>
RMI
查看>>
动态切换多数据源的配置
查看>>
win7电脑调整分区后分区不见的文件寻回法子
查看>>
《第一行代码》学习笔记2-Android开发特色
查看>>
bzoj3396 [Usaco2009 Jan]Total flow 水流
查看>>
20165231 2017-2018-2 《Java程序设计》第3周学习总结
查看>>
(180905)如何通过梯度下降法降低损失----Google机器学习速成课程笔记
查看>>
(响应式PC端媒体查询)电脑屏幕分辨率尺寸大全
查看>>
Crystal Reports拉报表报错:Error detected by database DLL
查看>>
Java获取新浪微博cookies
查看>>
面试题总结
查看>>