c - C语言算法——谁是获胜队

标签 c arrays string algorithm data-structures

我需要存储锦标赛的数据。我需要知道有多少支球队将参加比赛(n)以及他们将参加的比赛数量(n!)。然后是球队的名字和成绩。像这样的事情:

Input: 
3 6
TeamX
TeamY
TeamZ
TeamX 0 - TeamY 3
TeamX 1 - TeamZ 0
TeamY 1 - TeamX 0
TeamY 0 - TeamZ 0
TeamZ 0 - TeamX 0
TeamZ 3 - TeamY 1

输出将类似于:

This winner is TeamY, with 7 point(s)
Won 2 game(s), tied 1 game(s) e lost 1 game(s)
Scored 5 goal(s) e suffered 3 goal(s)

编辑2: 这就是我到目前为止所拥有的。但它在 scanf 上不起作用......我无法在球队数量和比赛数量后输入球队名称。你能运行它并尝试理解吗?


指南:我有游戏和团队结构,首先我将团队名称添加到结构的 Teams 数组中,然后将游戏添加到结构的 games 数组中。然后,if/else block 对胜利、失败等进行数学计算,最后查看并打印获胜者。

#include <stdio.h>
#include <string.h>

struct game {
  const char *teamA;
  int scoreA;
  const char *teamB;
  int scoreB;
};

struct team {
  const char *teamA;
  int score;
  int wins;
  int losses;
  int ties;
  int scored;
  int suff;

};

struct team team_new(const char *teamA, int score, int wins, int losses, int ties, int scored, int suff)
{
  struct team t;
  t.teamA = strdup(teamA);
  t.score = score;
  t.wins = wins;
  t.losses = losses;
  t.ties = ties;
  t.scored = scored;
  t.suff = suff;

  return t;
};

struct game game_new(const char *teamA, int scoreA, const char *teamB, int scoreB)
{
  struct game g;
  g.teamA = strdup(teamA);
  g.scoreA = scoreA;
  g.teamB = strdup(teamB);
  g.scoreB = scoreB;
  return g;
};

int main(void)
{

  int i, j, teams, nrgames, biggestScore, whichTeam;

  scanf("Teams and number of games %d %d", &teams, &nrgames);

  //add team names to theTeamss struct
  struct team theTeams[teams];
  size_t num_teams = 0;
  for (i = 0; i < teams; ++i)
  {
    char teamA[20];
    if (scanf("%s", teamA) != 1)
      exit(0);
    theTeams[++num_teams] = team_new(teamA, 0, 0, 0, 0, 0, 0);
  }


  struct game games[nrgames]; //add games
  size_t num_games = 0;
  for (i = 0; i < sizeof games / sizeof *games; ++i)
  {
    char teamA[20], teamB[20];
    int scoreA, scoreB;
    if (scanf(" %s %d - %s %d", teamA, &scoreA, teamB, &scoreB) != 4)
      exit(0);
    games[++num_games] = game_new(teamA, scoreA, teamB, scoreB);
  }


     //run through games[] to change values of theTeams[] scores

  //games - A against B
  for (i = 0; i < sizeof games / sizeof *games; ++i)
  {
    for (j = 0; j < sizeof theTeams / sizeof *theTeams; ++j)
    {
      if ((games[i].teamA == theTeams[j].teamA)) //team(A)
      {
        //if A wins
        if(games[i].scoreA > games[i].scoreB)
        {
          theTeams[j].score += 3;
          theTeams[j].wins += 1;
          theTeams[j].scored = games[i].scoreA;
        }
        //if A loses
        else if (games[i].scoreA < games[i].scoreB)
        {
          theTeams[j].score += 0;
          theTeams[j].losses += 1;
          theTeams[j].suff = games[i].scoreB;
        }
        else //tied
        {
          theTeams[j].score += 1;
          theTeams[j].ties += 1;
          theTeams[j].suff = games[i].scoreA;
        }
      }

      if ((games[i].teamB ==  theTeams[j].teamA))//team(B)
      {
        //if B wins
        if(games[i].scoreB > games[i].scoreA)
        {
          theTeams[j].score += 3;
          theTeams[j].wins += 1;
          theTeams[j].scored = games[i].scoreB;
        }
        //if B loses
        else if (games[i].scoreB < games[i].scoreA)
        {
          theTeams[j].score += 0;
          theTeams[j].losses += 1;
          theTeams[j].suff = games[i].scoreA;
        }
        else //tied
        {
          theTeams[j].score += 1;
          theTeams[j].ties += 1;
          theTeams[j].suff = games[i].scoreB;
        }
      }
    }
  }


  //accessing to the winner team
  biggestScore = theTeams[0].score;
  whichTeam = 0;
  for (i = 0; i < sizeof theTeams / sizeof *theTeams; ++i){
    if (theTeams[i].score > biggestScore){
      biggestScore = theTeams[i].score;
      whichTeam = i;

    }
  }

  //output
  printf("\n This winner is %s, with %d point(s), Won %d game(s), tied %d game(s) and lost %d game(s), Scored %d goal(s) e suffered %d goal(s)\n", theTeams[whichTeam].teamA, theTeams[whichTeam].score, theTeams[whichTeam].wins, theTeams[whichTeam].losses, theTeams[whichTeam].ties, theTeams[whichTeam].scored, theTeams[whichTeam].suff);

  return 0;
}

最佳答案

C语言和字符串没有“问题”;你想做什么,就可以做什么。与其他语言相比,它只是多了一点责任。

你似乎需要一个结构数组,是的。我建议将其建模为一系列所玩的游戏,其中每场游戏都会记录参加的团队及其得分。无需首先记录“可用”球队的列表,之后从游戏数据中提取该列表会更容易。

struct game {
  const char *teamA;
  int scoreA;
  const char *teamB;
  int scoreB;
};

struct game game_new(const char *teamA, int scoreA, const char *teamB, int scoreB)
{
  struct game g;
  g.teamA = strdup(teamA);
  g.scoreA = scoreA;
  g.teamB = strdup(teamB);
  g.scoreB = scoreB;
  return g;
}

然后在 man 程序中:

int main(void)
{
  struct game games[100];
  size_t num_games = 0;
  for (size_t i = 0; i < sizeof games / sizeof *games; ++i)
  {
    char teamA[100], teamB[100];
    int scoreA, scoreB;
    if (scanf(" %s %d - %s %d", teamA, &scoreA, teamB, &scoreB) != 4)
      break;
    games[++num_games] = game_new(teamA, scoreA, teamB, scoreB);
  }
}

关于c - C语言算法——谁是获胜队,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54982642/

相关文章:

ruby - 使用正则表达式在 Ruby 中的单词列表中拆分字符串

php - 如何在 php 中对字符串数组进行排序? [这里的字符串是路径。例如/root/mandy/a.pdf, b.pdf & c.pdf

c - X11 在移动窗口和调整窗口大小时进行干预

javascript - Reactjs,当我删除元素时数组索引发生变化

javascript - 我想从 php 例程中删除一些 if 循环,使代码更加紧凑和动态

Javascript将字符串切成指定长度的 block 存储在变量中

mysql - 如何在插入 MySQL 之前检查字符串长度?

c - 最小 SIMD vector 宽度数据类型

c - C中命令行的参数处理

c - 使用 getch() 将 char 数转换为 int 时出现问题;