1. 1
B¨¤i t?p Java
I/ C¨¢c b¨¤i t?p Java c? b?n
B¨¤i 05
/*
*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);
}
}
}
}
B¨¤i 06
/*
*In ra man hinh 15 so nguyen to dau tien
**/
public class bai06 {
public static void main(String[] args)
{
int k=0,n,dem,j=0;// khai bao cac bien, dat ja tri ban dau cho cac bien can thiet
while (j<15) // trong khi so nguyen to da in < 15
{
k++; // cong cho k 1 don vi
dem=0; // reset gia tri cua bien dem =0 ung voi moi ja tri cua k
for (n=2;n<=k;n++)
{
if (k%n==0)
{
dem++;
}
}
if (dem==1)
{
System.out.print(k+" ");
j++;// so lan in duoc cong them 1
2. 2
}
}
}}
K?t qu?
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Process completed.
B¨¤i 07
/*
*In ra man hinh tat ca cac so nguyen to tu 1000 den 2000
**/
public class bai7 {
public static void main(String[] 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)
System.out.println(k);
}
}
}
B¨¤i 08
/*
*In ra man hinh cac so <100 va chia het cho 3,7
*/
public class bai08 {
public static void main(String[] args) {
int k,n;
for (k=1;k<100;k++)
{
if ((k%3==0) && (k%7==0))
System.out.print(k+" ");
}
}
}
K?t qu?
21 42 63 84
Process completed.
B¨¤i 09
/*
*In ra man hinh cac so nam giua 1000 va 200 dong thoi chia het cho 3,5,7
*/
public class bai09 {
3. 3
public static void main(String[] args) {
int k;
for (k=1000;k<=2000;k++)
{
if((k%3==0)&(k%5==0)&(k%7==0))
System.out.print(k+" ");
}
}
}
K?t qu?
1050 1155 1260 1365 1470 1575 1680 1785 1890 1995
Process completed.
B¨¤i 10
/*
*In ra man hinh 5 so hoan hao dau tien( so hoan hao la so co tong bang cac uoc so cua minh ke ca 1)
**/
public class bai10 {
public static void main(String[] args) {
int k=0,j=0,n,tong;
while(j<5)
{
k++;
tong=1;// vi 1 luon la uoc cua cac so hang
for(n=2;n<k;n++) // ja tri ban dau cua n = 2 thay vi dat bang 1 vi da tinh 1 la uoc o phia tren
{
if (k%n==0)
tong+=n;
}
if (k==tong)
{
System.out.print(k+" ");
j++;
}
}
}
}
K?t qu?
1 6 28 496 8128
Process completed.
B¨¤i 11
/*Trong cac so tu 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 static void main(String[] args) {
int dem0,dem1,dem2,dem3;
int d,k;
4. 4
dem0=0;dem1=0;dem2=0;dem3=0;
for (k=5;k<=100;k++)
{
switch(d=k%5)
{
case 0 :
dem0++;
break;
case 1:
dem1++;
break;
case 2:
dem2++;
break;
case 3:
dem3++;
break;
}
}
System.out.println("so cac so chia het cho 5 la:" +dem0);
System.out.println("So cac so chia 5 du 1 la:"+dem1);
System.out.println("So cac so chia 5 du 2 la:"+dem2);
System.out.println("So cac so chia 5 du 3 la:"+dem3);
}
}
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 completed.
B¨¤i 12
/**
* Cho so tu nhien N bat ki( da gan truoc do),tim va in ra uoc so nguyen to nho nhat cua N
*/
public class bai12 {
public static void main(String[] 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))
{
5. 5
System.out.print("uoc so nguyen to nho nhat la:"+k );
break;
}
}
}
}
K?t qu?
uoc so nguyen to nho nhat la:2
Process completed.
B¨¤i 13
/*Cho so tu nhien N > 1 bat ki ( da gan truoc do)
*In ra khai trien thanh tich cac so nguyen to tinh tu nho den lon
*Vd 9--> 3.3
*12--> 2.2.3
*/
C¨¢ch 1 d¨´ng for
public class bai13 {
public static void main(String[] args) {
int N=12;
int k;
for (k=2;k<=N;k++)
{
if (N%k==0)
{
System.out.print(k+" ");
N=N/k;
k--;
}
}
}
}
C¨¢ch 2: d¨´ng while
public class bai13 {
public static void main(String[] args) {
int N=12,k=2;
while(k<=N)
{
for(k=2;k<=N;k++)
{
if (N%k==0)
{
System.out.print(k+" ");
N=N/k;
break;
}
}
}
}
}
K?t qu?
2 2 3
6. 6
Process completed.
B¨¤i 14
/*
*Cho truoc so tu nhien N bat ki (da gan truoc do)
*In ra man hinh tat ca cac uoc so nguyen to khac nhau cua N
*/
public class bai14 {
public static void main(String[] 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)
{
System.out.print(k+¡± ¡°);
}
}
}
}
K?t qu?
13 79
Process completed.
II/ B¨¤i t?p v? h¨¤m v¨¤ th? t?c (Method & function)
B¨¤i 03
/**
*Cho so thu nhien N bat ki
*Tinh tong S= 1+ 1/(1+2)+ 1/(1+2+3) + ... + 1/(1+2+3+...+N)
*/
public class ham03 {
public static void main(String[] args) {
int N=2;
float S=0;
int k;
for (k=1;k<=N;k++)
{
S+=1/(sum(k));
}
System.out.print("ket qua la:"+S);
}
public static float sum(int k)
{
int tong=0;
7. 7
int x;
for (x=1;x<=k;x++)
{
tong+=x;
}
return tong;
}
}
K?t qu?
ket qua la:1.3333334
Process completed.
B¨¤i 04
/**
*Cho so tu nhien N bat ki,tinh tong
*S = 1 + 1/2! + 1/3! +...+ 1/N!
*/
public class ham04 {
public static void main(String[ ] args) {
int k,N=3;
float S=0;
for (k=1;k<=N;k++)
{
S+=1/sum(k);
}
System.out.print("ket qua la:"+S);
}
public static float sum(int k)
{
int tich=1;
for (int x=1;x<=k;x++)
{
tich=tich*x;
}
return tich;
}
}
K?t qu?
ket qua la:1.6666666
Process completed.
B¨¤i 05
/*
*Cho so tu nhien N bat ki,tinh tong
*S= 1 + 1/(1+2!) + 1/(1+2!+3!) +...+ 1/(1+2!+3!+...+N!)
*/
public class ham05 {
public static void main(String[ ] args) {
int N=3,k;
float S=0;
8. 8
for (k=1;k<=N;k++)
{
S+=1/sum(k);
}
System.out.print("ket qua la "+S);
}
public static float sum(int k)
{
float tong=0;
for (int x=1;x<=k;x++)
{
tong+=tich(x);
}
return tong;
}
public static float tich(int x)
{
int t=1;
for ( int j=1;j<=x;j++)
{
t=t*j;
}
return t;
}
}
K?t qu?
ket qua la 1.4444445
Process completed.
B¨¤i 06
/**
*Day Fibonaxi 1 2 3 ... F(k)=F(k-1)+ F(k-2).Tinh so Fibonaxi thu N
*/
public class ham06 {
public static void main(String[] args) {
int a=1,b=2,c=0;
int N=10,j=3;
while ( j <=N)
{
c=a+b;
a=b;
b=c;
j++;
}
System.out.print("so fibonaxy thu 10 la:" +c);
}
}
K?t qu?
so fibonaxy thu 10 la:89
Process completed.
9. 9
III/ B¨¤i t?p v? m?ng (Array)
B¨¤i 01
/**
*Cho 1 day so tu nhien,viet chuong trinh sap xep day nay theo thu tu giam dan.
*/
public class mang01 {
public static void main(String[] args) {
int [] a ={3,1,7,0,10};
int N=5,k,j,temp;
for (k=0;k<N-1;k++)
{
for (j=k+1;j<N;j++)
{
if (a[k]<a[j])
{
temp=a[j];
a[j]=a[k];
a[k]=temp;
}
}
}
for (k=0;k<N;k++)
System.out.print(a[k]+" ");
}
}
K?t qu?:
10 7 3 1 0
Process completed.
B¨¤i 02
/**
* Cho 1 day so tu nhien, in ra man hinh tat ca cac so nguyen to cua day nay
*/
public class mang02 {
public static void main(String[] 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)
System.out.print(a[k]+" ");
}
}
}
K?t qu?
3 7
Process completed.
10. 10
B¨¤i 03
/**
*Cho 1day cac so tu nhien, tim va in ra 1 gia tri min cua day nay va tat ca cac chi so ung voi gt min nay
*/
public class mang03 {
public static void main(String[] args) {
int [] a ={3,1,7,0,10};
int N=5,k,min;
min=a[0];
for (k=0;k<N;k++)
if (min > a[k])
min=a[k];
System.out.println("gia tri nho nhat cua day la:" +min);
System.out.print("vi tri cua so co gia tri min la:");
for (k=0;k<N;k++)
if (min == a[k])
System.out.print(k+" ");
}}
K?t qu?
gia tri nho nhat cua day la:0
vi tri cua so co gia tri min la:3
B¨¤i 04
/**
*Cho 1day cac so tu nhien, tim va in ra 1 gia tri max cua day nay va tat ca cac chi so ung voi gt max nay
*/
public class mang04 {
public static void main(String[] args) {
int [] a ={3,1,7,0,10};
int N=5,k,max;
max=a[0];
for (k=0;k<N;k++)
if (max < a[k])
max=a[k];
System.out.println("gia tri lon nhat cua day la:"+max);
System.out.print("vi tri cua so co gia tri max la:");
for (k=0;k<N;k++)
if(max==a[k])
System.out.print(k+" ");
}
}
K?t qu?
gia tri lon nhat cua day la:10
vi tri cua so co gia tri max la:4
Process completed.
B¨¤i 05
/**
*Cho 1 day so tu nhien,hay dem xem trong day so tren co bao nhieu so nguyen to, co bao nhieu hop so
*/
public class mang05 {
public static void main(String[] args) {
11. 11
int [] a ={3,1,7,0,10};
int N=5,k;
int nt=0;
int hs=0;
for (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++;
}
System.out.println("so cac so nguyen to la:"+nt);
System.out.println("so cac hop so la:"+hs);
}
}
K?t qu?
so cac so nguyen to la:2
so cac hop so la:3
B¨¤i 06
/**
*Cho 1 day so tu nhien,hay in ra tat ca cac so hang cua day tren thoa man :
*So nay la la uoc so thuc su cua 1 so hang khac trong day tren
*/
public class mang06 {
public static void main(String[] 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))
continue;
if (a[j]%a[k]==0)
{
System.out.print(a[k]+" ");
break;
}
}
}
}
}
K?t qu?
1 7
Process completed.
12. 12
B¨¤i 07
/**
*Cho 1 day so tu nhien,haytim 1 so tu nhien nho nhat c khong bang bat cu so nao trong day tren
*/
public class mang07 {
public static void main(String[] 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 temp;
if (a[k]>a[j])
{
temp=a[j];
a[j]=a[k];
a[k]=temp;
}
}
}
for (k=0;k<N-1;k++)
{
if(a[k]!=a[k+1])
{
if(k==0)
{
System.out.println(a[k]);
break;
}
else
if (a[k-1]!=a[k])
{
System.out.print(a[k]);
break;
}
}
}
}
}
K?t qu?
9
Process completed.
B¨¤i 08
/**
* Cho 1 day so nguyen bat ki,hay xoa di trong day nay cac so hang = 0 va in ra man hinh cac so con lai
cua day
*/
C¨¢ch 1
public class mang08 {
public static void main(String[] args) {
13. 13
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++)
System.out.print(b[j]+" ");
}
}
C¨¢ch 2
public class mang08 {
public static void main(String[] 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++)
System.out.print(a[k]+" ");
}
}
K?t qu?
8 12 3
Process completed.
B¨¤i 09
/**
*Cho 1 day o nguyen bat ki, cho truoc 1 so c.
*Hay dem co bao nhieu so cua day tren =c; >c; <c.
*/
public class mang09 {
public static void main(String[] args) {
int N=5,k,c=3;
14. 14
int [] a={10,9,8,3,5};
int dem1=0,dem2=0,dem3=0;
for (k=0;k<N;k++)
{
if (a[k]<c) dem1++;
if (a[k]==c) dem2++;
if (a[k]>c) dem3++;
}
System.out.println("so cac so nho hon c la:"+dem1);
System.out.println("so cac so bang c la:"+dem2);
System.out.print("so cac so lon hon c la:"+dem3);
}
}
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 completed.
B¨¤i 10
/**
*Cho 1 day so nguyen to bat ki,hay tim ra 1 day so lien nhau dai nhat bao gom cac so bang nhau.
*Hay in ra so luong va cac chi so dau tien cua day con nay
*/
public class mang10 {
public static void main(String[] args) {
int [] a={8,4,9,12,8,8,8,8,8};
int N=9,k,demmax=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 (demmax<dem)
{
x=k;
demmax=dem;
}
}
}
}
System.out.println("so cac so thuoc day dai nhat la:"+demmax);
System.out.print("chi so cua day dai nhat la:"+x);
}
}
K?t qu?
so cac so thuoc day dai nhat la:5
chi so cua day dai nhat la:4
15. 15
Process completed.
B¨¤i 11
/**
*Cho 1 day so nguyen bat ki. Hay tim 1 day con lien tuc don dieu tang dai nhat cua day tren
*/
public class mang11 {
public static void main(String[] args) {
int [] a={8,4,9,12,1,2,3,3,10,3};
int N=10,k,demmax=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 (demmax<dem)
{
demmax=dem;
x=k;
}
}
else break;
}
}
}
System.out.println("so cac so thuoc day dai nhat la:" +demmax);
System.out.print("Chi so cua day dai nhat la:" +x);
}}
K?t qu?
so cac so thuoc day dai nhat la:5
Chi so cua day dai nhat la:4
Process completed.
B¨¤i 12
/**
*Day so a[ ] duoc goi la day con cua b[ ] neu tu b[ ] xoa di 1 vai so se thu duoc a[ ]
*Cho truoc 2 day 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. 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. 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. 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. 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. 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. 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. 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. 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. 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++;