科大讯飞编程题:球赛
发布于 2017-09-16 16:36 6229 次浏览 0 赞 来自 我要提问  

编程题|20.0分2/3

球赛

时间限制:C/C++语言 1000MS;其他语言 3000MS
内存限制:C/C++语言 65536KB;其他语言 589824KB

题目描述:

大学生足协决定举办全国性的大学生足球赛,由每个学校派遣一支队伍代表该校参赛。比赛分区分为几个赛区进行,最终的总决赛中,将有不超过n支队伍参加。经过激烈的角逐,有机会参与总决赛的队伍已经决出。

 

协会对比赛的规则进行了调整,以便使得比赛更具有观赏性。

1. 总决赛的参赛队伍为n支,n为偶数;

2. 进入前1/2的队伍才有资格进入淘汰赛;

3. 队伍按积分排名,具体规则为:胜一场积3分;平一场积1分;负一场积0分。队伍首先按积分降序排列,积分相同按净胜球数降序排列,仍然相同的按进球数降序排列。

4. 基于上述规则,尚未出现有排名歧义的情况发生。

 

随着赛程的进行,目前各个队伍对战的结果已经确定了,小B负责确定进入淘汰赛的名单,她向你求助,你能帮她吗?

输入

测试数据有多组,每组测试数据的第一行为一个整数n(1=< n <=50),为参与总决赛的球队数,随后的n行为球队的名字,由不超过30个的大小写拉丁字母构成。随后的n*(n-1)/2行为赛事的开展情况,每行的格式为name1-name2 num1:num2,表示两支队伍的比分情况(0=<num1, num2<=100)。确保不会有两支队伍同名,也不会出现队伍自己通自己比赛的情况,且每场比赛仅出现一次。

样例输入

4

A

B

C

D

A-B 1:1

A-C 2:2

A-D 1:0

B-C 1:0

B-D 0:3

C-D 0:3

2

a

A

a-A 2:1

<div class="outputarea yangli ng-scope" ng-if="model.ques.questype==6 && model.ques.outputSample != null && model.ques.outputSample!=''" "="" style="box-sizing: border-box; margin: 0px; padding: 0px;">

样例输出

A

D

a


56 条回复

0%.无解

2017-09-16 16:38

那个小a是什么意思。。没看懂题目。。什么告知一下啊


2017-09-16 16:56
coder_NIskgbo1 回复 coder_2QE9MQ3G

注意看输入 有多个

2017-09-16 16:57
coder_42TKXJMC 回复 coder_2QE9MQ3G

小a是队伍的名字

2017-09-16 16:58

样例里面A赢一场平两场积5分,D赢两场负一场积6分,输出A怎么排在D前面?

2017-09-16 16:59
coder_4QCF9BHV 回复 coder_9rmoVBGb

map集合排序

2017-09-16 17:01
coder_42TKXJMC 回复 coder_9rmoVBGb

确实,题目有毛病

2017-09-16 17:01
张胜东 回复 coder_9rmoVBGb

进入淘汰赛的,再按字母顺序

2017-09-16 17:01 1

0%;

2017-09-16 17:02

本地调试完全没问题,但在赛码上就是0%;

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<stdlib.h>

using namespace std;

bool myComp(const vector<int>& a, const vector<int>& b){
	if(a[1]>b[1])
		return true;
	else if(a[1]<b[1])
		return false;
	if(a[2]>b[2])
		return true;
	else if(a[2]<b[2])
		return false;
	if(a[3]>b[3])
		return true;
	else if(a[3]<b[3])
		return false;
	else
		return true;
}

int main(void){
	int n;
	while(cin>>n){
		vector<string> names;
		vector<vector<int>> datas;
		for(int i = 0; i < n; i++){
			vector<int> t;
			for(int j = 0; j < 4; j++){
				t.push_back(0);
			}
			datas.push_back(t);
		}
		for(int i = 0; i < n; i++){
			datas[i][0] = i;
		}
		for(int i = 0; i < n; i++){
			string t;
			cin>>t;
			names.push_back(t);
		}
		for(int i = 0; i < n*(n-1)/2; i++){
			string t;
			cin>>t;
			int ind = t.find('-');
			string name1 = t.substr(0,ind);
			string name2 = t.substr(ind+1,t.size()-ind-1);
			auto iter = find(names.begin(),names.end(),name1);            
			int team1 = iter-names.begin();
			iter = find(names.begin(),names.end(),name2);            
			int team2 = iter-names.begin();
			cin>>t;
			ind = t.find(':');
			string s1 = t.substr(0,ind);
			string s2 = t.substr(ind+1,t.size()-ind-1);
			int score1 = atoi(s1.c_str());
			int score2 = atoi(s2.c_str());
			if(score1 > score2){
				datas[team1][1] += 3;
			}
			else if(score1 == score2){
				datas[team1][1] += 1;
				datas[team2][1] += 1;
			}
			else{
				datas[team2][1] += 3;
			}
			datas[team1][2] += (score1-score2)>0?(score1-score2):0;
			datas[team2][2] += (score2-score1)>0?(score2-score1):0;
			datas[team1][3] += score1;
			datas[team2][3] += score2;   
		}
		sort(datas.begin(), datas.end(), myComp);
		vector<int> res;
		for(int i = 0; i < n/2; i++){
			int team = datas[i][0];
			res.push_back(team);
		}
		sort(res.begin(), res.end());

		for(int i = 0; i < n/2; i++){            
			cout<<names[res[i]]<<endl;
		}
	}
	return 0;
}

2017-09-16 17:02
1
coder_YA5SNZJQ 回复 张胜东

排了一样0%。。。

2017-09-16 17:02 1
过样例百分之0,这题有问题吧



#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <cstdio>
#include <unordered_set>
#include <algorithm>
using namespace std;

