Đáp án 42 bài tập lập trình Java cơ bản
package bai01;
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 UCLN(int a, int b){
while(a!= b){
if(a>b) a= a-b;
else b= b-a;
}
return (a);
}
public static void main(String[] args) {
System.out.println("Nhap a");
int a = nhap();
System.out.println("Nhap b");
int b= nhap();
System.out.println("Uoc chung lon nhat cua "+a+" va "+b+" la: "+UCLN(a,b));
System.out.println("Boi chung nho nhat cua "+a+" va "+b+" la: "+((a*b)/UCLN(a,b)));
}
}
o nguyen to"); } //Sap xep cac cot theo thu tang dan public static void sortColum(int[][] A, int n, int m){ int i,j,temp; for(j=0 ; j<m ; j++){ for(i=1 ; i<n ; i++){ if(A[i-1][j]>A[i][j]){ temp= A[i-1][j]; A[i-1][j]= A[i][j]; A[i][j]= temp; } } } inMT(A, n, m); } public static void main(String[] args) { System.out.print("Nhap so hang n="); int n= nhap(); System.out.print("Nhap so cot m="); int m= nhap(); int [][] A= new int[n][m]; int i,j; for(i=0 ; i<n ; i++){ for(j=0 ; j<m ; j++){ System.out.println("Nhap phan tu thu A["+(i+1)+"]["+(j+1)+"]= "); A[i][j]= nhap(); } } System.out.println("Ma tran nhap vao: "); inMT(A, n, m); for(i=0 ; i<n ; i++){ for(j=0 ; j<m ; j++){ if(A[i][j]==findMaxMT(A, n, m))System.out.println("\nPhan tu o hang "+i+" cot "+j+" dat Max: A["+i+"]["+j+"]= "+A[i][j]); } } phanTuSNT(A, n, m); sortColum(A, n, m); } } Bài 25 : package bai25; public class Main { 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 boolean testSoThuanNghich(int n){ StringBuilder xau= new StringBuilder(); String str= ""+n; xau.append(str); String check= ""+xau.reverse(); if(str.equals(check)) return true; else return false; } public static boolean nguyenTo(int n){ while(n!=0){ if(!checkSNT(n%10)) return false; n/= 10; } return true; } public static void main(String[] args) { int i,count= 0; System.out.println("cac so tu 5-7 chu so thoa man dieu kien la: "); for(i=22223 ; i<7777777 ; i+=2){ if(checkSNT(i) && nguyenTo(i) && testSoThuanNghich(i)){ System.out.println(" "+i); count++; } } System.out.println("\n Co "+count+" so thoa man"); } } Bài 26 : package bai26; public class Main { 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 boolean testSoThuanNghich(int n){ StringBuilder xau= new StringBuilder(); String str= ""+n; xau.append(str); String check= ""+xau.reverse(); if(str.equals(check)) return true; else return false; } public static boolean nguyenToTong(int n){ int T= 0; while(n!=0){ T+= n%10; if(!checkSNT(T)) return false; n/= 10; } return true; } public static void main(String[] args) { int i,count= 0; System.out.println("cac so tu 5-7 chu so thoa man dieu kien la: "); for(i=22223 ; i<7777777 ; i+=2){ if(checkSNT(i) && nguyenToTong(i) && testSoThuanNghich(i)){ System.out.println(" "+i); count++; } } System.out.println("\n Co "+count+" so thoa man"); } } Bài 27 : package bai27; import java.util.Arrays; 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 viTriMaxInt(int a[], int n){ int max= a[0]; int key= 0; for(int j=0 ; j<n ; j++){ if(max<a[j]){ max= a[j]; key= j; } } return (key); } public static void inArray(int[] a, int begin , int end){ System.out.println(); int i; for(i=begin ; i<end ; i++){ System.out.print(" "+a[i]); } System.out.println(); } public static int viTriMax2(int[] a,int n){ int i,key=0,Max2=0; for(i=0 ; i<n ; i++){ if(a[i]>Max2 && a[i]!= a[viTriMaxInt(a, n)]){ Max2= a[i];key= i; } }return (key); } public static void themPhanTu(int[] a,int n,int pt){ a[0]= pt; Arrays.sort(a); } public static void main(String[] args) { System.out.print("Nhap n= "); int n= nhap(); int[] a= new int[n+1]; int i; for(i=0 ; i<n ; i++){ System.out.print("\n Nhap phan tu thu "+i+" = "); a[i]= nhap(); } for(i=0 ; i<n ; i++){ if(a[i]== a[viTriMax2(a, n)]) System.out.println(" Phan tu thu "+i+" lon thu 2 trong mang a["+i+"]= "+a[i]); } Arrays.sort(a); inArray(a,1,n+1); System.out.print("Nhap phan tu muon them pt= "); int pt= nhap(); themPhanTu(a,n+1,pt); inArray(a,0,n); } } Bài 28 : package bai28; 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 void inMT(int[][] A, int n, int m){ int i,j; for(i=0 ; i<n ; i++){ System.out.print("\n"); for(j=0 ; j<m ; j++) System.out.print(" "+A[i][j]); } } public static int findMaxMT(int[][] A, int n, int m){ int Max= A[0][0]; for(int i=0 ; i<n ; i++){ for(int j=0 ; j<m ; j++){ if(Max<A[i][j]) Max= A[i][j]; } } return (Max); } public static void main(String[] args) { System.out.print("Nhap so hang n="); int n= nhap(); System.out.print("Nhap so cot m="); int m= nhap(); int [][] A= new int[n][m]; int i,j; for(i=0 ; i<n ; i++){ for(j=0 ; j<m ; j++){ System.out.println("Nhap phan tu thu A["+(i+1)+"]["+(j+1)+"]= "); A[i][j]= nhap(); } } //In ra ma tran nhap vao System.out.println("Ma tran nhap vao: "); inMT(A, n, m); //Tim phan tu max for(i=0 ; i<n ; i++){ for(j=0 ; j<m ; j++){ if(A[i][j]==findMaxMT(A, n, m))System.out.println("\nPhan tu o hang "+i+" cot "+j+" dat Max: A["+i+"]["+j+"]= "+A[i][j]); } } } } Bài 29 : package bai29; 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 void horner(int a[], int n){ int T=0,i,x; System.out.println("\n Nhap x= "); x= nhap(); T= a[n]; for(i=n ; i>0 ; i--){ T= T*x + a[i-1]; } System.out.println("Gia tri cua da thuc tinh theo cong thuc Horner la: "+T); } public static void inDaThuc(int a[], int n){ int i; for(i=n ; i>0 ; i--){ System.out.print(" "+a[i]+".x^"+i+" + "); } System.out.print(" "+a[0]); } public static void daoHam(int a[],int dh[], int n){ int i; for(i=n ; i>0 ; i--){ dh[i-1]= a[i]*i; } inDaThuc(dh,n-1); } public static void tongDaThuc(int a[], int n, int b[], int m){ int i; System.out.println("\n Tong cua 2 da thuc A va B la: "); if(n>m){ for(i=m ; i>=0 ; i--){ a[i]+=b[i]; } inDaThuc(a,n); } else for(i=n ; i>=0 ; i--){ b[i]+=a[i]; } inDaThuc(b,m); } public static void main(String[] args) { int i; System.out.println("Nhap bac cua da thuc n= "); int n= nhap(); int[] array= new int[n+1]; for(i=n ; i>=0 ; i--){ System.out.print("Nhap he so cua x^"+i+" = "); array[i]= nhap(); } System.out.println("Da thuc A nhap vao la: "); inDaThuc(array, n); horner(array, n); //Tinh dao ham bac 1 int[] dh= new int[n+1]; daoHam(array,dh,n); //Tinh tong 2 da thuc System.out.println("\n Nhap bac cua da thuc m= "); int m= nhap(); int[] array2= new int[m+1]; for(i=m ; i>=0 ; i--){ System.out.print("Nhap he so cua x^"+i+" = "); array2[i]= nhap(); } System.out.println("Da thuc B nhap vao la: "); inDaThuc(array2, m); tongDaThuc(array, n, array2, m); } } Bài 31 : import java.util.*; public class Bai31 { public static String chuyenInHoa(String str){ String s,strOutput; s= str.substring(0, 1); strOutput= str.replaceFirst(s,s.toUpperCase()); return (strOutput); } public static String chuanHoa(String strInput){ String strOutput=""; StringTokenizer strToken= new StringTokenizer(strInput," ,\t,\r"); strOutput+=""+chuyenInHoa(strToken.nextToken()); while(strToken.hasMoreTokens()){ strOutput+=" "+chuyenInHoa(strToken.nextToken()); } return(strOutput); } public static void main(String[] args) { // TODO Auto-generated method stub Scanner input= new Scanner(System.in); System.out.println("Nhap vao 1 xau: "); String strInput= input.nextLine(); System.out.println("Xau duoc chuan hoa la: "+chuanHoa(strInput)); } } Bài 32 : import java.util.*; public class Bai32 { public static void timXauMax(String strInput){ StringTokenizer strToken= new StringTokenizer(strInput," ,\t,\r"); int Max,i=1,lengthStr; Max= strToken.nextToken().length(); int viTriMax= i; while(strToken.hasMoreTokens()){ lengthStr= strToken.nextToken().length(); i++; if(Max < lengthStr){ Max= lengthStr; viTriMax= i; } } System.out.println("Do dai xau lon nhat la: "+Max+" o vi tri "+viTriMax); } public static void main(String[] args) { // TODO Auto-generated method stub Scanner input= new Scanner(System.in); System.out.println("Nhap vao 1 xau: "); String strInput= input.nextLine(); timXauMax(strInput); } } Bài 33 : import java.util.*; public class Bai33 { public static String doiViTri(String strInput){ String str= Bai31.chuanHoa(strInput); StringTokenizer strToken= new StringTokenizer(str," "); String ho = strToken.nextToken(); String hoDem = strToken.nextToken(); String ten = strToken.nextToken(); String strOutput= ten+" "+ho+" "+hoDem; return(strOutput); } public static void main(String[] args) { // TODO Auto-generated method stub Scanner input= new Scanner(System.in); System.out.println("Nhap vao ho ten ( ho-ho dem -ten) : "); String strInput= input.nextLine(); System.out.println("Ho va ten duoc sap xep lai (ten- ho - ho dem) : "+doiViTri(strInput)); } }
File đính kèm:
- Đáp án 42 bài tập lập trình Java cơ bản.doc