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

    1.定义长度位5的整型数组,输入他们的值,用冒泡排序后输出

    package mjm;
    
    import java.util.Scanner;
    public class ddd {
         public static void main(String[] args) {
        
                     int[] a = new int[5];
                     int i, j;
                     int temp;
                     Scanner input = new Scanner(System.in);
                     System.out.println("请输入5个整数:");
                     for (i = 0; i < 5; i++) {
                         a[i] = input.nextInt();
                     }
                     for (i = 0; i < a.length - 1; i++) {
                         for (j = 0; j < a.length - 1 - i; j++) {
                             if (a[j] > a[j + 1]) {
                                 temp = a[j];
                                 a[j] = a[j + 1];
                                 a[j + 1] = temp;
                             }
                         }
                     }
                     System.out.println("排序后:");
                     for (i = 0; i < a.length; i++) {
                         System.out.println(a[i] + "	");
                     }
                 }
             
    
    }

    2.定义数组{34,22,35,67,45,66,12,33},输入一个数a,查找在数组中是否存在,如果存在,输出下标,不存在输出"not found"

    package mjm;
    
    import java.util.Scanner;
    public class ddd {
          public static void main(String[] args) {
               Scanner input=new Scanner(System.in);
                int[] x={34,22,35,67,45,66,12,33};
                System.out.println("请输入一个数:");
                int a=input.nextInt();
                for(int i=0;i<x.length;i++){
                    if(x[i]==a){
                        System.out.println("该数存在于数组中,下标为"+i);
                    }else{
                        System.out.println("not found");
                    }break;
                }
     
    
    
    
    
            }
        }

     3.以矩阵的形式输出一个double型二维数组(长度分别为5、4,值自己设定)的值。

    package mjm;
    
    import java.util.Scanner;
    public class ddd {
          public static void main(String[] args) {
                  double arr[][] = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 },
                               { 11, 12, 13, 14, 15 }, { 16, 17, 18, 19, 20 } };
                        for (int i = 0; i < arr.length; i++) {
                            for (int j = 0; j < arr[i].length; j++) {
                                System.out.print(arr[i][j] + "	");
                            } System.out.println();
                        }
                   }
               }

    4.定义一个二维数组(长度分别为3,4,值自己设定),求该二维数组的最大值.

    package mjm;
    
    import java.util.Scanner;
    public class ddd {
          public static void main(String[] args) {
              int a[][] = {{1,6,8,9}, {1,3,1,3,}, {4,5,6,2,}};
              int max = a[0][0];
              for (int i = 0; i < a.length; i++) {
                  for (int j = 0; j < a[i].length; j++) {
                      if (a[i][j] > max) {
                          max = a[i][j];
                              }
                          }
                      }
                      System.out.println("最大值=" + max);
                  }
          }

  • 相关阅读:
    HDU 4745 Two Rabbits (区间DP)
    HDU 1007 Quoit Design最近点对( 分治法)
    acdream 小晴天老师系列——我有一个数列! (ST算法)
    HDU 3592 World Exhibition (差分约束,spfa,水)
    HDU 5501 The Highest Mark (贪心+DP,经典)
    HDU 5500 Reorder the Books (水题)
    HYSBZ 1010 玩具装箱toy (决策单调DP)
    POJ 3017 Cut the Sequence (单调队列优化DP)
    Vijos P1243 生产产品 (单调队列优化DP)
    PIVOT&UNPIVOT
  • 原文地址:https://www.cnblogs.com/dhy-com/p/12695797.html
Copyright © 2011-2022 走看看