数组迭代学习

#include <stdio.h>
#include <malloc.h>
#if 1
typedef struct { unsigned int teamId; unsigned int score; unsigned int winNum; unsigned int tieNum; unsigned int lostNum; unsigned int pos;}TeamInfo;TeamInfo teaminfo[1000];unsigned int teamNum = 0;int isInit = 0;void init(){ for (int i = 0; i < 1000; i++) { teaminfo[i].teamId = 0; teaminfo[i].score = 0; teaminfo[i].winNum = 0; teaminfo[i].tieNum = 0; teaminfo[i].lostNum = 0; teaminfo[i].pos = 0; } teamNum = 0;}void swap(TeamInfo *a, TeamInfo *b) //½»“QƒÉ‚€×ƒ”µ{ TeamInfo temp = *a; *a = *b; b = temp;}void updatePos(){ int i, j; for (i = 0; i < teamNum - 1; i++) { int max = i; for (j = i + 1; j < teamNum; j++) if (teaminfo[j].score > teaminfo[max].score) max = j; swap(&teaminfo[max], &teaminfo[i]); if (i == 0) { teaminfo[i].pos = 1; } else { if (teaminfo[i].score == teaminfo[i - 1].score) { teaminfo[i].pos = teaminfo[i - 1].pos; } else { teaminfo[i].pos = i + 1; } } } }void AddGameResult(unsigned int teamId1, unsigned int teamSocre1, unsigned int teamId2, unsigned int teamSocre2){ if (isInit == 0) { isInit = 1; init(); } if (teamId1 == teamId2) { return; } int teampos1 = -1; int teampos2 = -1; for (int i = 0; i < teamNum; i++) { if (teamId1 == teaminfo[i].teamId) { teampos1 = i; } if (teamId2 == teaminfo[i].teamId) { teampos2 = i; } } if (teampos1 == -1) { teampos1 = teamNum; teamNum++; } if (teampos2 == -1) { teampos2 = teamNum; teamNum++; } if (teamSocre1< teamSocre2) { teaminfo[teampos1].lostNum++; teaminfo[teampos1].score += 0; teaminfo[teampos2].winNum++; teaminfo[teampos2].score += 3; } if (teamSocre1 == teamSocre2) { teaminfo[teampos1].tieNum++; teaminfo[teampos1].score += 1; teaminfo[teampos2].tieNum++; teaminfo[teampos2].score += 1; } if (teamSocre1 > teamSocre1) { teaminfo[teampos1].winNum++; teaminfo[teampos1].score += 2; teaminfo[teampos2].lostNum++; teaminfo[teampos2].score += 0; } updatePos();}int GetTeamResult(int teamId, TeamInfo result){ for (int i = 0; i < teamNum; i++) { if (teamId == teaminfo[i].teamId) { result = &teaminfo[i]; return 0; } } result = NULL; return 1;}void clear(){ for (int i = 0; i < teamNum; i++) { teaminfo[i].teamId = 0; teaminfo[i].score = 0; teaminfo[i].winNum = 0; teaminfo[i].tieNum = 0; teaminfo[i].lostNum = 0; teaminfo[i].pos = 0; } teamNum = 0;}int main(){ return 0;}#endif

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值