struct team{
    string name;
    int scores;
    int goals;
    int wingoals;
    team():name(),scores(0){}
}teams[55];

bool cmp(team t1,team t2){
	if(t1.scores!=t2.scores)
    	return t1.scores>t2.scores;
    
    if(t1.wingoals!=t2.wingoals)
    	return t1.wingoals>t2.wingoals;
   	
   	if(t1.goals!=t2.goals)
   		return t1.goals>t2.goals;
}

bool cmp2(team t1,team t2){
    return t1.name<t2.name;
}

int main() {
//	freopen("E:\input.txt","r",stdin);
    int n;
    int mactchnum;
    while(cin>>n){
        vector<string> svec(n);
        unordered_map<string,int> mp;
        mactchnum=n*(n-1)/2;
        string tmp;
        for(int i=0;i<n;i++){
            cin>>teams[i].name;
            mp[teams[i].name]=i;
        }

        for(int i=0;i<mactchnum;i++){
            string a,b;
            cin>>tmp;
            int j=0;
            while(j!=tmp.size()&&tmp[j]!='-') ++j;
            a=tmp.substr(0,j-0);
            b=tmp.substr(j+1,tmp.size()-j-1);
           //cout<<a<<" "<<b<<" ";

            cin>>tmp;
            int s1=0,s2=0;
			j=0;
            while(tmp[j]!=':'){
                s1=s1*10+tmp[j]-'0';
                ++j;
            }

            ++j;
            while(j!=tmp.size()){
                s2=s2*10+tmp[j]-'0';
                ++j;
            }
			//printf("%d %d\n",s1,s2);
			teams[mp[a]].goals+=s1;
			teams[mp[a]].wingoals+=s1-s2;
			
			teams[mp[b]].goals+=s2;
			teams[mp[b]].wingoals+=s2-s1;
			
            if(s1==s2) {
                ++teams[mp[a]].scores;
                ++teams[mp[b]].scores;
            }
            else if(s1>s2)
            {
                teams[mp[a]].scores+=3;
            }else teams[mp[b]].scores+=3;

        }

        sort(teams,teams+n,cmp);
        sort(teams,teams+n/2,cmp2);
        
        
        for(int i=0;i<n;i++){    	
            cout<<teams[i].name<<endl;
            
            //printf(" %d %d %d\n",teams[i].scores,teams[i].wingoals,teams[i].goals);
        }
    }
}


2017-09-16 17:03

是输入所有测试数据再输出答案,还是输入一组数据就输出该组答案?或者两种都可以?

2017-09-16 17:03
3

有多少人是0%的。。。

2017-09-16 17:03

JS做的,控制台调试也没问题...不知道是不是输入用法错了

var n = +read_line();
while (n > 0) {
	var result = {};
	var printedResult = [];
	var l = n;
	while (l--) {
		var team = read_line();
		result[team] = {
			point: 0,
			shoot: 0,
			lose: 0,
			sum: 0
		};
	}

	var number = n * (n - 1) / 2;
	while (numer--) {
		var str = read_line();
		computeResult(str, result);
	}
	for (var i in result) {
		printedResult.push(result[i].sum);
	}
	printedResult.sort(function(a, b){
		return b.slice(0, -1) - a.slice(0, -1)
	}) //排序
	var printedArr = [];
	for (var i = 0; i < printedResult.length / 2; i++) {
		printedArr.push(printedResult[i].slice(-1))
	}
	printedArr.sort();
	for(var i = 0; i < printedArr.length;i++) {
		print(printedArr[i])
	}
	n = +read_line();
}


function computeResult(str, obj) {
	var a = str[0];
	var b = str[2];
	var c = +str[4]; //分数A
	var d = +str[6]; //分数B
	obj[a].shoot += c;
	obj[a].lose += d;
	obj[b].shoot += d;
	obj[b].lose += c;
	if (c > d) {
		obj[a].point += 3;
	} else if (c == d) {
		obj[a].point += 1;
		obj[b].point += 1;
	} else {
		obj[b].point += 3;
	}
	obj[a].sum = '' + obj[a].point + obj[a].shoot + obj[a].lose + a; // '555A'
	obj[b].sum = '' + obj[b].point + obj[b].shoot + obj[b].lose + b; // '424B'
}


2017-09-16 17:04

这个题难点就是怎么判断没有输入了吧,反正我不知道怎么搞,基本都会一直卡在读入哪里。

2017-09-16 17:04

哪里还能再交?


2017-09-16 17:04

真的,有毒


2017-09-16 17:04
coder_SNFEADM2 回复 coder_XFBTXFN2

2017-09-16 17:04
coder_7SC8PFAS 回复 coder_YA5SNZJQ

我也是0,这题太白痴了,估计是最后统一输出。。。

2017-09-16 17:05

本地调出来,没有任何问题,上传后,0%...................................

2017-09-16 17:05

这题应该有问题。。同样0%。。这么简单的一个水题感觉。。

2017-09-16 17:05
coder_yYpE1vJ5 回复 coder_SNFEADM2

一样

2017-09-16 17:05

同十楼,卡在最后一行的输入

2017-09-16 17:05

球赛有没有人通过的,我的一直是0%=_=

2017-09-16 17:06

怀疑测试数据中存在N=1(奇数)的情况  过了0.4

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
const int MAXN = 55;
struct Node {
    string nm;
    int sc1;
    int sc2;
    int sc3;
}G[MAXN];
int N = 0;
bool cmp1(const Node & P,const Node & T) {
    if (P.sc1 == T.sc1) {
        if (P.sc2 == T.sc2) return T.sc3 < P.sc3;
        else return T.sc2 < P.sc2;
    }
    else return T.sc1 < P.sc1;
}

bool cmp2(const Node & P,const Node & T) {
    return P.nm < T.nm;
}

