728x90 AdSpace

Latest News
Tuesday, February 20, 2018

bài tập java có lời giải 4


Bài 16. Nhập số liệu cho dãy số thực a0, a1,..., an-1. In ra màn hình số lần xuất hiện của các phần tử. 
Bài 17. Nhập số n và dãy các số thực a0, a1,..., an-1. Không đổi chỗ các phần tử và không dùng thêm mảng số thực nào khác (có thể dùng mảng số nguyên nếu cần) hãy cho hiện trên màn hình dãy trên theo thứ tự tăng dần. 
Bài 18. Nhập một xâu ký tự. Đếm số từ của xâu ký tự đó. Thí dụ " Trường học " có 2 từ. Xem giải Bài 18
Bài 19. Viết chương trình liệt kê tất cả các số nguyên tố có 5 chữ số sao cho tổng của các chữ số trong mỗi số nguyên tố đều bằng S cho trước.
Bài 20. Nhập một số tự nhiên n. Hãy liệt kê các số Fibonaci nhỏ hơn n là số nguyên tố. 
 Lời Giải
Bài 16: 
package bai16;
import java.util.Scanner; 
public class Main {

  public static int nhap(){
                Scanner input= new Scanner(System.in); 
                boolean check= false;
                int n=0; 
                while(!check){
                       System.out.print(" "); 
                       try{
                              n= input.nextInt(); 
                              check= true;
                       }catch(Exception e){
                              System.out.println("Ban phai nhap so! hay nhap lai..."); 
                              input.nextLine();
                       }
                }
                return (n);
       }
  public static int countElement(int a[], int n, int i){ 
                int count= 0;
                for(int j=0 ; j<n ; j++){
                        if(a[j]== i) 
                        count ++;
                }
                return (count);
      }
  public static void main(String[] args) { 
     int n,i;
                System.out.println("Nhap n= "); 
                n= nhap();
                int[] array= new int[n]; 
                for(i=0 ; i<n ; i++){
                        System.out.println("Nhap phan tu thu " +(i+1)+" "); 
                        array[i]= nhap();
                }
                for(i=0 ; i<n ; i++){
                        if(countElement(array, i, array[i])==0){
                                System.out.println("Phan tu "+array[i]+ " xuat hien "+countElement(array, n,
array[i])+" lan");
                        }
                }
    }
} 
Bài 17: 
package bai17; 
import java.util.Scanner; 
public class Main {
  public static int nhap(){
                Scanner input= new Scanner(System.in); 
                boolean check= false;
                int n=0; 
                while(!check){
                       System.out.print(" "); 
                       try{
                             n= input.nextInt(); 
                             check= true;
                       }catch(Exception e){
                              System.out.println("Ban phai nhap so! hay nhap lai..."); 
                              input.nextLine();
                       }
                }
                return (n);
      }
  public static float nhapFloat(){
                Scanner input= new Scanner(System.in); 
                boolean check= false;
                float n=0; 
                while(!check){
                       System.out.print(" "); 
                       try{
                             n= input.nextInt(); 
                             check= true;
                       }catch(Exception e){
                              System.out.println("Ban phai nhap so! hay nhap lai..."); 
                              input.nextLine();
                       }
                }
                return (n);
      }
  public static int viTriMinFloat(float a[], int n){ 
                float min= a[0];
                int key= 0;
                for(int j=0 ; j<n ; j++){
                        if(min>a[j]){
                               min= a[j]; 
                               key= j;
                        }
                }
                return (key);
      }
  public static float maxFloat(float a[], int n){ 
                float max= a[0];
                for(int j=0 ; j<n ; j++){
                         if(max<a[j]) max= a[j];
                }
                return (max);
     }
  public static void main(String[] args) {
    int n,i;
                System.out.println("Nhap n= "); 
                n= nhap();
                float[] array= new float[n]; 
                for(i=0 ; i<n ; i++){
                       System.out.println("Nhap phan tu thu " +(i+1)+" "); 
                       array[i]= nhapFloat();
                }
                i =0;
                System.out.println("Sap xep theo thu tu tang dan"); 
                while(i<n){
                      System.out.println(" "+array[viTriMinFloat(array, n)]); 
                             array[viTriMinFloat(array, n)]= maxFloat(array, n);
                i++;
                }
   }
} 
Bài 18: 
package bai18; 
import java.util.*; 
public class Main { 
  public static void main(String[] args) 
    { Scanner input= new 
    Scanner(System.in);
             System.out.println("Nhap vao 1 xau: "); 
             String str= input.nextLine();
             StringTokenizer strToken= new 
             StringTokenizer(str, " "); System.out.println("So cac tu trong xau la: "+strToken.countTokens());
  } 
} 
Bài 19:
package bai19;
import java.util.Scanner;
public class Main {

  public static int nhap(){
                Scanner input= new Scanner(System.in); 
                boolean check= false;
                int n=0; 
                while(!check){
                      System.out.print(" "); 
                      try{ 
                           n= input.nextInt(); 
                           check= true;
                      }catch(Exception e){
                             System.out.println("Ban phai nhap so! hay nhap lai..."); 
                             input.nextLine();
                      }
                }
                return (n);
     }
  public static boolean checkSNT(int n){
                if(n>1){
                for(int i=2;i<=Math.sqrt(n);i++){ 
                        if(n%i==0) return false;
                }
                return true;
     }
                else return false;
     }
  public static int tongChuSo(int n){ 
                int T=0;
                while(n>0){
                      T+= n%10;
                      n/= 10;
                }
                return (T);
     }
  public static void main(String[] args) { 
    System.out.print("Nhap S= ");
                 int s= nhap(); int i,count=0;
                 System.out.println("Cac so nguyen to co tong cac chu so co tong bang "+s+" la: ");
                 for(i=10000 ; i<=99999 ; i++){
                         if(checkSNT(i)){
                                 if(tongChuSo(i)== s) {
                                        System.out.println(" "+i); 
                                        count++;
                                 }
                                 else continue;
                         }
                 }
                 System.out.println("Co "+count+" so thoa man");
    } 
} 
Bài 20:
package bai20;
import java.util.Scanner; 
public class Main { 

  public static int nhap(){
                Scanner input= new Scanner(System.in); 
                boolean check= false;
                int n=0; 
                while(!check){
                       System.out.print(" "); 
                       try{
                              n= input.nextInt(); 
                              check= true;
                       }catch(Exception e){
                              System.out.println("Ban phai nhap so! hay nhap lai..."); 
                              input.nextLine();
                       }
               }
               return (n);
      }
//Ham kiem tra so nguyen to
  public static boolean checkSNT(int n){ 
         if(n>1){
         for(int i=2;i<=Math.sqrt(n);i++){ 
                   if(n%i==0) return false;
         }
         return true;
     }
     else return false;
     }
  public static void main(String[] args) { 
    System.out.print("Nhap n= ");
                int n= nhap();
                int[] f= new int[n]; 
                f[0]= 1; f[1]= 1;
                int i=1,count=1;
                System.out.print("Cac so Fibonanci nho hon "+n+" la so nguyen to: \n 1"); 
                while(f[i]<n){
                      if(checkSNT(f[i])){
                              System.out.print(" "+f[i]); 
                              count++;
                      } i++;
                      f[i]= f[i-1] + f[i-2];
      }
      System.out.println("\n Co "+count+" so thoa man");
   }
}  
  • Blogger Comments
  • Facebook Comments

0 comments:

Post a Comment

Item Reviewed: bài tập java có lời giải 4 Rating: 5 Reviewed By: Genm