SeanNers

DistinctNumber

Aug 16th, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. package vn.com.hoangminh.Physics;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class DistinctNumber {
  6.     public static void main(String[] args) {
  7.        
  8.         //Khai báo Var
  9.         Scanner input = new Scanner(System.in);
  10.         System.out.print("Chương trình loại bỏ các số bị lặp lại trong các số đã nhập \nNhập số chữ số cần nhập : ");
  11.         int soChuSo = input.nextInt();
  12.         int[] nhap = new int[soChuSo];
  13.         int[] xuat = new int[soChuSo];
  14.         int j = 0;
  15.        
  16.         //Declare input vào array
  17.         for( int i = 0 ; i < soChuSo ; i++ ) {
  18.             System.out.print("Nhập số thứ " + (i+1) +" : ");
  19.             nhap[i] = input.nextInt();
  20.         }
  21.        
  22.         //Tìm số bị lặp và đăng output
  23.         for ( int a = 0 ; a < soChuSo ; a++ ) {
  24.             for ( int b = 0 ; b < soChuSo ; b++ ) {
  25.                 if( nhap[a] == xuat[b] ) break;
  26.                 if( b == soChuSo - 1 ) {
  27.                     xuat[j] = nhap[a];
  28.                     j++;
  29.                 }
  30.             }
  31.            
  32.         }
  33.        
  34.         //Output
  35.         System.out.print("Kết quả sau khi loại bỏ các số bị lặp : ");
  36.        
  37.         for ( int c = 0 ; c < j ; c++ ) {
  38.             System.out.print(" " + xuat[c] );
  39.         }
  40.        
  41.        
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment