Join Regular Classroom : Visit ClassroomTech

Searching & Sorting | Football League | Codewindow.in

Football League Table Statement

All major football leagues have huge league tables. Whenever a new match is played, the league table of match is updated to show the current rankings (based on Scores, Goals For (GF), Goals Against (GA)). Given the results of a few matches among teams, write a code to print all the names of the teams in increasing order based on their rankings. Rules are as follow A win results in TWO points, a draw results in ONE point & a loss is worth ZERO points. The team with the most number of goals in a match wins the match. Goal Difference is calculated as Goals For GF: Goals Against GA. Football Teams can play a maximum of 2 matches against each other : Home & Away matches respectively. The Football match ranking is decided as below :

Football Team with maximum points is ranked 1 & minimum points is placed last Ties are broken as follows Football Teams with same points are ranked according to Goal Difference If Goal Difference(GD) is the equal , the Football team with higher Goals For is ranked ahead If GF is equal, the teams must be at the equal rank but they must be printed in case-insensitive alphabetic according to the team names. More than TWO matches of same teams, should be considered as Invalid I/P. A team can’t play matches against itself, Therefore if team names are same for a given match, it should be considered Invalid I/P .

Input Format:
Football teams [ Na] delimited by a whitespace character.
Third line: no. of matches M
Next M lines: a match information tuple {T1 T2 S1 S2}, where tuple is consisting of the following information.
Name of the first team: T1
Name of the second team: T2
Goals scored by the first team: S1
Goals scored by the second team: S2

Output Format:
Team names in order of their rankings, one team / line
OR
where appropriate Print “Invalid Input”.

Constraints:
0 < N < =10,000 0 < =S1, S2

Solution: In Java

import java.util.*;
import java.lang.*;

class Main
{
    public static void main(String args[]) throws NullPointerException
    {
        int n,m,i,j,k;
        int x=0;
        Scanner sc=new Scanner(System.in);
        System.out.println("\n\n");
        System.out.println("Enter number of Teams:");
        n=sc.nextInt();
        String z[]=new String[n];
        System.out.println("\n\n");
        System.out.println("Enter names of Teams:");
        for(i=0;i<n;i++)
        {
            z[i]=sc.next();
        }
        System.out.println("\n\n");
        System.out.println("Enter number of Matches:");
        m=sc.nextInt();
        int pt[]=new int[n];
        int gf[]=new int[n];
        int ga[]=new int[n];
        int gd[]=new int[n];
        int mt[]=new int[n];
        String s[]=new String[n+n];
        String a[][]=new String[m][4];
        System.out.println("\n\n");
        System.out.println("Enter details of Matches(Team1 Team2 T1goals T2goals)");
        for(i=0;i<m;i++)
        {
            for(j=0;j<4;j++)
            {
                a[i][j]=sc.next();
            }
            System.out.println();
        }
        
        for(i=0;i<m;i++)
        {
            for(j=0;j<2;j++)
            {
                k=0;
                int temp=0;
                int index=0;
                while(((i-k)>=0)&&(i>0))
                {
                    if(a[i][j].equals(s[i-k]))
                    {
                        temp=1;
                        index=i-k;
                    }
                    k++;
                }
                if(temp==1)
                {
                    mt[index]+=1;
                    gf[index]+=Integer.parseInt(a[i][(j+2)]);
                    ga[index]+=Integer.parseInt(a[i][(3-j)]);
                    if((Integer.parseInt(a[i][j+2]))>(Integer.parseInt(a[i][3-j])))
                        pt[index]+=2;
                    else if(Integer.parseInt(a[i][j+2])==Integer.parseInt(a[i][3-j]))
                        pt[index]+=1;
                    else
                        pt[index]+=0;
                }
                else
                {
                    s[x]=a[i][j];
                    mt[x]+=1;
                    gf[x]=Integer.parseInt(a[i][(j+2)]);
                    ga[x]=Integer.parseInt(a[i][(3-j)]);
                    if((Integer.parseInt(a[i][j+2]))>(Integer.parseInt(a[i][3-j])))
                        pt[x]=2;
                    else if(Integer.parseInt(a[i][j+2])==Integer.parseInt(a[i][3-j]))
                        pt[x]=1;
                    else
                        pt[x]=0;
                    x++;
                }
            }
        }
        int v=m;
        int p[]=new int[n];
        for(i=0;i<m;i++)
        {
            gd[i]=gf[i]-ga[i];
            p[i]=pt[i];
        }
        int g[]=new int[n];
        int b;
        int r[]=new int[n];
        for(i=0;i<m;++i)
        {
            for(j=0;j<(m-i-1);++j)
                if(p[j]<p[j+1])
                {
                    b=p[j];
                    p[j]=p[j+1];
                    p[j+1]=b;
                }
        }
        int mp[]=new int[n];
        int goalf[]=new int[n];
        int goala[]=new int[n];
        String tn[]=new String[n+n];
        for(i=0;i<m;i++)
        {
            for(j=0;j<m;j++)
            { 
                if(pt[i]==p[j])
                {
                    tn[j]=s[i];
                    mp[j]=mt[i];
                    goalf[j]=gf[i];
                    goala[j]=ga[i];
                    g[j]=gd[i];
                }
            }
        }
        for(i=m;i<n;i++)
        {
            mp[i]=0;
            goalf[i]=0;
            goala[i]=0;
            g[i]=0;
            p[i]=0;
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            { 
                if((z[i].equals(tn[j])))
                {
                    z[i]="0";
                }
            }
        }
        for(i=0;i<n;i++)
            if(z[i]!="0")
            {
                tn[v]=z[i];
                v++;
            }
        for(i=0;i<n;i++)
        {
            if(p[i]==p[i+1])
            { 
                if(g[i]>g[i+1])
                {
                    r[i]=i+1;
                    r[i+1]=i+2;
                    i=i+1;
                }
                else if(g[i]<g[i+1])
                {
                    r[i+1]=i+1;
                    r[i]=i+2;
                    i=i+1;
                }
                else
                {
                    try
                    {
                        int d=tn[i].compareToIgnoreCase(tn[i+1]);
                        if(d>0)
                        { 
                            s[i]=tn[i];
                            tn[i]=tn[i+1];
                            tn[i+1]=s[i];
                        }
                    }
                    catch(NullPointerException npe)
                    { 
                        npe.printStackTrace();
                    }
                    r[i]=i+1;
                    r[i+1]=i+1;
                    i=i+1;
                }
            }
            else
                r[i]=i+1;
        }
        System.out.print("\n\n\n\n\n");
        System.out.print("\t\t\t"+"Football Points Table"+"\t\t");
        System.out.print("\n\n");
        System.out.println("Name"+"\t\t"+"Match"+"\t"+"GoalF"+"\t"+"GoalA"+"\t"+"GD"+"\t"+"Points"+"\t"+"Rank");
        for(i=0;i<n;i++)
        {
            System.out.println(tn[i]+"\t\t"+mp[i]+"\t"+goalf[i]+"\t"+goala[i]+"\t"+g[i]+"\t"+p[i]+"\t"+r[i]);
        }
    }
}

Also Checkout

Recent Posts
Categories
Pages