Academia.eduAcademia.edu
Bài tập Java I/ Các bài tập Java cơ bản Bài 05 /* * I n ra m an hinh t at ca cac hop so < 100 **/ public class bai05 { public st at ic void m ain( St ring[ ] args) { int k,n,dem ; for( k= 1; k< = 100; k+ + ) { dem = 0; / / dat j a t ri cua bien dem = 0 ung voi m oi j a t ri cua k for( n= 2; n< = k; n+ + ) { if( k% n= = 0) / / neu so du khi chia k cho n = 0 { dem + + ; / / t hi bien dem duoc cong t hem 1 don vi } } if( dem > 1) { Syst em .out .print ln( " hop so la" + k) ; } } } } Bài 06 /* * I n ra m an hinh 15 so nguyen t o dau t ien **/ public class bai06 { public st at ic void m ain( St ring[ ] args) { int k= 0,n,dem ,j = 0; / / khai bao cac bien, dat j a t ri ban dau cho cac bien can t hiet while ( j < 15) / / t rong khi so nguyen t o da in < 15 { k+ + ; / / cong cho k 1 don vi dem = 0; / / reset gia t ri cua bien dem = 0 ung voi m oi j a t ri cua k for ( n= 2; n< = k; n+ + ) { if ( k% n= = 0) { dem + + ; } } if ( dem = = 1) { Syst em .out .print ( k+ " " ) ; j + + ; / / so lan in duoc cong t hem 1 1 } } }} Kết quả 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 Process com plet ed. Bài 07 /* * I n ra m an hinh t at ca cac so nguyen t o t u 1000 den 2000 **/ public class bai7 { public st at ic void m ain( St ring[ ] args) { int k,n,dem ; for ( k= 1000; k< = 2000; k+ + ) { dem = 0; for ( n= 2; n< = k; n+ + ) { if( k% n= = 0) dem + + ; } if( dem = = 1) Syst em .out .print ln( k) ; } } } Bài 08 /* * I n ra m an hinh cac so < 100 va chia het cho 3,7 */ public class bai08 { public st at ic void m ain( St ring[ ] args) { int k,n; for ( k= 1; k< 100; k+ + ) { if ( ( k% 3= = 0) && ( k% 7= = 0) ) Syst em .out .print ( k+ " " ) ; } } } Kết quả 21 42 63 84 Process com plet ed. Bài 09 /* * I n ra m an hinh cac so nam giua 1000 va 200 dong t hoi chia het cho 3,5,7 */ public class bai09 { 2 public st at ic void m ain( St ring[ ] args) { int k; for ( k= 1000; k< = 2000; k+ + ) { if( ( k% 3= = 0) &( k% 5= = 0) &( k% 7= = 0) ) Syst em .out .print ( k+ " " ) ; } } } Kết quả 1050 1155 1260 1365 1470 1575 1680 1785 1890 1995 Process com plet ed. Bài 10 /* * I n ra m an hinh 5 so hoan hao dau t ien( so hoan hao la so co t ong bang cac uoc so cua m inh ke ca 1) **/ public class bai10 { public st at ic void m ain( St ring[ ] args) { int k= 0,j = 0,n,t ong; while( j < 5) { k+ + ; t ong= 1; / / vi 1 luon la uoc cua cac so hang for( n= 2; n< k; n+ + ) / / j a t ri ban dau cua n = 2 t hay vi dat bang 1 vi da t inh 1 la uoc o phia t ren { if ( k% n= = 0) t ong+ = n; } if ( k= = t ong) { Syst em .out .print ( k+ " " ) ; j+ + ; } } } } Kết quả 1 6 28 496 8128 Process com plet ed. Bài 11 / * Trong cac so t u nhien < = 100 hay dem xem co bao nhiu so * - Chia het cho 5 * - Chia 5 du 1 * - Chia 5 du 2 * - Chia 5 du 3 */ public class bai11 { public st at ic void m ain( St ring[ ] args) { int dem 0,dem 1,dem 2,dem 3; int d,k; 3 dem 0= 0; dem 1= 0; dem 2= 0; dem 3= 0; for ( k= 5; k< = 100; k + + ) { swit ch( d= k% 5) { case 0 : dem 0+ + ; break; case 1: dem 1+ + ; break; case 2: dem 2+ + ; break; case 3: dem 3+ + ; break; } } Syst em .out .print ln( " so cac so chia het cho 5 la: " + dem 0) ; Syst em .out .print ln( " So cac so chia 5 du 1 la: " + dem 1) ; Syst em .out .print ln( " So cac so chia 5 du 2 la: " + dem 2) ; Syst em .out .print ln( " So cac so chia 5 du 3 la: " + dem 3) ; } } Kết quả so cac so chia het cho 5 la: 20 So cac so chia 5 du 1 la: 19 So cac so chia 5 du 2 la: 19 So cac so chia 5 du 3 la: 19 Process com plet ed. Bài 12 /* * * Cho so t u nhien N bat ki( da gan t ruoc do) ,t im va in ra uoc so nguyen t o nho nhat cua N */ public class bai12 { public st at ic void m ain( St ring[ ] args) { int N= 40; int k; for ( k= 2; k< = N; k+ + ) { int dem = 0; for( int x= 2; x< = k; x+ + ) if( k% x= = 0) dem + + ; if ( ( N% k= = 0) &&( dem = = 1) ) { 4 Syst em .out .print ( " uoc so nguyen t o nho nhat la: "+ k ) ; break; } } } } Kết quả uoc so nguyen t o nho nhat la: 2 Process com plet ed. Bài 13 / * Cho so t u nhien N > 1 bat ki ( da gan t ruoc do) * I n ra khai t rien t hanh t ich cac so nguyen t o t inh t u nho den lon * Vd 9- - > 3.3 * 12- - > 2.2.3 */ Cách 1 dùng for public class bai13 { public st at ic void m ain( St ring[ ] args) { int N= 12; int k; for ( k= 2; k< = N; k+ + ) { if ( N% k= = 0) { Syst em .out .print ( k+ " " ) ; N= N/ k; k- - ; } } } } Cách 2: dùng while public class bai13 { public st at ic void m ain( St ring[ ] args) { int N= 12,k= 2; while( k< = N) { for( k= 2; k< = N; k+ + ) { if ( N% k= = 0) { Syst em .out .print ( k+ " " ) ; N= N/ k; break; } } } } } Kết quả 2 2 3 5 Process com plet ed. Bài 14 /* * Cho t ruoc so t u nhien N bat ki ( da gan t ruoc do) * I n ra m an hinh t at ca cac uoc so nguyen t o khac nhau cua N */ public class bai14 { public st at ic void m ain( St ring[ ] args) { int k,n,dem ,N= 1027; for ( k= 1; k< = N; k+ + ) { dem = 0; for ( n= 2; n< = k; n+ + ) { if ( k% n= = 0) { dem + + ; } } if ( dem = = 1 & N% k= = 0) { Syst em .out .print ( k+ ” “ ) ; } } } } Kết quả 13 79 Process com plet ed. II/ Bài tập về hàm và thủ tục (Method & function) Bài 03 /* * * Cho so t hu nhien N bat ki * Tinh t ong S= 1+ 1/ ( 1+ 2) + 1/ ( 1+ 2+ 3) + ... + 1/ ( 1+ 2+ 3+ ...+ N) */ public class ham 03 { public st at ic void m ain( St ring[ ] args) { int N= 2; float S= 0; int k; for ( k= 1; k< = N; k+ + ) { S+ = 1/ ( sum ( k) ) ; } Syst em .out .print ( " ket qua la: " + S) ; } public st at ic float sum ( int k) { int t ong= 0; 6 int x; for ( x= 1; x< = k; x+ + ) { t ong+ = x; } ret urn t ong; } } Kết quả ket qua la: 1.3333334 Process com plet ed. Bài 04 /* * * Cho so t u nhien N bat ki,t inh t ong * S = 1 + 1/ 2! + 1/ 3! + ...+ 1/ N! */ public class ham 04 { public st at ic void m ain( St ring[ ] args) { int k,N= 3; float S= 0; for ( k= 1; k< = N; k+ + ) { S+ = 1/ sum ( k) ; } Syst em .out .print ( " ket qua la: " + S) ; } public st at ic float sum ( int k) { int t ich= 1; for ( int x= 1; x< = k; x+ + ) { t ich= t ich* x; } ret urn t ich; } } Kết quả ket qua la: 1.6666666 Process com plet ed. Bài 05 /* * Cho so t u nhien N bat ki,t inh t ong * S= 1 + 1/ ( 1+ 2! ) + 1/ ( 1+ 2! + 3! ) + ...+ 1/ ( 1+ 2! + 3! + ...+ N! ) */ public class ham 05 { public st at ic void m ain( St ring[ ] args) { int N= 3,k; float S= 0; 7 for ( k= 1; k< = N; k+ + ) { S+ = 1/ sum ( k) ; } Syst em .out .print ( " ket qua la " + S) ; } public st at ic float sum ( int k) { float t ong= 0; for ( int x= 1; x< = k; x+ + ) { t ong+ = t ich( x) ; } ret urn t ong; } public st at ic float t ich( int x) { int t = 1; for ( int j = 1; j < = x; j + + ) { t= t* j; } ret urn t ; } } Kết quả ket qua la 1.4444445 Process com plet ed. Bài 06 /* * * Day Fibonaxi 1 2 3 ... F( k) = F( k- 1) + F( k- 2) .Tinh so Fibonaxi t hu N */ public class ham 06 { public st at ic void m ain( St ring[ ] args) { int a= 1,b= 2,c= 0; int N= 10,j = 3; while ( j < = N) { c= a+ b; a= b; b= c; j+ + ; } Syst em .out .print ( " so fibonaxy t hu 10 la: " + c) ; } } Kết quả so fibonaxy t hu 10 la: 89 Process com plet ed. 8 III/ Bài tập về mảng (Array) Bài 01 /* * * Cho 1 day so t u nhien,viet chuong t rinh sap xep day nay t heo t hu t u giam dan. */ public class m ang01 { public st at ic void m ain( St ring[ ] args) { int [ ] a = { 3,1,7,0,10} ; int N= 5,k,j ,t em p; for ( k= 0; k< N- 1; k+ + ) { for ( j = k+ 1; j < N; j + + ) { if ( a[ k] < a[ j ] ) { t em p= a[ j ] ; a[ j ] = a[ k] ; a[ k] = t em p; } } } for ( k= 0; k< N; k+ + ) Syst em .out .print ( a[ k] + " " ) ; } } Kết quả: 10 7 3 1 0 Process com plet ed. Bài 02 /* * * Cho 1 day so t u nhien, in ra m an hinh t at ca cac so nguyen t o cua day nay */ public class m ang02 { public st at ic void m ain( St ring[ ] args) { int [ ] a = { 3,1,7,0,10} ; int N= 5,k,x,dem ; for ( k= 0; k< N; k+ + ) { dem = 0; for ( x= 2; x< = a[ k] ; x+ + ) if ( a[ k] % x= = 0) dem + + ; if ( dem = = 1) Syst em .out .print ( a[ k] + " " ) ; } } } Kết quả 3 7 Process com plet ed. 9 Bài 03 /* * * Cho 1day cac so t u nhien, t im va in ra 1 gia t ri m in cua day nay va t at ca cac chi so ung voi gt m in nay */ public class m ang03 { public st at ic void m ain( St ring[ ] args) { int [ ] a = { 3,1,7,0,10} ; int N= 5,k,m in; m in= a[ 0] ; for ( k= 0; k< N; k+ + ) if ( m in > a[ k] ) m in= a[ k] ; Syst em .out .print ln( " gia t ri nho nhat cua day la: " + m in) ; Syst em .out .print ( " vi t ri cua so co gia t ri m in la: " ) ; for ( k= 0; k< N; k+ + ) if ( m in = = a[ k] ) Syst em .out .print ( k+ " " ) ; }} Kết quả gia t ri nho nhat cua day la: 0 vi t ri cua so co gia t ri m in la: 3 Bài 04 /* * * Cho 1day cac so t u nhien, t im va in ra 1 gia t ri m ax cua day nay va t at ca cac chi so ung voi gt m ax nay */ public class m ang04 { public st at ic void m ain( St ring[ ] args) { int [ ] a = { 3,1,7,0,10} ; int N= 5,k,m ax; m ax= a[ 0] ; for ( k= 0; k< N; k+ + ) if ( m ax < a[ k] ) m ax= a[ k] ; Syst em .out .print ln( " gia t ri lon nhat cua day la: " + m ax) ; Syst em .out .print ( " vi t ri cua so co gia t ri m ax la: " ) ; for ( k= 0; k< N; k+ + ) if( m ax= = a[ k] ) Syst em .out .print ( k+ " " ) ; } } Kết quả gia t ri lon nhat cua day la: 10 vi t ri cua so co gia t ri m ax la: 4 Process com plet ed. Bài 05 /* * * Cho 1 day so t u nhien,hay dem xem t rong day so t ren co bao nhieu so nguyen t o, co bao nhieu hop so */ public class m ang05 { public st at ic void m ain( St ring[ ] args) { 10 int int int int for { [ ] a = { 3,1,7,0,10} ; N= 5,k; nt = 0; hs= 0; ( k= 0; k< N; k+ + ) int dem = 0; for ( int x= 2; x< = a[ k] ; x+ + ) { if ( a[ k] % x= = 0) dem + + ; } if ( dem = = 1) nt + + ; else hs+ + ; } Syst em .out .print ln( " so cac so nguyen t o la: " + nt ) ; Syst em .out .print ln( " so cac hop so la: " + hs) ; } } Kết quả so cac so nguyen t o la: 2 so cac hop so la: 3 Bài 06 /* * * Cho 1 day so t u nhien,hay in ra t at ca cac so hang cua day t ren t hoa m an : * So nay la la uoc so t huc su cua 1 so hang khac t rong day t ren */ public class m ang06 { public st at ic void m ain( St ring[ ] args) { int [ ] a = { 3,1,7,14,10} ; int N= 5,k; for ( k= 0; k< N; k+ + ) { for ( int j = 0; j < N; j + + ) { if ( ( j = = k) | ( a[ k] = = 0) ) cont inue; if ( a[ j ] % a[ k] = = 0) { Syst em .out .print ( a[ k] + " " ) ; break; } } } } } Kết quả 1 7 Process com plet ed. 11 Bài 07 /* * * Cho 1 day so t u nhien,hayt im 1 so t u nhien nho nhat c khong bang bat cu so nao t rong day t ren */ public class m ang07 { public st at ic void m ain( St ring[ ] args) { int [ ] a = { 8,8,8,12,9} ; int N= 5,k,in= 0; for ( k= 0; k< N- 1; k+ + ) { for ( int j = k+ 1; j < N; j + + ) { int t em p; if ( a[ k] > a[ j ] ) { t em p= a[ j ] ; a[ j ] = a[ k] ; a[ k] = t em p; } } } for ( k= 0; k< N- 1; k+ + ) { if( a[ k] ! = a[ k+ 1] ) { if( k= = 0) { Syst em .out .print ln( a[ k] ) ; break; } else if ( a[ k- 1] ! = a[ k] ) { Syst em .out .print ( a[ k] ) ; break; } } } } } Kết quả 9 Process com plet ed. Bài 08 /* * * Cho 1 day so nguyen bat ki,hay xoa di t rong day nay cac so hang = 0 va in ra m an hinh cac so con lai cua day */ Cách 1 public class m ang08 { public st at ic void m ain( St ring[ ] args) { 12 int N= 8,i= 0,j = 0,dem = 0; int [ ] a = { 8,0,0,0,0,0,12,3} ; int [ ] b= new int [ N] ; while( i< N) { if ( a[ i] = = 0) i+ + ; else { b[ j ] = a[ i] ; i+ + ; j+ + ; dem + + ; } } for( j = 0; j < dem ; j + + ) Syst em .out .print ( b[ j ] + " " ) ; } } Cách 2 public class m ang08 { public st at ic void m ain( St ring[ ] args) { int [ ] a= { 8,0,0,0,12,3} ; int N= 6,k; int dem = 0; for ( k= 0; k< N; k+ + ) { if ( a[ k] = = 0) { dem + + ; for ( int j = k; j < ( N- dem ) ; j + + ) a[ j ] = a[ j + 1] ; k- - ; } } for ( k= 0; k< ( N- dem ) ; k+ + ) Syst em .out .print ( a[ k] + " " ) ; } } Kết quả 8 12 3 Process com plet ed. Bài 09 /* * * Cho 1 day o nguyen bat ki, cho t ruoc 1 so c. * Hay dem co bao nhieu so cua day t ren = c; > c; < c. */ public class m ang09 { public st at ic void m ain( St ring[ ] args) { int N= 5,k,c= 3; 13 int [ ] a= { 10,9,8,3,5} ; int dem 1= 0,dem 2= 0,dem 3= 0; for ( k= 0; k< N; k+ + ) { if ( a[ k] < c) dem 1+ + ; if ( a[ k] = = c) dem 2+ + ; if ( a[ k] > c) dem 3+ + ; } Syst em .out .print ln( " so cac so nho hon c la: " + dem 1) ; Syst em .out .print ln( " so cac so bang c la: " + dem 2) ; Syst em .out .print ( " so cac so lon hon c la: " + dem 3) ; } } Kết quả so cac so nho hon c la: 0 so cac so bang c la: 1 so cac so lon hon c la: 4 Process com plet ed. Bài 10 /* * * Cho 1 day so nguyen t o bat ki,hay t im ra 1 day so lien nhau dai nhat bao gom cac so bang nhau. * Hay in ra so luong va cac chi so dau t ien cua day con nay */ public class m ang10 { public st at ic void m ain( St ring[ ] args) { int [ ] a= { 8,4,9,12,8,8,8,8,8} ; int N= 9,k,dem m ax= 0,dem ,x= 0; for ( k= 0; k< N- 1; k+ + ) { if ( a[ k] = = a[ k+ 1] ) { dem = 0; for ( int j = k; j < N; j + + ) { if ( a[ k] = = a[ j ] ) dem + + ; if ( dem m ax< dem ) { x= k; dem m ax= dem ; } } } } Syst em .out .print ln( " so cac so t huoc day dai nhat la: " + dem m ax) ; Syst em .out .print ( " chi so cua day dai nhat la: " + x) ; } } Kết quả so cac so t huoc day dai nhat la: 5 chi so cua day dai nhat la: 4 14 Process com plet ed. Bài 11 /* * * Cho 1 day so nguyen bat ki. Hay t im 1 day con lien t uc don dieu t ang dai nhat cua day t ren */ public class m ang11 { public st at ic void m ain( St ring[ ] args) { int [ ] a= { 8,4,9,12,1,2,3,3,10,3} ; int N= 10,k,dem m ax= 0,dem ,x= 0; for ( k= 0; k< N- 1; k+ + ) { if ( a[ k] < = a[ k+ 1] ) { dem = 1; for ( int j = k; j < N- 1; j + + ) { if ( a[ j ] < = a[ j + 1] ) { dem + + ; if ( dem m ax< dem ) { dem m ax= dem ; x= k; } } else break; } } } Syst em .out .print ln( " so cac so t huoc day dai nhat la: " + dem m ax) ; Syst em .out .print ( " Chi so cua day dai nhat la: " + x) ; }} Kết quả so cac so t huoc day dai nhat la: 5 Chi so cua day dai nhat la: 4 Process com plet ed. Bài 12 /* * * Day so a[ ] duoc goi la day con cua b[ ] neu t u b[ ] xoa di 1 vai so se t hu duoc a[ ] * Cho t ruoc 2 day so nguyen a[ ] ; b[ ] .Hay kiem t ra xem a[ ] co la day con cua b[ ] hay ko */ public class m ang12 { public st at ic void m ain( St ring[ ] 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+ + ) 15 { if( a[ j ] = = b[ k] ) { in+ + ; x= k+ 1; break; } } } if ( in= = N) Syst em .out .print ( " day a la day con cua day b" ) ; else Syst em .out .print ( " day a ko la day con cua day b" ) ; }} Kết quả day a la day con cua day b Process com plet ed. IV/ Bài tập về xâu ( String) Bài 01 /* * * Cho t ruoc 1 xau ky t u la ho t en nguoi day du nhung khi nhap co t he t hua m ot so dau cach. * Hay xoa di cac dau cach t hua va in ra ho t en chinh xac. */ public class St ring01 { public st at ic void m ain( St ring[ ] args) { St ring S = new St ring ( " Nguyen Thi Binh " ) ; St ring S1,S2 = new St ring ( ) ; S= S.t rim ( ) ; for ( int k= 0; k< S.lengt h( ) ; k+ + ) { S1= S.subst ring( k,k+ 1) ; if ( S1.equals( " " ) ) { S1= S.subst ring( k+ 1,k+ 2) ; if ( S1.equals( " " ) ) cont inue; else S2= S2+ S.subst ring( k,k+ 1) ; } else S2= S2+ S1; } Syst em .out .print ( S2) ; } } Kết quả Pepsi Milo Ovant ine Process com plet ed. Bài 02 /* * * Cho t ruoc xau ky t u bat ky. Hay dem xem t rong xau co bao nhieu lan xuat hien xau con “ abc” . 16 */ public class St ring02 { public st at ic void m ain( St ring[ ] args) { St ring S = new St ring ( " abc def ab cdfg abcabc" ) ; St ring S1= new St ring ( ) ; int dem = 0; for ( int k= 0; k< S.lengt h( ) - 2; k+ + ) { S1= S.subst ring( k,k+ 3) ; if( S1.equals( " abc" ) ) dem + + ; } Syst em .out .print ( dem ) ; } } Kết quả 3 Process com plet ed. Bài 03 /* * * Cho t ruoc 1 xau ky t u la ho t en nguoi day du, hay t ach ra phan t en cua nguoi nay. */ public class St ring03 { public st at ic void m ain( St ring[ ] args) { St ring S = new St ring ( " Nguyen Van An " ) ; St ring S1 = new St ring ( ) ; S= S.t rim ( ) ; int k; for ( k= S.lengt h( ) - 1; k> = 0; k- - ) { S1= S.subst ring( k,k+ 1) ; if( S1.equals( " " ) ) break; } Syst em .out .print ( " Ten cua nguoi do la: " + S.subst ring( k+ 1) ) ; } } Kết quả Ten cua nguoi do la: An Process com plet ed. Bài 04 /* * * Cho t ruoc 1 xau ky t u la 1 ho t en nguoi day du, hay t ach ra phan ho cua nguoi nay */ public class St ring04 { public st at ic void m ain( St ring[ ] args) { St ring S = new St ring ( " Nguyen Van An " ) ; St ring S1 = new St ring ( ) ; S= S.t rim ( ) ; int k; 17 for ( k= 0; k< = S.lengt h( ) ; k+ + ) { S1= S.subst ring( k,k+ 1) ; if( S1.equals( " " ) ) break; } Syst em .out .print ( " Ho cua nguoi do la: " + S.subst ring( 0,k) ) ; } } Kết quả Ho cua nguoi do la: Nguyen Process com plet ed. Bài 05 /* * * Cho 1 xau ky t u bao gom t oan cac ky t u 0, 1. Hay bien doi xau nay t heo cach 0 - > 1, 1- > 0 va in ra ket qua. */ public class St ring05 { public st at ic void m ain( St ring[ ] args) { St ring S = new St ring ( " 010001110001100" ) ; St ring S1= new St ring ( ) ; St ring S2= new St ring ( ) ; for ( int k= 0; k< S.lengt h( ) ; k+ + ) { S1= S.subst ring( k,k+ 1) ; if ( S1.equals( " 0" ) ) S1= " 1" ; else S1= " 0" ; S2= S2+ S1; } Syst em .out .print ( S2) ; } } Kêt quả 101110001110011 Process com plet ed. Bài 06 /* * * Cho t ruoc xau ky t u S, in ra xau S1 nguoc lai xau S. */ public class St ring06 { public st at ic void m ain( St ring[ ] args) { St ring S,S1,S2= new St ring ( ) ; S= " 1234567890" ; for ( int k= S.lengt h( ) - 1; k> = 0; k- - ) { S1= S.subst ring( k,k+ 1) ; 18 S2= S2+ S1; } Syst em .out .print ( S2) ; } } Kết quả 0987654321 Process com plet ed. Bài 07 /* * * Cho t ruoc xau ky t u S. Hay bien doi S t heo quy t ac sau: chu so t hi bien t hanh “ $” con cac ky t u khac giu nguyen. **/ public class St ring07 { public st at ic void m ain( St ring[ ] args) { St ring S = new St ring ( " 12a3456b78 c 90" ) ; St ring [ ] X= { " 0" ," 1"," 2" ," 3" ," 4","5" ," 6" ," 7" ," 8" ," 9" } ; St ring S1= new St ring ( ) ; St ring S2= new St ring ( ) ; for ( int k= 0; k< S.lengt h( ) ; k+ + ) { S1= S.subst ring( k,k+ 1) ; for ( int j = 0; j < 10; j + + ) if ( S1.equals( X[ j ] ) ) { S1= " $" ; break; } S2= S2+ S1; } Syst em .out .print ( S2) ; } } Kết quả $$a$$$$b$$ c $$ Process com plet ed. Bài 08 /* * * Cho t ruoc 2 xau ky t u S1, S2. Hay dem xem xau S1 xuat hien t rong S2 t ai bao nhieu vi t ri. */ public class St ring08 { public st at ic void m ain( St ring[ ] args) { St ring S1= new St ring ( " abc def ghj abc ab c" ) ; St ring S2= new St ring ( " abc" ) ; St ring S3= new St ring( ) ; int dem = 0; for ( int k= 0; k< S1.lengt h( ) - S2.lengt h( ) ; k+ + ) { S3= S1.subst ring( k,k+ S2.lengt h( ) ) ; if ( S3.equals( S2) ) 19 dem + + ; } Syst em .out .print ( dem ) ; } } Kết quả 2 Process com plet ed. Bài 09 /* * * Cho xau S va 2 chi so i, j . Hay doi cho 2 vi t ri i, j t rong S. */ public class St ring09 { public st at ic void m ain( St ring[ ] args) { St ring S= new St ring ( " 0123456789" ) ; St ring S1,S2 = new St ring( ) ; int i= 3,j = 8; int N= S.lengt h( ) ; for ( int k= 0; k< N; k+ + ) { S1= S.subst ring( k,k+ 1) ; if ( ( k! = i) && ( k! = j ) ) S2= S2+ S1; if ( k= = i) S2= S2+ S.subst ring( j ,j + 1) ; if ( k= = j ) S2= S2+ S.subst ring( i,i+ 1) ; } Syst em .out .print ( S2) ; } } Kết quả 0128456739 Process com plet ed. Bài 10 /* * * Cho m ang xau ky t u S1, S2… Sn. Hay t im va in ra phan t u xau co do dai lon nhat . */ public class St ring10 { public st at ic void m ain( St ring[ ] args) { St ring [ ] S = { "Hehe" , " hahaha", " hihihihi" } ; int m ax= 0; for ( int k= 0; k< 3; k+ + ) { if ( m ax< S[ k] .lengt h( ) ) m ax= S[ k] .lengt h( ) ; } for ( int k= 0; k< 3; k+ + ) { if ( S[ k] .lengt h( ) = = m ax) 20 Syst em .out .print ( S[ k] ) ; } } } Kết quả hihihihi Process com plet ed. Bài 11 /* * * Cho danh sach ho t en day du hoc sinh. Hay dem xem co bao nhieu ban t en “ An” . */ public class St ring11 { public st at ic void m ain( St ring[ ] args) { St ring [ ] 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] .t rim ( ) ; int N= ds[ k] .lengt h( ) ; St ring S1= ds[ k] .subst ring( N- 2) ; if ( S1.equals( " An" ) ) dem + + ; } Syst em .out .print ( dem ) ; } } Kết quả 2 Process com plet ed. Bài 12 /* * * Cho danh sach ho t en day du hoc sinh. Hay dem xem co bao nhieu ban co phan dem la “ Thi” . */ public class St ring12 { public st at ic void m ain( St ring[ ] args) { St ring [ ] dshs = { " Nguyen Thi Lan" , "Nguyen Thi Binh "," Le Van Lan " } ; int dem = 0; for ( int k= 0; k< 3; k+ + ) { dshs[ k] = dshs[ k] .t rim ( ) ; St ring S1= new St ring ( ) ; int N= dshs[ k] .lengt h( ) ; int i,j ; for ( i= 0; i< N; i+ + ) { S1= dshs[ k] .subst ring( i,i+ 1) ; if( S1.equals( " " ) ) break; 21 } for ( j = N- 1; j > = 0; j - - ) { S1= dshs[ k] .subst ring( j ,j + 1) ; if( S1.equals( " " ) ) break; } S1= dshs[ k] .subst ring( i+ 1,j ) ; if( S1.equals( " Thi" ) ) dem + + ; } Syst em .out .print ( dem ) ; } } Kết quả 2 Process com plet ed. Bài 13 /* * * Cho danh sach ho t en day du hoc sinh. Hay dem xem co bao nhieu ban co t en bat dau bang chu “ H” . */ Cách 1 public class St ring13 { public st at ic void m ain( St ring[ ] args) { St ring [ ] ds= { " Nguy en Thi Binh " ," Tran Binh Minh " ," Nguyen Thi Hoa " } ; int i; int dem = 0; St ring S= new St ring( ) ; for( int k= 0; k< 3; k+ + ) { ds[ k] = ds[ k] .t rim ( ) ; int N= ds[ k] .lengt h( ) ; for ( i= N- 1; i> = 0; i- - ) { S= ds[ k] .subst ring( i,i+ 1) ; if ( S.equals( " " ) ) break; } S= ds[ k] .subst ring( i+ 1,i+ 2) ; if( S.equals( "H" ) ) dem + + ; } Syst em .out .print ( dem ) ; } } Cách 2 public class St ring13_2 { public st at ic void m ain( St ring[ ] args) { St ring [ ] ds= { " Nguy en Thi Binh " ," Tran Binh Minh " ," Nguyen Thi Hoa " } ; 22 int i; int dem = 0; for( int k= 0; k< 3; k+ + ) { ds[ k] = ds[ k] .t rim ( ) ; int N= ds[ k] .lengt h( ) ; for ( i= N- 2; i> = 0; i- - ) { St ring S= ds[ k] .subst ring( i,i+ 2) ; if ( S.endsWit h( " H" ) && S.st art sWit h( " " ) ) { dem + + ; break; } } } Syst em .out .print ( dem ) ; } } Kết quả 1 Process com plet ed. Bài 15 /* * * Day xau ki t u S1,S2... duoc cho t heo quy t ac sau * S1= " 1111100000" , Sk t hu duoc t u Sk- 1 bang cach t hay doi cho lan luot cac vi t ri * 1- 2; 2- 3; 3- 4; 4- 5; 5- 6; 6- 7; 7- 8; 8- 9; 9- 10 * Cho t ruoc so t u nhien N , hay in ra xau Sn */ public class St ring15 { public st at ic void m ain( St ring[ ] args) { St ring S = new St ring ( " 0123456789" ) ; St ring S1 = new St ring ( ) ; int N= 2; int k,dem = 0; int L= S.lengt h( ) ; while ( dem < N) { for ( k= 1; k< L; k+ + ) S1= S1+ S.charAt ( k) ; S1= S1+ S.charAt ( 0) ; S= S1; S1= " " ; dem + + ; } Syst em .out .print ( S) ; } } Kết quả 2345678901 23 Process com plet ed. Bài 16 /* * * Cho t ruoc 2 xau ki t u S1,S2.hay chen xau S1 vao giua xau S2 va in ra ket qua */ public class St ring16 { public st at ic void m ain( St ring[ ] args) { St ring S2= new St ring ( " 123456789" ) ; St ring S1= new St ring( " abcdefg" ) ; St ring S3= new St ring( ) ; int N= S2.lengt h( ) ; int k; if ( N% 2= = 0) k= N/ 2; else k= ( N+ 1) / 2; S3= S2.subst ring( 0,k) ; S3= S3+ S1; S3= S3+ S2.subst ring( k) ; Syst em .out .print ( S3) ; } } Kết quả 12345abcdefg6789 Process com plet ed. Bài 17 /* * * Cho t ruoc 2 xau S1,S2. Hay xet xem xau S1 o phai la xau con cua S2 neu xoa bo vai ky t u cua xau S2 duoc xau S1 */ public class St ring17 { public st at ic void m ain( St ring[ ] args) { St ring S1= new St ring ( " abcdefg" ) ; St ring S2= new St ring ( " abc3456defg789" ) ; int x= 0,j = 0,dem = 0,k; int N2= S2.lengt h( ) ; int N1= S1.lengt h( ) ; while ( j < N1) { k= x; while ( k< N2) { if ( S2.charAt ( k) = = S1.charAt ( j ) ) { dem + + ; x= k; break; } else k+ + ; 24 } j+ + ; } if ( dem = = N1) Syst em .out .print ( " S1 la chuoi con cua S2 " ) ; else Syst em .out .print ( " S1 khong phai la chuoi con cua S2" ) ; } } Kết quả S1 la chuoi con cua S2 Process com plet ed. 25