Bài tập Java

/*

*In ra man hinh tat ca cac hop so <100

**/

public class bai05 {

public static void main(String[] args)

{

 int k,n,dem; for(k=1;k<=100;k++) { dem=0; // dat ja tri cua bien dem=0 ung voi moi ja tri cua k for(n=2;n<=k;n++) { if(k%n==0) // neu so du khi chia k cho n =0 {

 dem++; // thi bien dem duoc cong them 1 don vi }

 } if(dem>1) { System.out.println("hop so la"+k); } } }

}

pdf25 trang | Chuyên mục: Java | Chia sẻ: dkS00TYs | Lượt xem: 3189 | Lượt tải: 4download
Tóm tắt nội dung Bài tập Java, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
ay so nguyen a[ ];b[ ].Hay kiem tra xem a[ ] co la day con cua b[ ] hay ko 
 */ 
public class mang12 { 
 public static void main(String[] args) { 
 int [] a={0,1,2,3}; 
 int [] b={0,9,1,2,8,3,8,8,9}; 
 int M=9,N=4,k,x=0,j; 
 int in=0; 
 for(j=0;j<N;j++) 
 { 
 for(k=x;k<M;k++) 
 16
 { 
 if(a[j]==b[k]) 
 { 
 in++; 
 x=k+1; 
 break; 
 } 
 } 
 } 
 if (in==N) 
 System.out.print("day a la day con cua day b"); 
 else 
 System.out.print("day a ko la day con cua day b"); 
}} 
Kết quả 
day a la day con cua day b 
Process completed. 
IV/ Bài tập về xâu ( String) 
Bài 01 
/** 
 *Cho truoc 1 xau ky tu la ho ten nguoi day du nhung khi nhap co the thua mot so dau cach. 
 *Hay xoa di cac dau cach thua va in ra ho ten chinh xac. 
 */ 
public class String01 { 
 public static void main(String[] args) { 
 String S = new String (" Nguyen Thi Binh "); 
 String S1,S2 = new String (); 
 S=S.trim(); 
 for (int k=0; k<S.length();k++) 
 { 
 S1=S.substring(k,k+1); 
 if (S1.equals(" ")) 
 { 
 S1=S.substring(k+1,k+2); 
 if (S1.equals(" ")) 
 continue; 
 else S2=S2+S.substring(k,k+1); 
 } 
 else S2=S2+S1; 
 } 
 System.out.print(S2); 
 } 
} 
Kết quả 
Pepsi Milo Ovantine 
Process completed. 
Bài 02 
/** 
 *Cho truoc xau ky tu bat ky. Hay dem xem trong xau co bao nhieu lan xuat hien xau con “abc”. 
 17
 */ 
public class String02 { 
 public static void main(String[] args) { 
 String S = new String ("abc def ab cdfg abcabc"); 
 String S1= new String (); 
 int dem=0; 
 for (int k=0;k<S.length()-2;k++) 
 { 
 S1= S.substring(k,k+3); 
 if(S1.equals("abc")) 
 dem++; 
 } 
 System.out.print(dem); 
 } 
} 
Kết quả 
3 
Process completed. 
Bài 03 
/** 
 * Cho truoc 1 xau ky tu la ho ten nguoi day du, hay tach ra phan ten cua nguoi nay. 
 */ 
public class String03 { 
 public static void main(String[] args) { 
 String S = new String (" Nguyen Van An "); 
 String S1 = new String (); 
 S=S.trim(); 
 int k; 
 for (k=S.length()-1;k>=0;k--) 
 { 
 S1=S.substring(k,k+1); 
 if(S1.equals(" ")) 
 break; 
 } 
 System.out.print("Ten cua nguoi do la:"+S.substring(k+1)); 
 } 
} 
Kết quả 
Ten cua nguoi do la:An 
Process completed. 
Bài 04 
/** 
 *Cho truoc 1 xau ky tu la 1 ho ten nguoi day du, hay tach ra phan ho cua nguoi nay 
 */ 
public class String04 { 
 public static void main(String[] args) { 
 String S = new String (" Nguyen Van An "); 
 String S1 = new String (); 
 S=S.trim(); 
 int k; 
 18
 for (k=0;k<=S.length();k++) 
 { 
 S1=S.substring(k,k+1); 
 if(S1.equals(" ")) 
 break; 
 } 
 System.out.print("Ho cua nguoi do la:"+S.substring(0,k)); 
 } 
} 
Kết quả 
Ho cua nguoi do la:Nguyen 
Process completed. 
Bài 05 
/** 
 *Cho 1 xau ky tu bao gom toan cac ky tu 0, 1. Hay bien doi xau nay theo cach 0 -> 1, 1->0 va in ra ket 
qua. 
 */ 