inline void myINPUTstr(string & s1,string & s2) {
	string ss = "";
	s1 = "" , s2 = "";
    bool ok = true;
    cin >> ss;
    for (size_t i = 0; i < ss.length(); ++ i) {
        if (ss[i] == '-') ok = false;
        else {
        	if (ok) s1.push_back(ss[i]);
            else s2.push_back(ss[i]);
        }
    }
}

//inline void myINPUTnum(int & x1,int &x2)

void u0() {
    map<string,int> mp;
    mp.clear();
    string str = "";
    for (int i = 1; i <= N; ++ i) {
        cin >> str;
        mp[str] = i;
        G[i].nm = str;
        G[i].sc1 = G[i].sc2 = G[i].sc3 = 0;
    }
    string s1 = "", s2 = "";
    char cc = 'a';
    int x1 = 0, x2 = 0, ix = 0, iy = 0;
    for (int i = 1; i <= N; ++ i) {
        for (int j = i+1; j <= N; ++ j) {
            myINPUTstr(s1,s2);
            cin >> x1 >> cc >> x2;
            ix = mp[s1], iy = mp[s2];
            G[ix].sc2 += (x1-x2);
            G[iy].sc2 += (x2-x1);
            G[ix].sc3 += x1;
            G[iy].sc3 += x2;
            if (x1 > x2) {
            	G[ix].sc1 += 3;
            }
            else if (x1 == x2) {
                G[ix].sc1 += 1, G[iy].sc1 += 1;
            }
            else {
                G[iy].sc1 += 3;
            }
        }
    }
    ix = ((N+1)/2) + 1;
    sort(G+1,G+N+1,cmp1);
    for (int i = 1; i <= N; ++ i) {
  //      cout << G[i].nm << " " << G[i].sc << endl;
    }
    sort(G+1,G+ix,cmp2);
    for (int i = 1; i < ix; ++ i) {
        cout << G[i].nm << endl;
    }
}

int main() {
    while (cin >> N) {
        u0();
    }
    return 0;
}

/*

4
A
B
C
D
A-B 1:1
A-C 2:2
A-D 1:0
B-C 1:0
B-D 0:3
C-D 0:3
2
a
A
a-A 2:1
*/


2017-09-16 17:06
张胜东 回复 coder_7SC8PFAS

本来就是最后统一输出,题目都说了,进入淘汰赛的再按字母排序

2017-09-16 17:07

0% 无解了~

本地调试

2017-09-16 17:07

语言不熟,真的伤不起。。。就感觉对C++还记得些,不过提笔忘了99.9999999%。。。

2017-09-16 17:07

本地IDE直接把全部测试用例输进去,跑出来的结果一样,结果赛马AC0。

最开始是按顺序,D比A成绩好是D A,还改成A D了,依然AC0。

public class KDTwo {
   public static void main(String args[])
   {
      Scanner scanner = new Scanner(System.in);
      while (scanner.hasNext()) {
         int n = scanner.nextInt();
         Map<String,Team> map = new HashMap<>();
         for (int i = 0; i < n; i++) {
            String tName = scanner.next();
            map.put(tName,new Team(tName));
         }
         int allCom = n * (n-1) / 2;
         for (int i = 0; i < allCom; i++) {
            String strTeam = scanner.next();
            String strScore = scanner.next();
            solveStr(strTeam,strScore , map);
         }
         ArrayList<Team> rsList = getAnswer(map);
         ArrayList<Team> indexList = getAnswer(map);
         for (int i = 0; i < rsList.size(); i++) {
            indexList.add(rsList.get(i));
         }

         Collections.sort(indexList);
         int num = indexList.get(indexList.size()/2).result;

         for (int i = 0; i < rsList.size(); i++) {
            if(rsList.get(i).result > num) {
               System.out.println(rsList.get(i).name);
            }
         }
      }
   }

   public static ArrayList getAnswer(Map<String,Team> map) {
      ArrayList<Team> list = new ArrayList<>();
      Iterator iter = map.entrySet().iterator();
      while (iter.hasNext()) {
         Map.Entry entry = (Map.Entry) iter.next();
         list.add((Team)entry.getValue());
      }
      return list;
   }

   public static void solveStr(String str1,String str2,Map<String,Team> map) {
      String[] teamIn = str1.split("-");
      String[] goalIn = str2.split(":");
      Team team1 = map.get(teamIn[0]);
      Team team2 = map.get(teamIn[1]);
      if(Integer.parseInt(goalIn[0]) > Integer.parseInt(goalIn[1])) {
         team1.result = team1.result + 3;
         team2.result = team2.result + 0;
      } else if(Integer.parseInt(goalIn[0]) == Integer.parseInt(goalIn[1])) {
         team1.result = team1.result + 1;
         team2.result = team2.result + 1;
      } else {
         team1.result = team1.result + 0;
         team2.result = team2.result + 3;
      }
      team1.goal += Integer.parseInt(goalIn[0]);
      team1.goalEd += Integer.parseInt(goalIn[1]);
      team2.goal += Integer.parseInt(goalIn[1]);
      team2.goalEd += Integer.parseInt(goalIn[0]);
   }


}

class Team implements Comparable<Team>{
   public String name;
   public int result; //分数
   public int goal;   //进球数
   public int goalEd; //被进球数

   public Team() {
   }

   public Team(String name) {
      this.name = name;
   }

   public int getGoalDiff() {
      return (goal - goalEd);
   }
   public int getGoal(){
      return goal;
   }

   [@Override](/user/Override)
   public int compareTo(Team team) {
      if(this.result > team.result) {
         return -1;
      } else if(this.result < team.result) {
         return 1;
      } else {
         if( (this.getGoalDiff()) > (team.getGoalDiff())) {
            return -1;
         } else if( (this.getGoalDiff()) < (team.getGoalDiff())) {
            return 1;
         } else {
            if(this.getGoal() > team.getGoal()) {
               return -1;
            } else {
               return 1;
            }
         }
      }
   }
}


