• 第六周上机练习


    1.编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值。

    int[] arr=new int[]{10,20,30,40,50};
            for(int i=0;i<arr.length;i++){
            System.out.print(arr[i]+" ");
        }

    2.编写一个简单程序,要求数组长度为5,动态赋值10,20,30,40,50,在控制台输出该数组的值。

    nt[] arr=new int[5];
            arr[0]=10;
            arr[1]=20;
                arr[2]=30;
            arr[3]=40;
            arr[4]=50;
            System.out.println(arr[0]);
            System.out.println(arr[1]);
            System.out.println(arr[2]);
            System.out.println(arr[3]);
            System.out.println(arr[4]);
        

    3.编写一个简单程序,定义整型数组,里面的元素是{23,45,22,33,56},求数组元素的和、平均值

      int a[]= {23,45,22,33,56};
      double sum=0;
      for(int i=0;i<a.length;i++) {
      sum+=a[i];
      }
      System.out.println("和为"+sum+"平均值为"+sum/5);

    4.在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。

            public static void main(String[] args) {
                int a[]= {18,25,7,36,13,2,89,63},max=a[0],b=0;
                for(int i=1;i<a.length;i++) {
                    if(max < a[i]) {
                        max = a[i];
                        b = i;
                    }
                }
                System.out.println("最大数为"+max+"下标为"+b);
            }
        }

    5. 将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问)

    int[] a = {1,2,3,4,5};
                      
         int m;
         for(int i = 0; i < 2; i++){
                      
           m = a[i];
           a[i] = a[5-i-1];
           a[5-i-1] = m;
                     
     }
        for(int j =0; j < 5; j++){
         System.out.println(a[j]);
         }
                     
      }

    6、有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。

    public static void main(String[] args){
        int[] A = {1,2,4,5};
        int[] B = new int[5];
        int a = 3;
        for(int i =0; i< A.length; i++){
        if(A[i] > a){
            B[i] = a;
            for(i=i+1; i<B.length; i++ ){
                B[i] =A[i-1];    
            }
        }
        else{
            B[i] = A[i];
        }
    }
    
    for(int i = 0; i<5; i++){
        System.out.println(B[i]);
    }
    }
    }
  • 相关阅读:
    Revolving Digits[EXKMP]
    字符加密Cipher(bzoj 1031)
    Hotaru's problem
    1089 最长回文子串 V2(Manacher算法)
    3172: [Tjoi2013]单词
    3689: 异或之
    3942: [Usaco2015 Feb]Censoring [KMP]
    2795: [Poi2012]A Horrible Poem
    GT考试(bzoj 1009)
    NOIP2016提高组解题报告
  • 原文地址:https://www.cnblogs.com/Inati/p/12665830.html
Copyright © 2020-2023  润新知