public class String05 { 
 public static void main(String[] args) { 
 String S = new String ("010001110001100"); 
 String S1= new String (); 
 String S2= new String (); 
 for (int k=0;k<S.length();k++) 
 { 
 S1=S.substring(k,k+1); 
 if (S1.equals("0")) 
 S1="1"; 
 else 
 S1="0"; 
 S2=S2+S1; 
 } 
 System.out.print(S2); 
 } 
} 
Kêt quả 
101110001110011 
Process completed. 
Bài 06 
/** 
 *Cho truoc xau ky tu S, in ra xau S1 nguoc lai xau S. 
 */ 
public class String06 { 
 public static void main(String[] args) { 
 String S,S1,S2= new String (); 
 S="1234567890"; 
 for ( int k=S.length()-1;k>=0;k--) 
 { 
 S1=S.substring(k,k+1); 
 19
 S2=S2+S1; 
 } 
 System.out.print(S2); 
 } 
} 
Kết quả 
0987654321 
Process completed. 
Bài 07 
/** 
 * Cho truoc xau ky tu S. Hay bien doi S theo quy tac sau: chu so thi bien thanh “$” con cac ky tu khac 
giu nguyen. 
 **/ 
public class String07 { 
 public static void main(String[] args) { 
 String S = new String ("12a3456b78 c 90"); 
 String [] X= {"0","1","2","3","4","5","6","7","8","9"}; 
 String S1= new String (); 
 String S2= new String (); 
 for ( int k=0;k<S.length();k++) 
 { 
 S1=S.substring(k,k+1); 
 for (int j=0;j<10;j++) 
 if (S1.equals(X[j])) 
 { 
 S1="$"; 
 break; 
 } 
 S2=S2+S1; 
 } 
 System.out.print(S2); 
 } 
} 
Kết quả 
$$a$$$$b$$ c $$ 
Process completed. 
Bài 08 
/** 
 *Cho truoc 2 xau ky tu S1, S2. Hay dem xem xau S1 xuat hien trong S2 tai bao nhieu vi tri. 
 */ 
public class String08 { 
 public static void main(String[] args) { 
 String S1= new String ("abc def ghj abc ab c"); 
 String S2= new String ("abc"); 
 String S3= new String(); 
 int dem=0; 
 for (int k=0;k<S1.length()-S2.length();k++) 
 { 
 S3=S1.substring(k,k+S2.length()); 
 if (S3.equals(S2)) 
 20
 dem++; 
 } 
 System.out.print(dem); 
 } 
} 
Kết quả 
2 
Process completed. 
Bài 09 
/** 
 *Cho xau S va 2 chi so i, j. Hay doi cho 2 vi tri i, j trong S. 
 */ 
public class String09 { 
 public static void main(String[] args) { 
 String S= new String ("0123456789"); 
 String S1,S2 = new String(); 
 int i=3,j=8; 
 int N=S.length(); 
 for (int k=0;k<N;k++) 
 { 
 S1= S.substring(k,k+1); 
 if ((k!=i) && (k!=j)) 
 S2=S2+S1; 
 if (k==i) 
 S2=S2+S.substring(j,j+1); 
 if (k==j) 
 S2=S2+S.substring(i,i+1); 
 } 
 System.out.print(S2); 
 } 
} 
Kết quả 
0128456739 
Process completed. 
Bài 10 
/** 
 *Cho mang xau ky tu S1, S2… Sn. Hay tim va in ra phan tu xau co do dai lon nhat. 
 */ 
public class String10 { 
 public static void main(String[] args) { 
 String [] S = {"Hehe", "hahaha", "hihihihi"}; 
 int max=0; 
 for (int k=0;k<3;k++) 
 { 
 if (max<S[k].length()) 
 max=S[k].length(); 
 } 
 for (int k=0;k<3;k++) 
 { 
 if (S[k].length()==max) 
 21
 System.out.print(S[k]); 
 } 
 } 
} 
Kết quả 
hihihihi 
Process completed. 
Bài 11 
/** 
 *Cho danh sach ho ten day du hoc sinh. Hay dem xem co bao nhieu ban ten “An”. 
 */ 
public class String11 { 
 public static void main(String[] args) { 
 String [] ds = {" Nguyen Van An ","Nguyen Thi Binh ", "Le Van Lan ","Le An "}; 
 int dem=0; 
 for(int k=0;k<4;k++) 
 { 
 ds[k]=ds[k].trim(); 
 int N=ds[k].length(); 
 String S1=ds[k].substring(N-2); 
 if (S1.equals("An")) 
 dem++; 
 } 
 System.out.print(dem); 
 } 
} 
Kết quả 
2 
Process completed. 
Bài 12 
/** 
 *Cho danh sach ho ten day du hoc sinh. Hay dem xem co bao nhieu ban co phan dem la “Thi”. 
 */ 