2017-09-16 17:07
1
coder_LzPAu4Uf 回复 coder_FJ68NAJB

我也用js写的…本地调试没问题但是提交了就一直是10%

2017-09-16 17:08

希望本题能开放测试用例  

本地测试没有问题  一直提示没有0%  

真的有毒

2017-09-16 17:08
coder_X75PX4QD 回复 coder_XFBTXFN2

我用的C,样例输出跟你一样,20

2017-09-16 17:09
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;

struct Score
{
	string s;
	int score, count1, count2;
	Score()
	{
		score = 0;
		count1 = 0;
		count2 = 0;
	}
};

bool cmp(Score A, Score B)
{
	if (A.score == B.score)
	{
		if (A.count1 == B.count1)
			return A.count2 > B.count2;
		else
			return A.count1 > B.count1;
	}
	else
		return A.score > B.score;
}
int main()
{
	int n;
	while (cin >> n)
	{
		map<string, int> m;
		string str;
		for (int i = 0; i < n; i++)
		{
			cin >> str;
			m[str] = i;
		}
		string s1, s2;
		int num1, num2;
		char ch;
		vector<Score> vec(n);
		for (int i = 0; i < n*(n - 1) / 2; i++)
		{
			cin >> s1 >> num1 >> ch >> num2;
			s2 = s1.substr(s1.find('-') + 1);
			s1 = s1.substr(0, s1.find('-'));
			if (num1 > num2)
			{
				vec[m[s1]].s = s1;
				vec[m[s2]].s = s2;
				vec[m[s1]].score += 3;
				vec[m[s1]].count1 += (num1 - num2);
				vec[m[s1]].count2 += num1;
				vec[m[s2]].count2 += num2;
			}
			else if (num1 < num2)
			{
				vec[m[s1]].s = s1;
				vec[m[s2]].s = s2;
				vec[m[s2]].score += 3;
				vec[m[s2]].count1 += (num2 - num1);
				vec[m[s2]].count2 += num2;
				vec[m[s1]].count2 += num1;
			}
			else
			{
				vec[m[s1]].s = s1;
				vec[m[s2]].s = s2;
				vec[m[s1]].score += 1;
				vec[m[s2]].score += 1;
				vec[m[s2]].count2 += num2;
				vec[m[s1]].count2 += num1;
			}
		}
		sort(vec.begin(), vec.end(), cmp);
		vector<string> res;
		for (int i = 0; i < n / 2; i++)
		{
			res.push_back(vec[i].s);
		}
		sort(res.begin(), res.end());
		for (int i = 0; i < res.size(); i++)
		{
			cout << res[i] << endl;
		}
	}
	return 0;
}


2017-09-16 17:09
1
coder_K5CRWHMA 回复 coder_XFBTXFN2

2017-09-16 17:11

我通过了

#include<map>
#include<string>
#include<iostream>
#include<cstdio>
#include<vector>
#include<set>
#include<cstring>
using namespace std;

int main() {
	int n;
	while (scanf("%d", &n)!=EOF) {
		map<string, vector<int>>sc;//score,num
		map<vector<int>, string>sc2n;
		set<string> ans;
		char name[50] = { '\0' };
		for (int i = 0; i < n; i++) {
			scanf("%s",name);
			sc[name] = { 0,0,0 };
		}
		int line_num = n*(n - 1) / 2;
		char name1[100] = { '\0' };
		char name2[100] = { '\0' };
		int num1 = 0;
		int num2 = 0;
		for (int i = 0; i < line_num; i++) {
			scanf("%s %d:%d", name1, &num1, &num2);
			int sep = (int)(strchr(name1, '-') - name1);
			strcpy(name2, name1 + sep + 1);
			name1[sep] = '\0';

			if (num1 == num2) {
				sc[name1][0]++;
				sc[name2][0]++;
			}
			else if (num1 > num2) {
				sc[name1][0] += 3;
				sc[name1][1] += num1-num2;
			}
			else {
				sc[name2][0] += 3;
				sc[name2][1] += -(num1 - num2);
			}

			sc[name1][2] += num1;
			sc[name2][2] += num2;
		}
		for (auto &e : sc) sc2n[e.second] = e.first;
		int cnt = 0;
		auto rit = sc2n.rbegin();
		for (; rit != sc2n.rend();rit++) {
			auto &e = *rit;
			ans.insert(e.second);
			cnt++;
			if (cnt == n / 2) break;
		}
		for(auto &e:ans) printf("%s\n", e.c_str());
	}

	return 0;
}

%0,不知道为啥

2017-09-16 17:12

AC 0

2017-09-16 17:13

有没有使用Java处理好输入输出的,能不能贴一下代码?

2017-09-16 17:18
2
coder_Si4zJQJR 回复 coder_XFBTXFN2

+1

2017-09-16 17:22

没做完,分享下自己的思路吧

import java.util.Scanner;

public class Main {

    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();//球队数
        doi(n,sc);
        while(sc.hasNext())
        	doi(n,sc);
        sc.close();
    }
    public static void doi(int n,Scanner sc){
    	team[] sdui = new team[n];
    	for(int i=0;i<n;i++){
    		sdui[i].name = sc.next();
    	} 
    	for(int i=0;i<n*(n-1)/2;i++){
    		String s = sc.nextLine();
    		String[] st = s.split(" ");
    		String tA = st[0].split("-")[0];
    		String tB = st[0].split("-")[1];
    		int numA = Integer.parseInt(st[1].split(":")[0]);
    		int numB = Integer.parseInt(st[1].split(":")[1]);
    		int ta= findteam(sdui,tA);
    		int tb = findteam(sdui,tB);
    		if(numA>numB){
    			sdui[ta].countA+=3;
    		}else if(numA==numB){
    			
    		}else{
    			
    		}
    	}
    }
    public static int findteam(team[] sdui,String name){
    	for(int i=0;i<sdui.length;i++){
    		if(sdui[i].name.equals(name))
    			return i;
    	}
    	return -1;
    }
    class team{
    	String name;
    	int countA;
    	int countB;
    	int countC;
    }
}


