天天看點

PAT (Basic Level) Practice (中文)1047 程式設計團體賽

1047 程式設計團體賽

程式設計團體賽的規則為:每個參賽隊由若幹隊員組成;所有隊員獨立比賽;參賽隊的成績為所有隊員的成績和;成績最高的隊獲勝。

現給定所有隊員的比賽成績,請你編寫程式找出冠軍隊。

輸入格式:

輸入第一行給出一個正整數 N(≤104​​ ),即所有參賽隊員總數。随後 N 行,每行給出一位隊員的成績,格式為:隊伍編号-隊員編号 成績,其中隊伍編号為 1 到 1000 的正整數,隊員編号為 1 到 10 的正整數,成績為 0 到 100 的整數。

輸出格式:

在一行中輸出冠軍隊的編号和總成績,其間以一個空格分隔。注意:題目保證冠軍隊是唯一的。

輸入樣例:

6

3-10 99

11-5 87

102-1 0

102-3 100

11-9 89

3-2 61

輸出樣例:

11 176

分析

1、對隊伍編号去重

2、隊伍中每個隊員成績求和

3、找出總成績最高的一支隊伍,輸出

代碼:

#include<stdio.h>
int temp[100000];
int t=0;
void fun(int n)
{
  if(t==0) temp[t++]=n;
  else
  {
      int i;
      int l=1;
      for(i=0;i<t;i++)
      {
          if(n==temp[i])
          {
              l=-1;
              break;
          }
      }
      if(l==1)temp[t++]=n;
  }
  return t;
}
struct stu
{
    int dwn;
    int dyn;
    int che;
};
struct stu arr[100000];
struct stu temparr[100000];
int main()
{
    int N;
    scanf("%d",&N);
    int i;
    for(i=0;i<N;i++)
    {
        scanf("%d-%d %d",&arr[i].dwn,&arr[i].dyn,&arr[i].che);
        fun(arr[i].dwn);
    }
    int l=0;
    int j;
    for(i=0;i<t;i++)
    {
        int sum=0;
        for(j=0;j<N;j++)
        {
            if(temp[i]==arr[j].dwn) sum+=arr[j].che;
        }
        temparr[l].dwn=temp[i];
        temparr[l].che=sum;
        l++;
    }
    int index=0;
    for(i=1;i<l;i++)
      if(temparr[i].che>=temparr[index].che) index=i;
    printf("%d %d\n",temparr[index].dwn,temparr[index].che);
    return 0;

}           

複制