Advertisement
rockman1510

Bai5_Exercise3

May 2nd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.72 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package javacore_season5_exercise3;
  7.  
  8. import java.util.Scanner;
  9.  
  10. /**
  11.  *
  12.  * @author HuyLV
  13.  */
  14. public class Exercise3 {
  15.  
  16.     public static int n;
  17.     public static int[] arr;
  18.  
  19.     /**
  20.      * @param args the command line arguments
  21.      */
  22.     public static void main(String[] args) {
  23.         // TODO code application logic here
  24.         input();
  25.         System.out.println("Original Array:");
  26.         for (int value : arr) {
  27.             System.out.print(value + "\t");
  28.         }
  29.  
  30.         System.out.println();
  31.         findMaxNumber(arr);
  32.  
  33.         System.out.println();
  34.         System.out.println("Decreasing Array:");
  35.         decreasing(arr);
  36.         for (int i = 0; i < arr.length; i++) {
  37.             if (arr[i] < 0) {
  38.                 arr[i] = 0;
  39.             }
  40.             System.out.print(arr[i] + "\t");
  41.         }
  42.         System.out.println("-------------Input Number-------------");
  43.         inputMember(arr);
  44.     }
  45.  
  46.     private static void input() {
  47.         Scanner scanner = new Scanner(System.in);
  48.         System.out.print("Nhap n phan tu cua mang: ");
  49.         n = scanner.nextInt();
  50.         arr = new int[n];
  51.         for (int i = 0; i < arr.length; i++) {
  52.             System.out.print("Nhap phan tu o vi tri " + i + " : ");
  53.             arr[i] = scanner.nextInt();
  54.             while (arr[i] < 0 || arr[i] > 100) {
  55.                 System.out.printf("Hay nhap lai phan tu thu %d (0 < phantu < 100): ", i);
  56.                 arr[i] = scanner.nextInt();
  57.             }
  58.         }
  59.     }
  60.  
  61.     public static void findMaxNumber(int[] arr) {
  62.         int total = 0;
  63.         int index = 0;
  64.         for (int value : arr) {
  65.             total += value;
  66.         }
  67.         int max = arr[0];
  68.         int temp = total - arr[0];
  69.         for (int i = 1; i < arr.length; i++) {
  70.             int result = total - arr[i];
  71.             if (result < temp) {
  72.                 temp = result;
  73.                 max = arr[i];
  74.                 index = i;
  75.             }
  76.         }
  77.         System.out.printf("So lon nhat: %d\t tai vi tri: %d\n", max, index);
  78.     }
  79.  
  80.     public static int[] decreasing(int[] arr) {
  81.         int temp;
  82.         for (int i = 0; i < arr.length; i++) {
  83.             for (int j = i; j > 0; j--) {
  84.                 if (arr[j] > arr[j - 1]) {
  85.                     temp = arr[j];
  86.                     arr[j] = arr[j - 1];
  87.                     arr[j - 1] = temp;
  88.                 }
  89.             }
  90.         }
  91.         return arr;
  92.     }
  93.  
  94.     public static void inputMember(int[] arr) {
  95.         Scanner scanner = new Scanner(System.in);
  96.         System.out.print("Hay nhap vao 1 so: ");
  97.         int number = scanner.nextInt();
  98.         int[] result = new int[arr.length + 1];
  99.         for (int i = 0; i < result.length; i++) {
  100.         // cach 1
  101.             if(i < result.length - 1){
  102.                 result[i] = arr[i];
  103.             } else{
  104.                 result[i] = number;
  105.             }
  106.         //
  107.        
  108.     /* Cach 2
  109.         boolean isAdded = false;
  110.         if (!isAdded) {
  111.                 if (i < arr.length) {
  112.                     if (arr[i] > number) {
  113.                         result[i] = arr[i];
  114.                     } else {
  115.                         result[i] = number;
  116.                         isAdded = true;
  117.                     }
  118.                 } else {
  119.                     result[i] = number;
  120.                 }
  121.             } else {
  122.                 result[i] = arr[i - 1];
  123.             }
  124.     */
  125.         }
  126.         decreasing(result);
  127.         for (int i = 0; i < result.length; i++) {
  128.             System.out.print(result[i] + "\t");
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement