zoukankan      html  css  js  c++  java
  • 第六次作业

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

    package learn;
    
    public class learn1 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            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,在控制台输出该数组的值。

    package learn;
    
    public class learn1 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int a[]= new int[5];
            a[0]=10;
            a[1]=20;
            a[2]=30;
            a[3]=40;
            a[4]=50;
            for(int i=0;i<a.length;i++) {
                System.out.println(a[i]);
            }
        }
        
        }

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

    package learn;
    
    public class learn1 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            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)的数组中找出其中最大的数及其下标。

    package learn;
    
    public class learn1 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int[] a = new int[]{18,25,7,36,13,2,89,63};
            int x = 0; 
            int max = a[0];
            for(int i=1;i<a.length-1;i++) //这行的i表示循环的次数不是指下标
            {
                if(a[i]>max) //这行的i表示下标,以为有a[i]
                {
                    max=a[i];
                    x=i;
                }
            }
            System.out.println("最大的数为" + max  + ";下标为" + x);
            }
        }

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

    package learn;
    public class learn1 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int num[] = new int[] { 1, 2, 3, 4, 5, 7 };
            int num2[] = new int[num.length];
            int index = 0;
            for (int i = num.length - 1; i >= 0; i --) {
            num2[index] = num[i];
            index ++;
            }
            num = num2;
            for(int n:num){
            System.out.println(n);
            }
        }
    }

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

    package learn;
    import java.util.Scanner;
    public class learn1 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int[] a = {1,2,5,8,9,11,13,19};
            int[] b = new int[a.length + 1];
            System.out.print("请输入一个数字(6或者10或者15):");
            Scanner in = new Scanner(System.in);
            //获取待插入的数字
            int n = in.nextInt();
            //获取插入的位置
            int index = 0;
            for(int i=1; i<a.length-1; i++) {
                if(a[i] <= n && a[i+1] >=n) {
                    index = i+1;
                    break;
                }
            }
            
            for(int k=0; k<index; k++) {
                b[k] = a[k]; 
            }
            //插入一个数字
            b[index] = n;
            //拷贝后面部分
            for(int m=index+1; m <b.length; m++) {
                b[m] = a[m-1];
            }
            //打印数组
            for(int o:b) {
                System.out.print(o + " ");
            }
            in.close();
    }
    }

  • 相关阅读:
    PTA(Advanced Level)1037.Magic Coupon
    PTA(Advanced Level)1033.To Fill or Not to Fill
    PTA(Basic Level)1020.月饼
    PTA(Advanced Level)1048.Find Coins
    PTA(Advanced Level)1050.String Subtraction
    PTA(Advanced Level)1041.Be Unique
    PTA(Basci Level)1043.输出PATest
    PTA(Basic Level)1039.到底买不买
    PTA(Basic Level)1033.旧键盘打字
    PTA(Advanced Level)1083.List Grades
  • 原文地址:https://www.cnblogs.com/zhangbowen123/p/12665751.html
Copyright © 2011-2022 走看看