• 随机数组的求余运算



          时间是2017年5月6日,青年节刚过,坐标上海,沪漂2个月。

          就这样简短地开始吧。                                by:slowcity


    【案例分析1】

    要求定义一个int 型数组a,包含100 个元素,保存100个随机的4 位数。再定义一个int 型数组b,包含10 个元素。统计a 数组中的元素对10 求余等于0 的个数,保存到 b[0]中;对10 求余等于1 的个数,保存到b[1]中,……依此类推

    一般看到这类的最基本的思路:
    1先建立一个含数组存随机四位数
    2检索数组,进行条件运算计数
    3将计数结果存储到新的数组
    4输出打印新数组
    刚开始的我-菜鸟级新手的思路:看完代码感觉太耿直的boy

    1 import java.util.Random;
     2 public class Demo1 {
     3     public static  void main(String[]args){
     4         int a[]=new int  [100];
     5         int b1=0 ,b2=0,b3=0,b4=0,b5=0,b6=0,b7=0,b8=0,b9=0,b10=0;
     6         for(int i=0;i<a.length;i++){
     7             int max=9999;     //生成100个4位随机数,并保存到数组中
     8                 int min=1000;
     9                    Random random = new Random();
    10                 int s = random.nextInt(max)%(max-min+1) + min;
    11             a[i]=s;                      
    12             System.out.print (a[i]+"  ");
    13             if(a[i] / 10==0){                //将数组中元素对10求余运算,并计数
    14               b1++;
    15               System.out.print (b1) ;
    16             }else if (a[i]%10==1){
    17                 b2++;
    18             }else if(a[i]%10==2){
    19                 b3++;
    20                 }else if(a[i]%10==3){
    21                     b4++;
    22                 }else if (a[i]%10==4){
    23                 b5++;
    24             }else if(a[i]%10==5){
    25                 b6++;
    26                 }else if(a[i]%10==6){
    27                    b7++;
    28                 }else if (a[i]%10==7){
    29                 b8++;
    30             }else if(a[i]%10==8){
    31                 b9++;
    32                }else if(a[i]%10==9){
    33                b10++;
    34                 }
    35         }
    36         int b[]=new int [10];         //定义新的数组,存储计数
    37                 b[0]=b1;
    38                 b[1]=b2;
    39                 b[2]=b3;
    40                 b[3]=b4;
    41                 b[4]=b5;
    42                 b[5]=b6;
    43                 b[6]=b7;
    44                 b[7]=b8;
    45                 b[8]=b9;
    46                 b[9]=b10;
    47         for(int j=0;j<b.length;j++){
    48             System.out.println(b[j]);   //使用for循环,打印新的数组
    49         } 
    50     }
    51 }

    优化之后

    【优化一】
    相比较而言,代码的长度少了一大半,算法的逻辑性更强了
    1
    public class Demo2 { 2 public static void main(String[]args){ 3 int a[]=new int [100];           //声明长度为100的数组a 4 int b[]=new int [10];           //声明长度为10的数组b 5 for(int i=0;i<a.length;i++){       //生成4位数,存储在a数组中 6 a[i]=(int)(10000*Math.random()); 7 } 8 for(int j=0;j<b.length;j++){     //循环体嵌套 9 int sum=0; 10 for(int i=0;i<a.length;i++){ 11 if(a[i]%10==j){ 12 sum++; 13 } 14 } 15 b[j]=sum; 16 System.out.println(b[j]);        //打印数组b 17 } 18 } 19 }
    【优化二】

    1
    import java.util.Random; 2 3 public class RadomDemo{ 4 public static void main(String[] args){ 5 int [] a=new int[100]; 6 int [] b=new int [10]; 7 Random ran=new Random(); //定义run方法-生成随机数 8 int index=0; 9 while(index<100){ //使用while进行条件筛选 10 int tmp=ran.nextInt(); //生成随机数,且筛选 ---此处在代码运行时会慢很多 11 if((tmp<=9999&&tmp>=1000)){ 12 a[index]=tmp; //将筛选的随机数进行存储 13 index ++; 14 System.out.println(tmp); //此处输出不必要 15 } 16 } 17 System.out.println("-----------------------------------"); 18 for(int j=0;j<10;j++){        //嵌套循环 19 int num=0; 20 for(int i:a){ //for-each增强语句,遍历数组 21 if(i%10==j){ 22 num++; 23 } 24 } 25 b[j]=num; //将计数结果赋值给数据b 26 System.out.println(b[j]); 27 } 28 } 29 }

    学习总结:方法很重要,最重要的是简化算法,凝练代码。

  • 相关阅读:
    GRUB引导Win8,Win7,Ubuntu
    The vim syntax of systemd unit file
    Win8蓝屏(WHEA_UNCORRECTABLE_ERROR)
    C#生成Excel
    IE中使用IFrame或Frameset导致session丢失的问题
    Apache 配置详解 ( 最好的 APACHE 配置教程 )
    关于(enctype="multipart/formdata") post 提交时中文乱码解决方案(使用jspsmartupload时)
    Java获取当前时间
    windows中定时操作(SetTimer函数用法)
    _RecordsetPtr的 open函数
  • 原文地址:https://www.cnblogs.com/slowcity/p/6816916.html
Copyright © 2020-2023  润新知