View Code of Problem 100

#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;

typedef struct {
	char name[30];
	int times;
	int score;
}Song;

int findIndex(Song s[], int len, char name[]) {
	for (int i = 0;i < len;i++) {
		if (strcmp(s[i].name, name) == 0) {
			return i;
		}
	}
	return -1;
}

int fun(Song s, int times) {
	if (times * 5 < s.times) {
		return 0;
	}
	else if (times * 5 < s.times * 2) {
		return 1;
	}
	else if (times * 5 < s.times * 3) {
		return 2;
	}
	else if (times * 5 < s.times * 4) {
		return 3;
	}
	else if (times * 5 < s.times * 5) {
		return 4;
	}
	else {
		return 5;
	}
}

bool cmp(Song a, Song b) {
	if (a.score == b.score) {
		return strcmp(a.name, b.name) < 0;
	}else {
		return a.score > b.score;
	}
}

int main() {
	int n;
	while (cin>>n && n != 0) {
		Song song[101];
		int a, b;
		char c;
		for (int i = 0;i < n;i++) {
			cin >> song[i].name;
			cin >> a >> c >> b;
			song[i].times = a * 60 + b;
			song[i].score = 0;
		}
		int m, times;
		char name[30];
		cin >> m;
		while (m--) {
			cin >> name;
			cin >> a >> c >> b;
			times = a * 60 + b;
			song[findIndex(song, n, name)].score += fun(song[findIndex(song, n, name)], times);
		}

		sort(song, song + n, cmp);

		for (int i = 0;i < n;i++) {
			cout<<song[i].name<<" "<<song[i].score<<endl;
		}
	}
}

Double click to view unformatted code.


Back to problem 100