Re: 10194 - Football (aka Soccer)
Posted: Fri Feb 18, 2011 9:12 pm
pretty easy problem bt every1 should read the problem description carefully
Code: Select all
#include <iostream>
#include <algorithm>
#define TEAMNAME 31
#define MAXLINE 70
using namespace std;
struct Team {
int points;
int wins;
int goalsScored;
int goalsAgainst;
int losses;
int gamesPlayed;
char name[TEAMNAME];
};
Team * locateTeam( Team * teams, char * teamName) {
int i = 0;
while (strcmp(teams[i].name, teamName) != 0 ) {
i++;
}
return teams + i;
}
void parseGame(char * encodedGame, Team teams[]) {
// First team
char * tok = strtok(encodedGame, "#");
Team * firstTeam = locateTeam(teams, tok);
// First team score
tok = strtok(NULL, "@");
int firstTeamScore = atoi(tok);
// Second team score
tok = strtok(NULL, "#");
int secondTeamSocre = atoi(tok);
//Second team
tok = strtok(NULL, "#");
Team * secondTeam = locateTeam(teams, tok);
Team * winner = NULL;
Team * loser = NULL;
if ( firstTeamScore > secondTeamSocre ) {
winner = firstTeam;
loser = secondTeam;
} else if ( secondTeamSocre > firstTeamScore ){
winner = secondTeam;
loser = firstTeam;
}
// Has a winner?
if (winner) {
winner->points += 3;
winner->wins += 1;
loser->losses += 1;
} else {
firstTeam->points += 1;
secondTeam->points += 1;
}
// Update scores
firstTeam->goalsScored += firstTeamScore;
firstTeam->goalsAgainst += secondTeamSocre;
secondTeam->goalsAgainst += firstTeamScore;
secondTeam->goalsScored += secondTeamSocre;
// Games played
firstTeam->gamesPlayed++;
secondTeam->gamesPlayed++;
}
void initializeTeam(Team& team, char * teamName) {
strcpy(team.name, teamName);
team.points = 0;
team.wins = 0;
team.goalsScored = 0;
team.goalsAgainst = 0;
team.losses = 0;
team.gamesPlayed = 0;
}
bool compareTeams(Team a, Team b) {
if ( a.points > b.points ) {
return true;
} else if ( a.points < b.points ) {
return false;
}
if ( a.wins > b.wins ) {
return true;
} else if ( a.wins < b.wins ) {
return false;
}
int aGd = a.goalsScored - a.goalsAgainst;
int bGd = b.goalsScored - b.goalsAgainst;
if ( aGd > bGd ) {
return true;
} else if ( aGd < bGd ) {
return false;
}
if ( a.goalsScored > b.goalsScored ) {
return true;
} else if ( a.goalsScored < b.goalsScored ){
return false;
}
if ( a.gamesPlayed < b.gamesPlayed ) {
return true;
} else if ( a.gamesPlayed > b.gamesPlayed ) {
return false;
}
return strcasecmp(a.name, b.name) < 0;
}
int main() {
char line[MAXLINE];
cin.getline(line, MAXLINE);
int nOfTournment = atoi(line);
for (int t = 0; t < nOfTournment; t++) {
if ( t != 0 ) {
cout << endl;
}
// Reads the tournment name
cin.getline(line, MAXLINE);
int lengthTournmentName = strlen(line);
char tournamentName[lengthTournmentName + 1];
strcpy(tournamentName, line);
// How many teams?
cin.getline(line, MAXLINE);
int nOfTeams = atoi(line);
Team teams[nOfTeams];
for ( int team = 0; team < nOfTeams; team++) {
// Reads the name of the team
cin.getline(line, MAXLINE);
initializeTeam(teams[team], line);
}
// How many games?
cin.getline(line, MAXLINE);
int nOfGames = atoi(line);
for ( int game = 0; game < nOfGames; game++ ) {
// Reads the game
cin.getline(line, MAXLINE);
parseGame(line, teams);
}
// Sorts
sort(teams, teams + nOfTeams, compareTeams);
// Prints
cout << tournamentName << endl;
for ( int team = 0; team < nOfTeams; team ++ ) {
Team tm = teams[team];
printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n", team+1, tm.name,
tm.points, tm.gamesPlayed, tm.wins,
(tm.gamesPlayed-tm.wins-tm.losses), tm.losses,
(tm.goalsScored - tm.goalsAgainst), tm.goalsScored,
tm.goalsAgainst);
}
}
return 0;
}
Code: Select all
Scanner cin = new Scanner(System.in, "ISO-8859-1");
PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out, "ISO-8859-1"));
// your code here
cout.flush();
Code: Select all
return strcasecmp(team1Name, team2Name) < 0;
Code: Select all
char team1[31], team2[31];
int goalsForTeam1, goalsForTeam2;
.
.
scanf ("%[^#]#%d@%d#%[^\n]\n", team1, &goalsForTeam1, &goalsForTeam2, team2);
Code: Select all
%[^#] - this will take all the characters until the # character is encountered. The string will be stored in the variable 'team1'.
# - when this character is encountered, the scanf function will consume it. It is not stored in the variables.
%d@ - will take a normal integer value until the character @ is encountered. The integer is stored in the variable 'goalsForTeam1'.
%d# - will take a normal integer value until the character # is encountered. The integer is stored in the variable 'goalsForTeam2'.
[^\n] - will take any character till end of line is encountered. The string will be stored in the variable 'team2'.
\n - when this character is encountered, the scanf function will consume it and hence will not be sent to the next string to be read.
Using this technique, we avoid flushing stdin.
Code: Select all
Accepted after so many wrong answer..
thank you very much amishera for your test data.
Accepted![]()
. Compare Function's string compare insensitive.
animenologist wrote:Here's the input and output I used. I used it to test whether or not the comparisons were working correctly, by creating two teams that would make it all the way to that level. Also, my last case, I had them with different cases to make sure that they were sorted by lexicographical order.
Input:Output:Code: Select all
6 Test 1 4 A B C D 3 A#3@1#C A#1@1#C B#3@0#D TEST 2 4 A B C D 5 A#3@1#C B#1@1#D B#1@1#D B#1@1#D B#0@1#D TEST 3 4 A B C D 2 A#2@1#C B#3@1#D TEST 4 4 A B C D 3 A#3@1#C B#4@1#D B#1@2#D TEST 5 4 A B C D 3 B#3@1#C A#3@0#D A#0@1#D TEST 6 5 aa BB c D eE 0 Test 6 4 A B C D 3 A#1@3#C A#1@1#C B#0@3#D
Code: Select all
Test 1 1) A 4p, 2g (1-1-0), 2gd (4-2) 2) B 3p, 1g (1-0-0), 3gd (3-0) 3) C 1p, 2g (0-1-1), -2gd (2-4) 4) D 0p, 1g (0-0-1), -3gd (0-3) TEST 2 1) D 6p, 4g (1-3-0), 1gd (4-3) 2) A 3p, 1g (1-0-0), 2gd (3-1) 3) B 3p, 4g (0-3-1), -1gd (3-4) 4) C 0p, 1g (0-0-1), -2gd (1-3) TEST 3 1) B 3p, 1g (1-0-0), 2gd (3-1) 2) A 3p, 1g (1-0-0), 1gd (2-1) 3) C 0p, 1g (0-0-1), -1gd (1-2) 4) D 0p, 1g (0-0-1), -2gd (1-3) TEST 4 1) B 3p, 2g (1-0-1), 2gd (5-3) 2) A 3p, 1g (1-0-0), 2gd (3-1) 3) D 3p, 2g (1-0-1), -2gd (3-5) 4) C 0p, 1g (0-0-1), -2gd (1-3) TEST 5 1) B 3p, 1g (1-0-0), 2gd (3-1) 2) A 3p, 2g (1-0-1), 2gd (3-1) 3) D 3p, 2g (1-0-1), -2gd (1-3) 4) C 0p, 1g (0-0-1), -2gd (1-3) TEST 6 1) aa 0p, 0g (0-0-0), 0gd (0-0) 2) BB 0p, 0g (0-0-0), 0gd (0-0) 3) c 0p, 0g (0-0-0), 0gd (0-0) 4) D 0p, 0g (0-0-0), 0gd (0-0) 5) eE 0p, 0g (0-0-0), 0gd (0-0)
Code: Select all
// Program :
// Author : Anindya Sundar Paul
// Run-time:
// Verdict : WA
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
using namespace std;
#define EPS 1e-9
#define INF 2147483647
#define PI 3.14159265358979323846264338327950
#define MEM( x, y ) memset( x, y, sizeof( x ) )
#define READ( file ) freopen( file, "r", stdin )
#define WRITE( file ) freopen( file, "w", stdout )
typedef long long LL;
typedef unsigned long long ULL;
/// Template ends, coding starts
class Team {
public:
string name;
int points;
int played;
int wins;
int ties;
int losses;
int gd, gs, ga;
Team() { points = 0; played = 0; wins = 0; ties = 0; losses = 0; gd = 0; ga = 0; gs = 0; }
bool operator < ( const Team& x ) const
{
if( points != x.points ) return points > x.points;
if( wins != x.wins ) return wins > x.wins;
if( gd != x.gd ) return gd > x.gd;
if( gs != x.gs ) return gs > x.gs;
if( played != x.played ) return played < x.played;
char str1[100], str2[100];
strcpy( str1, name.c_str() );
strcpy( str2, x.name.c_str() );
for( int i = 0; str1[i]; i++ ) str1[i] = tolower( str1[i] );
for( int i = 0; str2[i]; i++ ) str2[i] = tolower( str2[i] );
return strcmp( str1, str2 ) < 0;
}
};
vector <Team> v;
int main()
{
// freopen( "in.txt", "r", stdin );
// freopen( "out.txt", "w", stdout );
int n, i;
cin >> n;
getchar();
while( n-- ) {
char str[110];
char tourName[110];
gets( tourName );
int nTeams;
cin >> nTeams;
getchar();
v.clear();
for( i = 0; i < nTeams; i++ ) {
gets( str );
string s( str );
Team t;
t.name = s;
v.push_back( t );
}
int m, j;
cin >> m;
getchar();
while( m-- ) {
gets( str );
char t1[100], t2[100], g1[10], g2[10];
bool hashFlag = false;
for( i = 0; str[i] != '@'; i++ ) {
if( !hashFlag && str[i] == '#' ) {
strncpy( t1, str, i );
t1[i] = 0;
hashFlag = true;
j = 0;
}
else {
g1[j] = str[i];
g1[j+1] = 0;
j++;
}
}
i++;
j = 0;
hashFlag = false;
for( ; str[i]; i++ ) {
if( !hashFlag && str[i] != '#' ) {
g2[j] = str[i];
g2[j+1] = 0;
j++;
}
else break;
}
strcpy( t2, &str[i+1] );
int goal1 = atoi( g1 );
int goal2 = atoi( g2 );
for( i = 0; i < nTeams; i++ ) {
if( !( v[i].name.compare( t1 ) ) ) {
v[i].played++;
v[i].gs += goal1;
v[i].ga += goal2;
v[i].gd = v[i].gs - v[i].ga;
if( goal1 > goal2 ) {
v[i].wins++;
v[i].points += 3;
}
else if( goal1 == goal2 ) {
v[i].ties++;
v[i].points++;
}
else v[i].losses++;
}
else if( !( v[i].name.compare( t2 ) ) ) {
v[i].played++;
v[i].gs += goal2;
v[i].ga += goal1;
v[i].gd = v[i].gs - v[i].ga;
if( goal2 > goal1 ) {
v[i].wins++;
v[i].points += 3;
}
else if( goal1 == goal2 ) {
v[i].ties++;
v[i].points++;
}
else v[i].losses++;
}
}
}
sort( v.begin(), v.end() );
puts( tourName );
for( i = 0; i < nTeams; i++ ) {
printf( "%d) ", i+1 );
cout << v[i].name << ' ' << v[i].points << 'p' << ", " << v[i].played << 'g';
printf( " (%d-%d-%d), %dgd (%d-%d)\n", v[i].wins, v[i].ties, v[i].losses, v[i].gd, v[i].gs, v[i].ga );
}
if( n != 0 ) puts( "" );
}
return 0;
}
thanks for your reply but i usually remove read/write to files lines before sending it to the sitebrianfry713 wrote:Don't read and write to files.