Facebook
From Nguyễn Thành Đạo, 3 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 63
  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 baitap;
  7.  
  8. import java.util.Scanner;
  9.  
  10. /**
  11.  *
  12.  * @author Admin
  13.  */
  14. public class SoDoiXung {
  15.  
  16.     int n;
  17.  
  18.     public void input() {
  19.         Scanner kb = new Scanner(System.in);
  20.         do {
  21.             System.out.println("Nhap vao so nguyen n: ");
  22.             n = kb.nextInt();
  23.         } while (n <= 0);
  24.     }
  25.     public void ouput1(){
  26.         System.out.println(" cac so doi  xung nho hon "+n);
  27.         for (int i = 0; i < n; i++) {
  28.             if(checkSDX(i)){
  29.                 System.out.print(i+" ");
  30.             }
  31.         }
  32. }
  33.     public void output2(){
  34.         Scanner kb = new Scanner(System.in);
  35.         int s =0,dem=0;
  36.         for (int i = 0; i < n; i++) {
  37.             System.out.println("Nhap vao so nguyen thu "+(i+1));
  38.             int a = kb.nextInt();
  39.             if(checkSDX(a)){
  40.                 s+=a;
  41.                 dem++;
  42.             }
  43.         }
  44.         if(dem==0)System.out.println("TBC so doi xung la:"+0);
  45.         System.out.println("TBC so doi xung la: "+s*1.0/dem);
  46.     }
  47.     private boolean checkSDX(int n) {
  48.         int temp = n,s=0;
  49.         while(temp!=0){
  50.            s=s*10+temp%10;
  51.            temp/=10;
  52.         }
  53.         return s==n;
  54.     }
  55.     public static void main(String[] args) {
  56.         SoDoiXung p = new SoDoiXung();
  57.         System.out.println("Thuc hien bai 1:");
  58.         p.input();
  59.         p.ouput1();
  60.         System.out.println("\nThuc hien bai 2:");
  61.         p.input();
  62.         p.output2();
  63.     }
  64. }