0% found this document useful (0 votes)
66 views

Cod Java

The document contains Java code that reads data from files about mountain peaks and rock types, and defines methods for analyzing and manipulating that data. Some key methods include: - getList() which reads the data from files and returns a 2D array of objects containing height and rock type - afish() which prints out the rock types - interschimb() which swaps the first and last rows of data - delElement() which deletes an element based on position input - maximeLocale() which finds local maxima - cautaRoca() which analyzes data for a given rock type - other methods for sorting rocks by type, analyzing rock distribution, and finding

Uploaded by

Dana Botezatu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Cod Java

The document contains Java code that reads data from files about mountain peaks and rock types, and defines methods for analyzing and manipulating that data. Some key methods include: - getList() which reads the data from files and returns a 2D array of objects containing height and rock type - afish() which prints out the rock types - interschimb() which swaps the first and last rows of data - delElement() which deletes an element based on position input - maximeLocale() which finds local maxima - cautaRoca() which analyzes data for a given rock type - other methods for sorting rocks by type, analyzing rock distribution, and finding

Uploaded by

Dana Botezatu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Cod Java

import java.io.*;
import java.util.*;

class Main
{
static int n;
static int m;

static Punct[][] getList()


{
Punct[][] rez=new Punct[100][100];
Scanner fin1=null;
Scanner fin2=null;
try
{
fin1=new Scanner(new File("Munte.in"));
fin2=new Scanner(new File("Roci.in"));

n=fin1.nextInt();
m=fin1.nextInt();
for(int i=1;i<=n;i++)
{
for(int ii=1;ii<=m;ii++)
{
// rez[i][ii]=new Punct();
// rez[i][ii].h=fin1.nextInt();
// rez[i][ii].tip=fin2.next();
rez[i][ii]=new
Punct(fin1.nextInt(),fin2.next());
}
}

}
catch (Exception e)
{

return rez;
}

static void afish()


{
Punct[][] a=getList();

for(int i=1;i<=n;i++)
{
for(int ii=1;ii<=m;ii++)
{
System.out.print(a[i][ii].tip+" ");
}
System.out.println();
}
System.out.println();
}

static void interschimb()


{
Punct[][] a=getList();
try
{
BufferedWriter fout1=new BufferedWriter(new
FileWriter("Munte.in",false));
BufferedWriter fout2=new BufferedWriter(new
FileWriter("Roci.in",false));

fout1.write(Integer.toString(n)+" "+Integer.toString(m)
+"\n");

for(int ii=1;ii<=m;ii++)
{
fout1.write(a[n][ii].h+" ");
fout2.write(a[n][ii].tip+" ");
}
fout1.newLine();
fout2.newLine();

for(int i=2;i<n;i++)
{
for(int ii=1;ii<=m;ii++)
{
fout1.write(a[i][ii].h+" ");
fout2.write(a[i][ii].tip+" ");
}
fout1.newLine();
fout2.newLine();
}

for(int ii=1;ii<=m;ii++)
{
fout1.write(a[1][ii].h+" ");
fout2.write(a[1][ii].tip+" ");
}
fout1.close();
fout2.close();
}
catch (Exception e)
{

}
}

static void delElement()


{
Punct[][] a=getList();

Scanner cin=new Scanner(System.in);

String poz=null;
System.out.println("Pozitia : ");
do
{
System.out.println("( Nord | Sud | Est | Vest )");
System.out.print(">");
poz=cin.next();
}
while(!poz.equals("Nord") && !poz.equals("Sud") && !
poz.equals("Est") && !poz.equals("Vest"));

try
{
BufferedWriter fout1=new BufferedWriter(new
FileWriter("Munte.in",false));
BufferedWriter fout2=new BufferedWriter(new
FileWriter("Roci.in",false));

if(poz.equals("Nord") || poz.equals("Sud"))
{
n--;
}
if(poz.equals("Vest") || poz.equals("Est"))
{
m--;
}

fout1.write(Integer.toString(n)+" "+Integer.toString(m)
+"\n");
if(poz.equals("Nord") || poz.equals("Sud"))
{
n++;
}
if(poz.equals("Vest") || poz.equals("Est"))
{
m++;
}
for(int i=1;i<=n;i++)
{
if(poz.equals("Nord") && i==1)i++;
if(poz.equals("Sud") && i==n)break;
for(int ii=1;ii<=m;ii++)
{
if(poz.equals("Vest") && ii==1)ii++;
if(poz.equals("Est") && ii==m)break;
fout1.write(Integer.toString(a[i][ii].h)+" ");
fout2.write(a[i][ii].tip+" ");
}

fout1.newLine();
fout2.newLine();
}
if(poz.equals("Nord") || poz.equals("Sud"))
{
n--;
}
if(poz.equals("Vest") || poz.equals("Est"))
{
m--;
}

fout1.close();
fout2.close();
}
catch (Exception e)
{

}
}
static void maximeLocale()
{
Punct[][] a=getList();

int[] diri= {-1,-1, 0, 1, 1, 1, 0,-1};


int[] dirii={ 0, 1, 1, 1, 0,-1,-1,-1};

for(int i=1;i<=n;i++)
{
for(int ii=1;ii<=m;ii++)
{
int ok=1;
if(i!=1 && ii!=1 && i!=n && ii!=m)
{
for(int d=0;d<=7;d++)
{
int ni=i+diri[d];
int nii=ii+dirii[d];

if(a[ni][nii].h>=a[i][ii].h)ok=0;
}
if(ok==1)
{
System.out.println(i+" "+ii);
}

}
}
}
}

static void cautaRoca()


{
Punct[][] a=getList();

Scanner cin=new Scanner(System.in);

System.out.print("Tipul de roca : ");


String tipc=cin.next();

int count=0;
int sum=0;
for(int i=1;i<=n;i++)
{
for(int ii=1;ii<=m;ii++)
{
if(a[i][ii].tip.equals(tipc))
{
count++;
sum+=a[i][ii].h;
}
}
}

int nr=0;
if(count>0)
{
for(int i=1;i<=n;i++)
{
for(int ii=1;ii<=m;ii++)
{
if(a[i][ii].h>(sum/count))
{
nr++;
}
}
}
}
System.out.println("Numarul zonelor mai inalte de cat media :
"+nr);
}

static ArrayList<String> getRoci()


{
ArrayList<String> rez=new ArrayList<String>();

Punct[][] a=getList();

for(int i=1;i<=n;i++)
{
for(int ii=1;ii<=m;ii++)
{
int ok=0;
for(String y:rez)
{
if(a[i][ii].tip.equals(y))ok=1;
}
if(ok==0)rez.add(a[i][ii].tip);
}
}
return rez;
}

static void sortRoci()


{
Punct[][] a=getList();

ArrayList<String> roci=getRoci();
int[] count=new int[roci.size()+1];
for(int y=0;y<roci.size();y++)
{
for(int i=1;i<=n;i++)
{
for(int ii=1;ii<=m;ii++)
{
if(a[i][ii].tip.equals(roci.get(y)))
{
count[y]++;
}
}
}
}

for(int y=0;y<roci.size();y++)
{
for(int yy=y;yy<roci.size();yy++)
{
if(count[y]<count[yy])
{
int aux=count[y];
count[y]=count[yy];
count[yy]=aux;

Collections.swap(roci,y,yy);
}
}
}

for(int y=0;y<roci.size();y++)
{
System.out.println(roci.get(y)+" "+count[y]);
}
}

static void rociTip()


{
Punct[][] a=getList();
int sum=0;
for(int i=1;i<=n;i++)
{
for(int ii=1;ii<=m;ii++)
{
sum+=a[i][ii].h;
}
}

int med=sum/(n*m);

try
{
BufferedWriter fout1=new BufferedWriter(new
FileWriter("RociTip.txt",false));

for(int i=1;i<=n;i++)
{
int ko=0;
for(int ii=1;ii<=m;ii++)
{
if(a[i][ii].h>med)ko=1;
}
if(ko==1)
{
for(int ii=1;ii<=m;ii++)
{
fout1.write(a[i][ii].tip+" ");
}
fout1.newLine();
}
}

fout1.close();
}
catch (Exception e)
{

}
}

static int perimetru(int x1,int x2,int x3,int x4)


{
Punct[][] a=getList();

int rez=1;
int sum=0;
int count=0;
for(int i=x1;i<=x3;i++)
{
for(int ii=x2;ii<=x4;ii++)
{
if(i!=x1 && i!=x3 && ii!=x2 && ii!=x4)
{
sum+=a[i][ii].h;
count++;
}
}
}
if(count==0)count++;
for(int i=x1;i<=x3;i++)
{
for(int ii=x2;ii<=x4;ii++)
{
if(i==x1 || i==x3 || ii==x2 || ii==x4)
{
if(a[i][ii].h<(sum/count))rez=0;
}
}
}
return rez;
}

static void dreptunghiOriginal()


{
Punct[][] a=getList();

int min=-1;
int x1=0,x2=0,y1=0,y2=0;
int nn=n,mm=m;
for(int i=1;i<=n;i++)
{
mm=m;
for(int ii=1;ii<=m;ii++)
{
for(int y=i+1;y<=nn;y++)
{
for(int yy=ii+1;yy<=mm;yy++)
{
if(perimetru(i,ii,y,yy)==1)
{
if(min==-1)
{
min=(y-i+1)*(yy-ii+1);
x1=i;
x2=ii;
y1=y;
y2=yy;
}
else if((y-i+1)*(yy-ii+1)>min)
{
min=(y-i+1)*(yy-ii+1);
x1=i;
x2=ii;
y1=y;
y2=yy;
}
}
}
}
}
}
if(min!=-1)
System.out.println(min+" "+(x1)+" "+(x2)+" "+(y1)+" "+(y2));
}

static void alpinistuafisgheste()


{
Punct[][] a=getList();
int[][] b=new int[100][100];

int[] diri= {-1,-1, 0, 1, 1, 1, 0,-1};


int[] dirii={ 0, 1, 1, 1, 0,-1,-1,-1};

for(int i=1;i<=n;i++)
{
for(int ii=1;ii<=m;ii++)
{
b[i][ii]=0;
}
}

Scanner cin=new Scanner(System.in);


System.out.println("Introduceti coordonatele : ");
System.out.print("x>");
int x=cin.nextInt();
System.out.print("y>");
int y=cin.nextInt();

b[x][y]=1;
for(int d=0;d<=7;d++)
{
int ni=x+diri[d];
int nii=y+dirii[d];
if(a[x][y].h<=a[ni][nii].h)b[ni][nii]=2;
}

int ok=0;
int count=2;
while(ok==0)
{
count++;
ok=1;
for(int i=1;i<=n;i++)
{
for(int ii=1;ii<=m;ii++)
{
for(int d=0;d<=7;d++)
{
try
{
int ni=i+diri[d];
int nii=ii+dirii[d];
if(a[i][ii].h<=a[ni][nii].h && b[i]
[ii]==count-1 && b[ni][nii]==0)
{
b[ni][nii]=count;
ok=0;
}

}
catch (Exception e) {}
}
}
}
}

int hmax=0;

for(int i=1;i<=n;i++)
{
for(int ii=1;ii<=m;ii++)
{
if(a[i][ii].h>hmax)
{
hmax=a[i][ii].h;
}
}
}

int danu=0;
int xf=-1;
int yf=-1;
for(int i=1;i<=n;i++)
{
for(int ii=1;ii<=m;ii++)
{
if(a[i][ii].h==hmax && b[i][ii]!=0)
{
danu=1;
if(xf==-1)
{
xf=i;
yf=ii;
}
else
{
if(b[xf][yf]>b[i][ii])
{
xf=i;
yf=ii;
}
}
}
}
}

if(danu==1)
{
ArrayList<Integer> pi=new ArrayList<Integer>();
ArrayList<Integer> pii=new ArrayList<Integer>();
pi.add(xf);
pii.add(yf);

System.out.println("Da "+b[xf][yf]);
while(b[xf][yf]>1)
{
for(int d=0;d<=7;d++)
{
if(b[xf+diri[d]][yf+dirii[d]]<b[xf][yf] &&
b[xf+diri[d]][yf+dirii[d]]>0)
{
xf=xf+diri[d];
yf=yf+dirii[d];
pi.add(xf);
pii.add(yf);
}
}
}

for(int i=pi.size()-1;i>=0;i--)
{
System.out.println(pi.get(i)+" "+pii.get(i));
}
}
else
{
System.out.println("NU");
}

for(int i=1;i<=n;i++)
{
for(int ii=1;ii<=m;ii++)
{
System.out.print(b[i][ii]+" ");
}
System.out.println();
}

public static void main(String[] args)


{
afish();
//interschimb();

//delElement();

// maximeLocale();

// cautaRoca();

// sortRoci();

// rociTip();

// dreptunghiOriginal();

// alpinistuafisgheste();

}
}

class Punct
{
int h;
String tip;

Punct(){}

Punct(int h,String tip)


{
this.h=h;
this.tip=tip;
}}

You might also like