乒乓球竞赛 c++大作业-代写

方案A

2020年要举行新一年的乒乓球比赛了。今年比赛的选手总数([8-16])和选手名单(名字各不相同)在程序运行前通过命令行输入。请按照双败淘汰制随机列出一份初始比赛名单,每场比赛都是3局2胜制,每场比赛的结果在程序运行阶段输入。每轮比赛前,打印出两两对阵名单(相同败率的选手随机安排对局),比赛后要输入各场比赛的结果,然后打印出下轮比赛的名单,直到决出冠亚军为止。最后打印比赛前4名的名字。如果选手数量是单数,可能会轮空,轮空算晋级获胜处理。

程序执行如下:

$ ./game 8 Alice Bob Charlie Dave Ele Flemming Gorge Howard

以上紫色字体是命令行输入的字符串.

请按照题目要求自行设计数据类型和算法,要

#include
#include
int sum_people;
char people[20][20];
int win_people[20], fail_people[20];
int win_sum = 0, fail_sum = 0; 
int now_people_win[20], now_people_fail[20];
int now_people_win_sum = 0, now_people_fail_sum = 0;
int fail_rank[20];
int fail_rank_sum = 0;
int contest(int a, int b) {
	printf("%s和%s比赛开始\n", people[a], people[b]);
	int n = 3;int suma = 0, sumb = 0;
	for(int i = 1;i <= n;++i) {
		printf("第%d次请输入结果:", i);
		int x, y;
		scanf("%d%c%d", &x, &y);
		if(x < 0 || y < 0) {
			printf("比分相同或者输入比分错误该轮不算再比一轮\n");n++;continue;
		} 
		if(x > y) {
			suma++;
		} 	
		else if(x == y) {
			printf("比分相同或者输入比分错误该轮不算再比一轮\n");n++; 
		}
		else {
			sumb++;
		}
	}
	if(suma > sumb) {
		return 1;
	}
	else {
		return -1;
	}
}
int cmp(char a[], char b[]) {
	int da = strlen(a);
	int db = strlen(b);
	if(da != db) {
		return 0;
	}
	for(int i = 0;i < da;++i) {
		if(a[i] == b[i] || a[i] == b[i]-('z'-'a') || a[i] == b[i]+('z'-'a')) {
			
		}
		else {
			return 0;
		}
	}
	return 1;
}
void init() {
    for(int i = 0;i < now_people_win_sum;++i) {
        win_people[i] = now_people_win[i];
    }
    win_sum = now_people_win_sum;
    for(int i = 0;i < now_people_fail_sum;++i) {
        fail_people[i] = now_people_fail[i];
    }
    fail_sum = now_people_fail_sum;
    memset(now_people_fail, 0, sizeof(now_people_fail));
    memset(now_people_win, 0, sizeof(now_people_win));
    now_people_fail_sum = 0, now_people_win_sum = 0;
} 
void print_now() {
    printf("打印现在情况:\n");
    printf("在成功组的有:");
    for(int i = 0;i < win_sum;++i) {
        printf("%s\n", people[win_people[i]]);
    }
    printf("在失败组的有:");
    for(int i = 0;i < fail_sum;++i) {
        printf("%s\n", people[fail_people[i]]);
    }
}
int main() {
	int sum;
	scanf("%d", &sum);
    if(8 < sum || sum > 16) {
        printf("不在题目范围要求退出重新运行程序");return 0;
    }
	for(int i = 0;i < sum;++i) {
		scanf("%s", people[i]);
		for(int j = 0;j < i;++j) {
			if(cmp(people[j], people[i]) == 1) {
				i--;
			}
		}//避免重复数据 
	}
	printf("开始比赛\n");
    printf("第1轮比赛:");
	for(int i = 0;i < sum/2;++i) {
		int h1 = i;
		int h2 = sum/2+i;
		int result = contest(h1, h2);
		if(result == 1) {
			win_people[win_sum] = h1;
			win_sum++;
            fail_people[fail_sum] = h2;
            fail_sum++;
		}
        else if(result == -1){
            win_people[win_sum] = h2;
            win_sum++;
            fail_people[fail_sum] = h1;
            fail_sum++;
        }
	}
    if(sum & 1) {
        win_people[win_sum] = sum;
        win_sum++;
    }//轮不到者当晋级获胜
    int now = 2;
    while(win_sum == 1 && fail_sum == 0) {
        printf("第%d轮比赛:\n");
        if(win_sum != 1) {
            for(int i = 0;i < win_sum/2;++i) {
                int h1 = i;
                int h2 = win_sum/2+i;
                int result = contest(win_people[h1], win_people[h2]);
                if(result == 1) {
                    now_people_win[now_people_win_sum] = win_people[h1];
                    now_people_win_sum++;
                    now_people_fail[now_people_fail_sum] = win_people[h2];
                    now_people_fail_sum++;
                }
                else if(result == -1){
                    now_people_win[now_people_win_sum] = win_people[h2];
                    now_people_win_sum++;
                    now_people_fail[now_people_fail_sum] = win_people[h1];
                    now_people_fail_sum++;
                }
                if(win_sum & 1) {
                    now_people_win[now_people_win_sum] = win_people[win_sum];
                    now_people_win_sum++;
                }//轮不到者当晋级获胜
                init(); 
            }   
        }
        if(fail_sum != 0) {
            for(int i = 0;i < fail_sum/2;++i) {
                int h1 = i;
                int h2 = fail_sum/2+i;
                int result = contest(fail_people[h1], fail_people[h2]);
                if(result == 1) {
                    fail_rank[fail_rank_sum] = fail_people[h2];
                    fail_rank_sum++;
                    now_people_fail[now_people_fail_sum] = fail_people[h1];
                    now_people_fail_sum++;
                }
                else if(result == -1){
                    fail_rank[fail_rank_sum] = fail_people[h1];
                    fail_rank_sum++;
                    now_people_fail[now_people_fail_sum] = fail_people[h2];
                    now_people_fail_sum++;
                }
                if(win_sum & 1) {
                    now_people_win[now_people_win_sum] = win_people[win_sum];
                    now_people_win_sum++;
                }//轮不到者当晋级获胜
                init();
            }
        }
	}
    printf("前4名是:");
    for(int i = sum-5;i < sum;++i) {
        printf("%s\n", people[fail_rank[i]]);
    }  
	return 0;
}
 

求每次运行同样的数据输出的结果不一样。

你可能感兴趣的:(c++)