Mảng 1 chiều và tìm phần tử lớn nhất:
[codes=java]import java.util.;
import java.io.;
class bt3
{
public static void main (String[] args)
{
int a[],n=0,i;
Scanner nhap=new Scanner(System.in);
System.out.print("Nhap vao so phan tu n");
n=nhap.nextInt();
a=new int[n+1];
for( i=0; i<n; i++)
{
System.out.print("Nhap phan tu thu a["+i+"]: ");
a[i]= nhap.nextInt();
}
for( i=0; i<n; i++)
{
System.out.print("\t"+a[i]);
}
int max=a[0];
for( i=0; i<n; i++)
{
if(a[i]>max)
max=a[i];
} System.out.print("\nMax la "+max);
}
}[/codes]
Mảng 2 chiều:
[codes=java]import java.util.;
import java.io.;
import java.lang.;
class bt4
{
public static void main (String[] args)
{
Scanner nhap=new Scanner(System.in);
int a[][],i,j;
System.out.print("Nhap so dong"); int dong=nhap.nextInt();
System.out.print("Nhap so cot"); int cot=nhap.nextInt();
a=new int [dong][cot];
for(i=0; i<dong; i++)
for(j=0;j<cot; j++)
a[i][j]=(int) (Math.random()50);
for(i=0; i<dong; i++)
{
for(j=0;j<cot; j++)
System.out.print("\t"+a[i][j]);
System.out.print("\n");
}
int s=0;
System.out.print("Nhap vao dong thu k can tinh tong:");
int k=nhap.nextInt();
for(i=0; i<cot; i++)
s+=a[i][k];
System.out.print("Tong dong la: "+s);
}
}[/codes]
Last modified on March 31st, 2021 at 12:56 am
Nam Le
lequocnam
0 responds