2017-09-16 17:26
郝平没了 回复 acmcoderKZJg1x7e

你的compareTo的写法有问题,不应该只返回+-1 需要返回积分或净胜球的差*相应权值。

你的写法跑下面这个测试用例会出问题,你可以试试

4

A

B

C

D

A-B 4:0

A-C 2:2

A-D 1:1

B-C 1:0

B-D 0:0

C-D 5:0


2017-09-16 17:42

import java.util.*;


/**

 * Created by 78284 on 2017/8/29.

 */

public class Main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        while(in.hasNext()){

            Map<String , Integer> map1 = new LinkedHashMap<>();//进球数

            Map<String , Integer> map2 = new LinkedHashMap<>();//净胜球数

            Map<String , Integer> map3 = new LinkedHashMap<>();//积分

            int n = Integer.parseInt(in.nextLine());

            for(int i = 0 ;i<n;i++){


                String t = in.nextLine();

                map1.put(t,0);

                map2.put(t,0);

                map3.put(t,0);

            }

            for(int i =0 ;i<n*(n-1)/2;i++){

                String str = in.nextLine();

                String [] newstr = str.split(" ");

                String [] front = newstr[0].split("-");

                String [] behind = newstr[1].split(":");

                //存入进球数

                map1.put(front[0],map1.get(front[0])+Integer.parseInt(behind[0]));

                map1.put(front[1],map1.get(front[1])+Integer.parseInt(behind[1]));

                map2.put(front[0],map2.get(front[0])+Integer.parseInt(behind[0])-Integer.parseInt(behind[1]));

                map2.put(front[1],map2.get(front[1])+Integer.parseInt(behind[0])-Integer.parseInt(behind[1]));

                if(behind[0].compareTo(behind[1])>0){

                    map3.put(front[0],map3.get(front[0])+3);

                }else if(behind[0].compareTo(behind[1])==0){

                    map3.put(front[0],map3.get(front[0])+1);

                    map3.put(front[1],map3.get(front[1])+1);

                }else{

                    map3.put(front[1],map3.get(front[1])+3);

                }

            }

            String [] str = new String[100];

            int [] val = new int[100];

            int i=0,y=0;

            for(String key:map3.keySet()){

                str[i++] = key;

                val[y++] = map3.get(key);

            }

            for(int j = 0 ;j<n-1;j++){

                for(int k=0;k<n-j-1;k++){

                    if(val[k]<val[k+1]){

                        int temp = val[k];

                        val[k]=val[k+1];

                        val[k+1]=temp;

                        String s = str[k];

                        str[k]=str[k+1];

                        str[k+1] = s;

                    }else if(val[k]==val[k+1]){

                        if(map2.get(str[k])<map2.get(str[k+1])){

                            int temp = val[k];

                            val[k]=val[k+1];

                            val[k+1]=temp;

                            String s = str[k];

                            str[k]=str[k+1];

                            str[k+1] = s;

                        }else if(map2.get(str[k])==map2.get(str[k+1])){

                            if(map1.get(str[k])<map1.get(str[k+1])){

                                int temp = val[k];

                                val[k]=val[k+1];

                                val[k+1]=temp;

                                String s = str[k];

                                str[k]=str[k+1];

                                str[k+1] = s;

                            }

                        }

                    }

                }

            }

            for(i= 0 ;i<n/2-1;i++){

                for(int j= 0 ;j<n/2-1-i;j++){

                    if(str[j].compareTo(str[j+1])>0){

                        String s = str[j];

                        str[j]=str[j+1];

                        str[j+1]= s;

                    }

                }

            }

            for(i=0;i<n/2;i++){

                System.out.println(str[i]);

            }

        }

    }

}


2017-09-16 17:50
coder_7SC8PFAS 回复 张胜东

字幕排序理解 但最后统一输出干毛用。。

2017-09-16 17:56

import java.util.Iterator;

import java.util.Map;

import java.util.Scanner;

import java.util.Set;

import java.util.TreeMap;

import java.util.TreeSet;


public class Mian5 {


public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

while (true) {

int n = Integer.parseInt(scanner.nextLine());

int n1=n*(n-1)/2;

Map<String,Score> map =new TreeMap<String,Score>();

for (int i = 0; i < n; i++) {

String name=scanner.nextLine();

map.put(name, new Score(name));

}

for (int i = 0; i < n1; i++) {

String temp=scanner.nextLine();

String a=temp.substring(0,1);

String b=temp.substring(2,3);

int a1=Integer.parseInt(temp.substring(4,5));

int b1=Integer.parseInt(temp.substring(6,7));

if(a1>b1){

Score score1=map.get(a);

Score score2=map.get(b);

score1.score+=3;

score1.goal+=a1-b1;

score2.goal-=a1-b1;

score1.winTeam.add(b);

}else if(a1<b1){

Score score1=map.get(b);

Score score2=map.get(a);

score1.score+=3;

score1.goal+=b1-a1;

score2.goal-=b1-a1;

score1.winTeam.add(a);

}else{

Score score1=map.get(a);

Score score2=map.get(b);

score1.score+=1;

score2.score+=1;

}

}

Set<Score> set = new TreeSet<Score>();

for(String s:map.keySet()){

set.add(map.get(s));

}

Iterator<Score> iterator = set.iterator();

for (int i = 0; i < n/2; i++) {

iterator.next();

}

for (int i = 0; i < n/2; i++) {

System.out.println(iterator.next().teamName);

}


}


}


//4

//A

//B

//C

//D

//A-B 1:1

//A-C 2:2

//A-D 1:0

