zoukankan      html  css  js  c++  java
  • 4月9日上机练习

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

    public class TEXT {
        
        
        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,在控制台输出该数组的值。

    
    
    import java.util.Scanner;
    public class TEXT {
        
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
             
    
            
            
                    int ass [] = new int [5];
                    Scanner sc = new Scanner(System.in);
                    for(int i=0;i<ass.length;i++){
                        System.out.println("请输入数字");
                        ass[i] = sc.nextInt();
                    }
                    System.out.println("赋值结束------------");
              
              
                              
                          
                    }
                }
    
    
    
    
    


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

    public class TEXT {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int[] arr ;
            arr = new int[]{23,45,22,33,56};
    
            int sum = 0;
            double avg;
            int max = 0;
            int min = 0;
            for(int i = 0 ;i<arr.length - 1;i++){
                sum = sum +arr[i];
                if(arr[i]>max){
                    max = arr[i];
                }if(arr[i]<min){
                    min = arr[i];
                }
            }
            
            avg = sum/arr.length;
            System.out.println("数组中平均数为:"+avg);
            System.out.println("数组中和为:"+sum);
    
    
        }
    
    }


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

    public class TEXT {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int arr1[];
            int max = 0;
            int n = 0;
            arr1 = new int[]{18,25,7,36,13,2,89,63};
            max = arr1[0];
            for(int i = 0;i<arr1.length-1;i++){
    
            if(arr1[i]>max){
                    max = arr1[i];
                    n = i;
                }
            }
            System.out.println("最大的数为"+max+",其下标为 "+n);
    
        }
    
    }



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

    import java.util.Scanner;
    public class TEXT {
        
        
        public static void main(String[] args) {
        
                    Scanner s = new Scanner(System.in);
                    System.out.println("请输入数组长度");
                    int a = s.nextInt();
                    int b[] = new int[a];
                    for (int i = 0; i < b.length; i++) {
                        System.out.println("请输入第" + (i + 1) + "个元素");
                        b[i] = s.nextInt();
                    }
                    System.out.println("排序前:");
                    for (int i = 0; i < b.length; i++) {
                        System.out.print(b[i] + "	");
                    }
                    System.out.println();
                    int temp;
                    // 正序排序
                    for (int i = 0; i < b.length - 1; i++) {
                        for (int j = b.length - 1; j > i; j--) {
                            if (b[j] < b[j - 1]) {
                                temp = b[j];
                                b[j] = b[j - 1];
                                b[j - 1] = temp;
                            }
                        }
                    }
                    System.out.println("排序后:");
                    for (int i = 0; i < b.length; i++) {
                        System.out.print(b[i] + "	");
                    }
                    // 逆序排序
                    for (int i = 0; i < b.length - 1; i++) {
                        for (int j = b.length - 1; j > i; j--) {
                            if (b[j] > b[j - 1]) {
                                temp = b[j - 1];
                                b[j - 1] = b[j];
                                b[j] = temp;
                            }
                        }
                    }
                    System.out.println();
                    System.out.println("逆序排出:");
                    for (int i = 0; i < b.length; i++) {
                        System.out.print(b[i] + "	");
                    }
                    s.close();
                }
            }
                
    
    
    





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

    public class TEXT {
        
        
        public static void main(String[] args) {
            
                    int[] shu = {1,2,7,9};//原数组
                    int[] shu2 = new int[5];//输出数组
                    int a = 3;//要插入的数
                    for(int i =0; i< shu.length; i++){
                        if(shu[i] > a){
                            shu2[i] = a;
                            for(i = i + 1; i < shu2.length; i++ ){
                                shu2[i] = shu[i-1];
    
                                //System.out.println(shu2[i]);
                            }
                        }
                        else{
                            shu2[i] = shu[i];
                        }
                    }
                    //循环输出
                    for(int i = 0; i<5; i++){
                        System.out.println(shu2[i]);
                    }
                }
            }
  • 相关阅读:
    志愿者招募 [NOI2008] [鬼畜网络流]
    莫队入门
    分块入门
    高速公路 [HAOI2012] [线段树]
    游历校园 [COGS 614] [欧拉图]
    网络吞吐量 [CQOI2015] [网络流]
    LeetCode 27. Remove Element
    LeetCode 26. Remove Duplicates from Sorted Array
    LeetCode 21. Merge Two Sorted Lists
    LeetCode 20. Valid Parentheses
  • 原文地址:https://www.cnblogs.com/zhangjun19991118/p/12665332.html
Copyright © 2011-2022 走看看