UEFA CHAMPIONSHIP problem

I m not able to crack UEFA championship problem via this code.why?
import java.util.*;

class teamInfo {
String teamName;
int score,goalDifference;

teamInfo(String teamName,int score,int goalDifference){
    this.teamName=teamName;
    this.score = score;
    this.goalDifference=goalDifference;
}

void display(){
    System.out.println(teamName);
    System.out.println(score);
    System.out.println(goalDifference);
}

}
public class Main
{
public static void main(String[] args) {
try{
int z=-1,x=-1,s=0;
int score_t1=0,score_t2=0,t1_score,t2_score;
String t1,t2;
teamInfo[] teams = new teamInfo[4];
String[] str= new String[5];
str[0]=str[1]=str[2]=str[3]="";
int temp;
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();

        for(int m=1;m<=t;m++){
             
                z=-1;x=-1;
            for(int j=1;j<=12;j++){
                    
                t1 = sc.next();
                t1_score = sc.nextInt();
                t2 = sc.next();
                t2_score = sc.nextInt();
                t2 = sc.next();
                if(t1_score>t2_score){
                    score_t1=3;
                    score_t2=0;
                }
                else if (t1_score<t2_score) {
                    score_t1=0;
                    score_t2=3;
                }else{
                    score_t2=1;score_t1=1;
                }
               
                for(int k=0;k<=z;k++){
                    x=-1;
                    if(teams[k].teamName.equals(t1)){
                        x=k;
                        break;
                    }
                }
               
                if (x==-1){
                    z++;
                    teams[z]= new teamInfo(t1,score_t1,t1_score-t2_score);
                    
                }
                else{
                    teams[x].score+=score_t1;
                    teams[x].goalDifference+=t1_score-t2_score;
                    
                }
                
                for(int k=0;k<=z;k++){
                    x=-1;
                   
                    if(teams[k].teamName.equals(t2)){
                        x=k;
                        break;
                    }
                }
                if (x==-1){
                    z++;
                    teams[z]= new teamInfo(t2,score_t2,t2_score-t1_score);
                    
                }
                else{
                    teams[x].score+=score_t2;
                    teams[x].goalDifference+=t2_score-t1_score;
                   
                }
                
            }
          for(int a=0;a<2;a++){
               temp=a;
                for(int k=a+1;k<4;k++){
                    if(teams[a].score<teams[k].score){
                        temp=k;
                     }
                    else if((teams[a].score==teams[k].score) && (teams[a].goalDifference<teams[k].goalDifference)){         
                      temp=k;
                    }
                    
                }   
                str[s]=teams[temp].teamName;
                s++;
                teams[temp].score=-1;         
            }
            teams[0]=teams[1]=teams[2]=teams[3]=null;
            
    }
        
             System.out.println(str[0]+" "+str[1]);
             System.out.println(str[2]+" "+str[3]);
    }catch(Exception e){
        return;
    }
	
}

}

Problem Link ? :slight_smile:

1 Like

Here is the problem link
UCL

You only have to display the team name, and nothing else. That is the first observation I observed.

Please give some tips on how to reduce runtime errors on this program`

#include<string.h>
#include<stdio.h>
int main()
{
int opu=0,in;
scanf("%d",&in);
while(opu<in)
{
int zi=0,si=0,point[4]{0},i,k,j,y=1,o,p,gd[4]{0},par[4]{0};
char h[10],a[4][10],b[10],u[3],d[10],z[4][10];
scanf("%s %d %s %d %s",&a[0],&o,&u,&p,&a[1]);
gd[0]+=o-p;
gd[1]+=p-o;
if(o>p)
{
point[0]+=3;
}
else if(p>o)
{
point[1]+=3;
}
else
{
point[1]++;
point[0]++;
}
for(i=1;i<12;i++)
{
scanf("%s %d %s %d %s",&b,&o,&u,&p,&d);
for(k=0;k<=y;k++)if(strcmp(a[k], b)==0)
{
gd[k]+=o-p;
if(o>p)
{
point[k]+=3;
}
else if(p==o)
{
point[k]++;
}
break;
}
for(j=0;j<=y;j++)if(strcmp(a[j], d)==0)
{
gd[j]+=p-o;
if(p>o)
{
point[j]+=3;
}
else if(p==o)
{
point[j]++;
}
break;
}
if(strcmp(a[j], d)==0)
{

	}
	else
	{
		y++;
		strcpy(a[y],d);
		gd[y]+=p-o;
		if(p>o)
		{
			point[y]+=3;
		}
		else if(p==o)
		{
			point[y]++;
    }
	}
	if(strcmp(a[k], b)==0)
	{
		
	}
	else
	{
		y++;
		strcpy(a[y],b);
		gd[y]+=o-p;
		if(o>p)
		{
			point[y]+=3;
		}
		else if(p==o)
		{
			point[y]++;
		}
	}

}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++) if(point[i]>=point[j])
{
par[i]++;
}
}
for(j=1;j<5;j++)
{
for(i=0;i<4;i++)
{
if(j==par[i])
{
strcpy(z[zi],a[i]);
zi++;
}
else
{

	}
}
}
for(i=0;i<3;i++)
{
	if(strcmp(z[i],z[i+1])==0&&gd[i]>gd[i+1])
	{
		strcpy(h,z[i]);
		strcpy(z[i],z[i+1]);
		strcpy(z[i+1],h);
	}
}
printf("%s %s\n",z[3],z[2]);
opu++;

}
return 0;
}`