//B-C 1:0

//B-D 0:3

//C-D 0:3


class Score implements Comparable {

int score = 0;

int goal = 0;

String teamName;

Set<String> winTeam = new TreeSet<String>();

public Score(String name){

teamName=name;

}


[@Override](/user/Override)

public int compareTo(Object o) {

Score s = (Score) o;

if (this.score - s.score != 0) {

return 100*(this.score - s.score);

} else {

if(this.goal-s.goal!=0){

return 10*(this.goal-s.goal);

}else{

if(this.winTeam.contains(s.teamName)){

return 1;

}else{

return-1;

}

}

}

}


}



2017-09-16 18:09

考试结束了,问题找出来了,好气啊!!!

import java.util.HashMap;
import java.util.Scanner;

public class Main {
	
	public static void win(String[] str, String[] _str){
		
		if(str == null || _str == null)
			return;
		HashMap<String, Integer> map = new HashMap<String,Integer>();
		HashMap<String, Integer> map1 = new HashMap<String,Integer>();
		for(int i = 0; i < str.length; i++){
			map.put(str[i], 0);
			map1.put(str[i], 0);
		}
		String[] t1 = new String[_str.length/2];
		String[] t2 = new String[_str.length/2];
		int p = 0, q = 0;
		for(int i = 0; i < _str.length; i++){
			
			if(i % 2 == 0){
				t1[p++] = _str[i];
			}else{
				t2[q++] = _str[i];	
			}
		}
		
		for(int i = 0; i < t1.length; i++){
			String[] temp1 = t1[i].split("-");
			String[] temp2 = t2[i].split(":");
			map1.put(temp1[0], map1.get(temp1[0]) + Integer.parseInt(temp2[0]));
			map1.put(temp1[1], map1.get(temp1[1]) + Integer.parseInt(temp2[1]));
			int num = Integer.parseInt(temp2[0]) - Integer.parseInt(temp2[1]);
			if(num > 0){
				map.put(temp1[0], map.get(temp1[0]) + 3);
			}else if(num < 0){
				map.put(temp1[1], map.get(temp1[1]) + 3);
			}else{
				map.put(temp1[0], map.get(temp1[0]) + 1);
				map.put(temp1[1], map.get(temp1[1]) + 1);
			}
		}
		
		int count = 0;
		String lastname = "";
		for(String name : map.keySet()){
			int temp = map.get(name);
			if(temp > count){
				count = temp;
				lastname = name;
			}else if(temp == count){
				if(map1.get(lastname) < map1.get(name))
					lastname = name;
			}
		}
		System.out.println(lastname);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		String[] str = new String[n];
		for(int i = 0; i < n; i++){
			str[i] = in.next();
		}
		String[] _str = new String[n * (n - 1)];
		for(int j = 0; j < _str.length; j++){
			_str[j] = in.next();
		}
		win(str,_str);
	}
}


2017-09-16 18:42
coder_GXFE7WJP 回复 Cherie

你的通过了吗?我写的代码基本上和你一模一样。。。

struct team
{
    string name;
    int score = 0;
    int win = 0;
    int in = 0;
};

struct {     bool operator()(team a, team b)     {         if (a.score > b.score)             return true;         else if (a.score == b.score)         {             if (a.win > b.win)                 return true;             else if (a.win == b.win)                 return a.in > b.in;             else                 return false;         }         else             return false;     } } customMore;

int main() {     std::ios::sync_with_stdio(false); //    freopen("/Users/xuxu/Desktop/input.in", "r", stdin); //    freopen("/Users/xuxu/Desktop/output.out", "w", stdout);

    int n;     while (cin >> n)     {         map<string, team> teams;         int m = (n * (n - 1)) / 2;         for (int i = 0; i < n; ++i)         {             string name;             cin >> name;             teams[name].score = 0;         }

        for (int i = 0; i < m; ++i)         {             char ch;             string name, name1, name2;             int in1, in2;

            cin >> name >> in1 >> ch >> in2;             for (int j = 0; j < name.size(); ++j)             {                 if (name[j] == '-')                 {                     name1 = name.substr(0, j);                     name2 = name.substr(j + 1);                     break;                 }             }

            teams[name1].name = name1;             teams[name2].name = name2;             teams[name1].in += in1;             teams[name2].in += in2;

            if (in1 > in2)             {                 teams[name1].score += 3;                 teams[name1].win += in1 - in2;             }             else if (in1 < in2)             {                 teams[name2].score += 3;                 teams[name2].win += in2 - in1;             }             else             {                 teams[name1].score += 1;                 teams[name2].score += 1;             }         }

        vector<team> vec;         for (auto &item : teams)             vec.push_back(item.second);         sort(vec.begin(), vec.end(), customMore);

        vector<string> ss;         for (int i = 0; i < vec.size() / 2; ++i)         {             ss.push_back(vec[i].name);         }         sort(ss.begin(), ss.end());         for (string s: ss)         {             cout << s << endl;         }     }

    return 0; }


2017-09-16 18:52

这道题感觉有毒啊。明明很水的一个题,怎么呢都是0%,气人

2017-09-16 18:53
coder_YA5SNZJQ 回复 coder_7SC8PFAS

啥统一输出?所有测例输入完了再统一输出?

2017-09-16 19:25
coder_56MFXAFC 回复 coder_XFBTXFN2

同,难道只能统一最后输出。。

2017-09-16 19:27
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

