所以基本上我正在为锦标赛写一段代码,我有两个可选的玩家编号和玩家名称,我将如何配对/合并这两个变量?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define CONTESTANTS 16
int main(void)
{
char array[CONTESTANTS][20];
int n;
for(n=0;n<CONTESTANTS;n++)
{
printf("Player %d: ", n+1);
scanf("%s", array[n]);
fflush(stdin);
}
return 0;
}
答案 0 :(得分:1)
使用struct。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define CONTESTANTS 16
typedef struct player {
int number;
char name[20];
} Player;
int main(void)
{
Player array[CONTESTANTS];
int n;
for(n=0;n<CONTESTANTS;n++)
{
printf("Player %d: ", array[n].number = n+1);
scanf("%19s", array[n].name);
}
return 0;
}
答案 1 :(得分:0)
使用结构存储有关参赛者的信息:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define CONTESTANTS_LEN 16
#define NAME_LEN 20
struct Contestant {
int contestant_id;
char contestant_name[NAME_LEN];
};
int main(void)
{
struct Contestant contestants[CONTESTANTS_LEN];
int i;
for(i=0;i<CONTESTANTS_LEN;i++) {
printf("Player %d: ", i+1);
contestants[i].contestant_id = i+1;
scanf("%s", &(contestants[i].contestant_name));
}
return 0;
}