View Code of Problem 100

#include <string>
#include <iostream>
#include <cstdio>
#include <sstream>
#include <algorithm>
#include <cstring>
using namespace std;
struct song{
	string name;
	int second;
	int score;
};
int getscore(int you,int my){
	double e=my*1.0/you;
	if(e>=0.2&&e<0.4)
		return 1;
	else if(e>=0.4&&e<0.6)
		return 2;
	else if(e>=0.6&&e<0.8)
		return 3;
	else if(e>=0.8&&e<1)
		return 4;
	else if(e==1)
		return 5;
	else
		return 0;

}
bool cmp(song a,song b){
	if(a.score==b.score)
		return a.name.compare(b.name)<0;
	return a.score>b.score;
}
int main(){
	int n;
	while(cin>>n){
		song songs[101];
		if(n==0)
			break;
		for(int i=0;i<n;i++){
			string name;
			string time;
			cin>>name>>time;
			stringstream ss(time);
			string temp;
			int times[2]={0};
			int count=0;
			while(getline(ss,temp,':')){
				times[count++]=stoi(temp);
			}
			songs[i].name=name;
			songs[i].second=times[0]*60+times[1];
		}
		int m;
		cin>>m;
		for(int i=0;i<m;i++){
			string name;
			string time;
			cin>>name>>time;
			stringstream ss(time);
			string temp;
			int times[2]={0};
			int count=0;
			while(getline(ss,temp,':')){
				times[count++]=stoi(temp);
			}
			for(int j=0;j<n;j++){
				if(name==songs[j].name){
					songs[j].score+=getscore(songs[j].second,times[0]*60+times[1]);
				}
			}
		}
		sort(songs,songs+n,cmp);
		for(int i=0;i<n;i++){
			cout<<songs[i].name<<" "<<songs[i].score<<endl;
		}
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 100