Bài tập java cơ bản (P3 - mảng)


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
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) {
              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
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) {
              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;
       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
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++)
                     {
                           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 complet

No comments:

Post a Comment