public class String12 { 
 public static void main(String[] args) { 
 String [] dshs = {" Nguyen Thi Lan", "Nguyen Thi Binh ","Le Van Lan "}; 
 int dem=0; 
 for (int k=0;k<3;k++) 
 { 
 dshs[k]=dshs[k].trim(); 
 String S1= new String (); 
 int N= dshs[k].length(); 
 int i,j; 
 for (i=0;i<N;i++) 
 { 
 S1=dshs[k].substring(i,i+1); 
 if(S1.equals(" ")) 
 break; 
 22
 } 
 for (j=N-1;j>=0;j--) 
 { 
 S1=dshs[k].substring(j,j+1); 
 if(S1.equals(" ")) 
 break; 
 } 
 S1=dshs[k].substring(i+1,j); 
 if(S1.equals("Thi")) 
 dem++; 
 } 
 System.out.print(dem); 
 } 
} 
Kết quả 
2 
Process completed. 
Bài 13 
/** 
 *Cho danh sach ho ten day du hoc sinh. Hay dem xem co bao nhieu ban co ten bat dau bang chu “H”. 
 */ 
Cách 1 
public class String13 { 
 public static void main(String[] args) { 
 String [] ds={"Nguyen Thi Binh "," Tran Binh Minh "," Nguyen Thi Hoa "}; 
 int i; 
 int dem=0; 
 String S= new String(); 
 for(int k=0;k<3;k++) 
 { 
 ds[k]=ds[k].trim(); 
 int N=ds[k].length(); 
 for (i=N-1;i>=0;i--) 
 { 
 S=ds[k].substring(i,i+1); 
 if (S.equals(" ")) 
 break; 
 } 
 S=ds[k].substring(i+1,i+2); 
 if(S.equals("H")) 
 dem++; 
 } 
 System.out.print(dem); 
 } 
} 
Cách 2 
public class String13_2 { 
 public static void main(String[] args) { 
 String [] ds={"Nguyen Thi Binh "," Tran Binh Minh "," Nguyen Thi Hoa "}; 
 23
 int i; 
 int dem=0; 
 for(int k=0;k<3;k++) 
 { 
 ds[k]=ds[k].trim(); 
 int N=ds[k].length(); 
 for (i=N-2;i>=0;i--) 
 { 
 String S=ds[k].substring(i,i+2); 
 if (S.endsWith("H") && S.startsWith(" ")) 
 { 
 dem++; 
 break; 
 } 
 } 
 } 
 System.out.print(dem); 
 } 
} 
Kết quả 
1 
Process completed. 
Bài 15 
/** 
 *Day xau ki tu S1,S2... duoc cho theo quy tac sau 
 *S1="1111100000", Sk thu duoc tu Sk-1 bang cach thay doi cho lan luot cac vi tri 
 *1-2;2-3;3-4;4-5;5-6;6-7;7-8;8-9;9-10 
 *Cho truoc so tu nhien N , hay in ra xau Sn 
 */ 
public class String15 { 
 public static void main(String[] args) { 
 String S = new String ("0123456789"); 
 String S1 = new String (); 
 int N=2; 
 int k,dem=0; 
 int L=S.length(); 
 while (dem<N) 
 { 
 for (k=1;k<L;k++) 
 S1=S1+S.charAt(k); 
 S1=S1+S.charAt(0); 
 S=S1; 
 S1=""; 
 dem++; 
 } 
 System.out.print(S); 
 } 
} 
Kết quả 
2345678901 
 24
Process completed. 
Bài 16 
/** 
 *Cho truoc 2 xau ki tu S1,S2.hay chen xau S1 vao giua xau S2 va in ra ket qua 
 */ 
public class String16 { 
 public static void main(String[] args) { 
 String S2= new String ("123456789"); 
 String S1=new String("abcdefg"); 
 String S3=new String(); 
 int N=S2.length(); 
 int k; 
 if (N%2==0) k=N/2; 
 else 
 k=(N+1)/2; 
 S3=S2.substring(0,k); 
 S3=S3+S1; 
 S3=S3+S2.substring(k); 
 System.out.print(S3); 
 } 
} 
Kết quả 
12345abcdefg6789 
Process completed. 
Bài 17 
/** 
 *Cho truoc 2 xau S1,S2. Hay xet xem xau S1 o phai la xau con cua S2 neu xoa bo vai ky tu cua xau S2 
duoc xau S1 
 */ 
public class String17 { 
 public static void main(String[] args) { 
 String S1= new String ("abcdefg"); 
 String S2= new String ("abc3456defg789"); 
 int x=0,j=0,dem=0,k; 
 int N2=S2.length(); 
 int N1=S1.length(); 
 while (j<N1) 
 { 
 k=x; 
 while (k< N2) 
 { 
 if (S2.charAt(k)==S1.charAt(j)) 
 { 
 dem++; 
 x=k; 
 break; 
 } 
 else k++; 
 25
 } 
 j++; 
 } 
 if (dem==N1) 
 System.out.print("S1 la chuoi con cua S2 "); 
 else 
 System.out.print("S1 khong phai la chuoi con cua S2"); 
 } 
} 
Kết quả 
S1 la chuoi con cua S2 
Process completed. 

File đính kèm:

  • pdfBài tập Java.pdf
Tài liệu liên quan