• 第七次作业


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

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

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

    package d07;
    
    public class lol {
    
        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;
            int i;
            for(i=0;i<5;i++){
                System.out.println(a[i]);
            }
    
        }
    
    }

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

    package d07;
    
    public class lol {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int[] a = new int[] { 23, 45, 22, 33, 56 };
            int i;
            int sum = 0;
            for (i = 0; i < 5; i++) {
                sum += a[i];
            }
            System.out.println("总和=" + sum);
            System.out.println("平均值=" + sum / 5);
    
        }
    
    }

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

    package d07;
    
    public class lol {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int[] a = new int[]{18,25,7,36,13};
            int i;
            int n=0;
            int max=a[0];
            for(i=0;i<5;i++){
                if(a[i]>max){
                    max=a[i];
                    n=i;
                }
            }
            System.out.println(max);
            System.out.println(n);
            
        }
    
    }

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

    package d07;
    
    public class lol {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int [] a = {18,25,7,36,13,2,89,63};
            for (int i = 0; i < a.length / 2; i++) {  
                 int temp = a[i];
                 a[i] = a[a.length - 1 - i];
                 a[a.length - 1 - i] =temp;
              }  
                 for (int i = 0; i < a.length; i++) {  
                   System.out.println(a[i] + " ");  
              } 
        }
    
    }

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

  • 相关阅读:
    BestCoder Round #32
    USACO Runaround Numbers
    USACO Subset Sums
    USACO Sorting a Three-Valued Sequence
    USACO Ordered Fractions
    USACO 2.1 The Castle
    Codeforces Round #252 (Div. 2)
    Codeforces Round #292 (Div. 2)
    BZOJ 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
    BZOJ 1603: [Usaco2008 Oct]打谷机
  • 原文地址:https://www.cnblogs.com/ld1017/p/12665766.html
Copyright © 2020-2023  润新知