public class Main2 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);

		while (in.hasNextInt()) {
			int n = in.nextInt();
			in.nextLine();
			HashMap<String, Integer> map = new HashMap<String, Integer>();
			for (int i = 0; i < n; i++) {
				String tmp = in.nextLine();
				map.put(tmp, 0);
			}
			for (int k = 0; k < n * (n - 1) / 2; k++) {
				String[] tmp = in.nextLine().split(" ");
				String[] groups = tmp[0].split("-");
				String[] counts = tmp[1].split(":");
				if (Integer.valueOf(counts[0]) == Integer.valueOf(counts[1])) {
					map.put(groups[0], map.get(groups[0]) + 1);
					map.put(groups[1], map.get(groups[1]) + 1);
				} else if (Integer.valueOf(counts[0]) > Integer
						.valueOf(counts[1])) {
					map.put(groups[0], map.get(groups[0]) + 3);
				} else {
					map.put(groups[1], map.get(groups[1]) + 3);
				}
			}
			List<Map.Entry<String, Integer>> entryList = new ArrayList<Map.Entry<String, Integer>>(
					map.entrySet());
			Collections.sort(entryList,
					new Comparator<Map.Entry<String, Integer>>() {
						[@Override](/user/Override)
						public int compare(Entry<String, Integer> o1,
								Entry<String, Integer> o2) {
							// TODO Auto-generated method stub
							return (o2.getValue() - o1.getValue());
						}
					});

			ArrayList<String> list = new ArrayList<String>();

			for (int i = 0; i < n / 2; i++) {
				list.add(entryList.get(i).getKey());
			}
			Collections.sort(list);
			for (int i = 0; i < list.size(); i++) {
				System.out.println(list.get(i));
			}

		}
	}
}

0%

2017-09-16 19:38

import java.util.*;

public class KE1 {

public static void main(String[] args){

Scanner in =new Scanner(System.in);

while(in.hasNext()){

int n=in.nextInt();

String[] str =new String[n];

for(int i=0;i<n;i++){

str[i]=in.next();

}

String[] score =new String[n*(n-1)/2];

in.nextLine();

//System.out.println(n*(n-1)/2);

for(int j=0;j<(n*(n-1)/2);j++){

score[j]=in.nextLine();

}

game(str,score);

}

}

public static void game(String[] name,String[] core){

int n =name.length;

int[] rec =new int[n];

//int[] small = new int[7];

for(int i=0;i<core.length;i++){

String ss =core[i];

int c5 =Integer.parseInt(String.valueOf(ss.charAt(4)));

int c7=Integer.parseInt(String.valueOf(ss.charAt(6)));

if(c5>c7){

rec[find(name, ss.charAt(0))]+=3;

}else if(c5==c7){

//System.out.println(ss.charAt(0));

rec[find(name, ss.charAt(0))]+=1;

rec[find(name, ss.charAt(2))]+=1;

}else{

rec[find(name, ss.charAt(2))]+=3;

}

}

List<Integer> list = new ArrayList<>();

for(int j=0;j<n;j++){

list.add(rec[j]);

}

Collections.sort(list);

TreeSet<String> set =new TreeSet<>();

int len =list.size();

for(int p=0;p<(n/2);p++){

for(int k=0;k<n;k++){

if(rec[k]==list.get(len-1)){

set.add(name[k]);

len--;

break;

}

}

}

for(String c:set){

System.out.println(c);

}


}

public static  int find(String[] n,char s1){

for(int i=0;i<n.length;i++){

char ch =n[i].charAt(0);

if(s1==ch) return i;

}

return 0;

}


}


2017-09-16 20:22

我也觉得需要自定义一个类 跟楼上的想法小类似


2017-09-16 21:06
package xiaozhao;

import java.util.*;

public class t3 {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());
        Map<String, team> map = new HashMap<String, team>();
        for (int i = 0; i < n; i++) {
            String name = scanner.nextLine();
            team t = new team(name);
            map.put(name, t);
        }
        for (int i = 0; i < (n * (n - 1) / 2); i++) {
            String str = scanner.nextLine();
            String s[] = str.split(" ");
            String name1 = s[0].split("-")[0];
            String name2 = s[0].split("-")[1];
            team t1 = map.get(name1);
            team t2 = map.get(name2);
            String scores[] = s[1].split(":");
            int s1 = Integer.parseInt(scores[0]);
            int s2 = Integer.parseInt(scores[1]);
            t1.setGoal(t1.getGoal() + s1);
            t2.setGoal(t2.getGoal() + s2);
            t1.setWin(t1.getWin() + s1 - s2);
            t2.setWin(t2.getWin() + s2 - s1);
            if (s1 > s2) {
                t1.setScore(t1.getScore() + 3);
            } else if (s1 == s2) {
                t1.setScore(t1.getScore() + 1);
                t2.setScore(t2.getScore() + 1);
            } else {
                t2.setScore(t1.getScore() + 3);
            }
        }
        team arr[] = new team[n];
        int k = 0;
        for (String str : map.keySet()) {
            arr[k++] = map.get(str);
        }
        Arrays.sort(arr);
        for (int i = arr.length-1; i>=arr.length/2; i--) {
          System.out.println(arr[i].getName());
        }
    }
}
package xiaozhao;

import java.util.Comparator;

public class team implements Comparable {
    private String name;
    private int score;
    private int goal;
    private int win;

    public team(String name, int score, int goal) {
        this.name = name;
        this.score = score;
        this.goal = goal;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getGoal() {
        return goal;
    }

    public int getWin() {
        return win;
    }

    public void setWin(int win) {
        this.win = win;
    }

    public void setGoal(int goal) {
        this.goal = goal;
    }

    public team(String name) {
        this.name = name;
        this.goal=0;
        this.score=0;
        this.win=0;
    }

    public int compareTo(Object o) {
        team t = (team) o;
        if (this.score > t.score) {
            return 1;
        } else if (this.score == t.score) {
            if (this.win == t.win) {
                return this.goal > t.goal ? 1 : -1;            }
            return this.win > t.win ? 1 : -1;
        } else {
            return -1;
        }
    }
}


2017-09-16 21:49
acmcoderKZJg1x7e 回复 郝平没了

这块只是排序,我试了你的用例,排序出来的结果是没错的,ACBD。这个compareTo就是根据题设,现根据分数判定,如果分数无法决定排序就根据净胜球排序,如果还是无法排序就继续根据各自的进球数排序。这段只是为了根据题设的大小来对List里的Team进行排序。经过检验,我发现应该是对排序之后的结果输出有问题。

2017-09-16 23:34
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

public class MyFootball {

    public static void main(String[] args) {
        
        Scanner in = new Scanner(System.in);
        
        while(in.hasNext()){
            Map<String,Integer> score = new HashMap<String,Integer>();//存放分数
            Map<String,Integer> winNumber = new HashMap<String,Integer>();//存放净胜球数
            Map<String,Integer> number = new HashMap<String,Integer>();//存放进球数
            
            int count = Integer.parseInt(in.nextLine());
            
            for(int i = 0;i<count;i++){
                String t = in.nextLine();
                score.put(t, 0);
                winNumber.put(t, 0);
                number.put(t, 0);
            }
            
            for(int i =0;i<count*(count-1)/2;i++){
                String[] str = in.nextLine().split(" ");
                String[] pre = str[0].split("-");
                String[] scores = str[1].split(":");
                int score1 = Integer.parseInt(scores[0]);
                int score2 = Integer.parseInt(scores[1]);
                
                winNumber.put(pre[0], winNumber.get(pre[0])+score1-score2);
                winNumber.put(pre[1], winNumber.get(pre[1])+score2-score1);
                number.put(pre[0], number.get(pre[0])+score1);
                number.put(pre[1], number.get(pre[1])+score2);
                if(score1>score2){
                    score.put(pre[0], score.get(pre[0])+3);
                }
                else if(score1<score2){
                    score.put(pre[1], score.get(pre[1])+3);
                }
                else{
                    score.put(pre[1], score.get(pre[1])+1);
                    score.put(pre[0], score.get(pre[0])+1);
                }
                
            }
            
            //给list排序
            List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(score.entrySet());
            
            for(int i =0;i<list.size();i++){
                for(int j =i+1;j<list.size();j++){
                    if(list.get(i).getValue()>list.get(j).getValue()){
                        Entry temp = list.get(i);
                        list.set(i, list.get(j));
                        list.set(j,temp);
                    }
                    else if(list.get(i).getValue()==list.get(j).getValue()){
                        if(winNumber.get(list.get(i).getKey())>winNumber.get(list.get(j).getKey())){
                            Entry temp = list.get(i);
                            list.set(i, list.get(j));
                            list.set(j,temp);
                        }
                    }
                }
            }
            
            for(int i = count/2;i<count;i++){
                System.out.println(list.get(i).getKey());
            }
        }
    }
}


2017-09-17 15:00
#include <iostream>
#include <vector>
#include <map>
#include <set>

using namespace std;

class groupInfo {
public:
    groupInfo() {};
    groupInfo(char c):groupName_m(c), score_m(0), gd_m(0), gi_m(0) {}
    groupInfo(char c, vector<int> info):groupName_m(c), score_m(info[0]), gd_m(info[1]),gi_m(info[2]) {}
    const char getGroupName() const {
        return groupName_m;
    }
    const int getScore() const {
        return score_m;
    }
    void addScore(int score) {
        score_m += score;
    }
    const int getGd() const {
        return gd_m;
    }
    void addGd(int gd) {
        gd_m += gd;
    }
    const int getGi() const {
        return gi_m;
    }
    void addGi(int gi) {
        gi_m += gi;
    }
    void addInfo(vector<int> info) {
        addScore(info[0]);
        addGd(info[1]);
        addGi(info[2]);
    }
    bool operator>(const groupInfo & gI) const {
        if(score_m > gI.getScore()) {
            return true;
        }
        else if (score_m == gI.getScore()) {
            if(gd_m > gI.getGd())
                return true;
            else if(gd_m == gI.getGd()) {
                if(gi_m > gI.getGi())
                    return true;
            }
        }
        return false;
    }
    
private:
    char groupName_m;
    int score_m;
    int gd_m;
    int gi_m;
};

vector<vector<int>> figureInfo(const string& str) {
    vector<vector<int>> VV;
    vector<int> V0;
    vector<int> V2;
    if(str[0] == str[2]) {
        V0.push_back(1);
        V2.push_back(1);
    }
    else if(str[0] > str[2]) {
        V0.push_back(3);
        V2.push_back(0);
    }
    else {
        V0.push_back(0);
        V2.push_back(3);
    }
    V0.push_back(str[0] - str[2]);
    V2.push_back(str[2] - str[0]);
    V0.push_back(str[0]);
    V2.push_back(str[2]);
    VV.push_back(V0);
    VV.push_back(V2);
    return VV;
}

int main(int argc, const char * argv[]) {
    int n;
    while (cin >> n) {
        int vectoryNums = n/2;
        int battalNums = n*(n-1)/2;
        map<char, groupInfo> gnameInfo;
        while (n--) {
            char gName;
            cin >> gName;
            groupInfo group(gName);
            gnameInfo[gName] = group;
        }
        while (battalNums--) {
            string strName, strScore;
            cin >> strName >> strScore;
            vector<vector<int>> VV = figureInfo(strScore);
            gnameInfo[strName[0]].addInfo(VV[0]);
            gnameInfo[strName[2]].addInfo(VV[1]);
        }
        set<groupInfo, greater<groupInfo>> groupInfoSet;
        for(auto v : gnameInfo) {
            groupInfoSet.insert(v.second);
        }
        int i = 0;
        set<char> groupNameSet;
        for(auto v : groupInfoSet) {
            i++;
            if(i > vectoryNums) break;
            groupNameSet.insert(v.getGroupName());
        }
        for(auto v : groupNameSet) {
            cout << v << endl;
        }
    }
    return 0;
}


2017-09-17 21:23
添加回复
回到顶部