문제 링크:https://nypc.github.io/2018/2018_online_4.html
코드
더보기
#include <stdio.h>
#include <Windows.h>
#include <string.h>
const char * ItemModeWinTeam(char GameResult[][20]);
const char * SpeedModeWinTeam(char GameResult[][20]);
int TimeAttackToInt(char param1[]);
int main(void) {
int TestCaseCount = 0; //테스트 케이스 갯수
char WinGameTeam[100][5] = { 0 }; //누가 승리했는지 기록하는 배열
scanf("%d", &TestCaseCount);
for (int i = 0; i < TestCaseCount; i++) {
char GameMode[10] = { 0 }; //아이템전인지 스피드전인지
char GameResult[8][20] = { 0 }; //게임 끝나고 타임시간 데이터
scanf("%s", GameMode);
while(getchar() != '\n'){}
for (int j = 0; j < 8; j++) {
//gets_s(GameResult[j], 20);
fgets(GameResult[j], 20, stdin);
}
if (!strcmp(GameMode, "item")) {
strcpy(WinGameTeam[i], ItemModeWinTeam(GameResult));
}
else {
strcpy(WinGameTeam[i], SpeedModeWinTeam(GameResult));
}
}
for (int i = 0; i < TestCaseCount; i++) {
int str = strlen(WinGameTeam[i]);
for (int j = 0; j < str; j++) {
printf("%c", WinGameTeam[i][j]);
}
printf("\n");
}
system("Pause");
}
const char * ItemModeWinTeam(char GameResult[][20]) {
int min = 999999;
int Result = 0;
//타임시간에 상관없이 랜덤이므로 정렬해야함.
for (int i = 0; i < 8; i++) {
int temp = TimeAttackToInt(GameResult[i]);
if (min > temp) {
min = temp;
Result = i;
}
}
if (strstr(GameResult[Result], "blue") == NULL) {
return "red";
}
else {
return "blue";
}
}
const char * SpeedModeWinTeam(char GameResult[][20]) {
int RedTeamCount = 0, BlueTeamCount = 0;
for (int i = 0; i < 8; i++) {
//버블정렬
for (int j = 0; j < 8 - 1; j++) {
int Temp1 = TimeAttackToInt(GameResult[j]);
int Temp2 = TimeAttackToInt(GameResult[j + 1]);
if (Temp1 >= Temp2) {
char StrTemp[20] = { 0 };
strcpy(StrTemp, GameResult[j]);
for (int a = 0; a < 20; a++) {
GameResult[j][a] = '\0';
}
strcpy(GameResult[j], GameResult[j + 1]);
for (int a = 0; a < 20; a++) {
GameResult[j + 1][a] = '\0';
}
strcpy(GameResult[j + 1], StrTemp);
}
}
}
/*printf("버블정렬 후에..");
for (int i = 0; i < 8; i++) {
printf("%s \n", GameResult[i]);
}
*/
char * ptr = strstr(GameResult[0], "blue");
if (ptr == NULL) {
RedTeamCount += 10;
}
else {
BlueTeamCount += 10;
}
int FirstTime = TimeAttackToInt(GameResult[0]);
FirstTime += 1000;
int tema = FirstTime % 10000;
if ((tema / 1000) >= 6 && ((tema % 1000) / 1000) >= 0) {
FirstTime += (10 - (tema / 1000)) * 1000;
}
for (int i = 1; i < 8; i++) {
char * ptr = strstr(GameResult[i], "blue");
int temp1 = TimeAttackToInt(GameResult[i]);
if (FirstTime > temp1) {
if (ptr == NULL) {
//레드일때
switch (i) {
case 0:
RedTeamCount += 10; break;
case 1:
RedTeamCount += 8; break;
case 2:
RedTeamCount += 6; break;
case 3:
RedTeamCount += 5; break;
case 4:
RedTeamCount += 4; break;
case 5:
RedTeamCount += 3; break;
case 6:
RedTeamCount += 2; break;
case 7:
RedTeamCount += 1; break;
}
}
else {
//블루일때
switch (i) {
case 0:
BlueTeamCount += 10; break;
case 1:
BlueTeamCount += 8; break;
case 2:
BlueTeamCount += 6; break;
case 3:
BlueTeamCount += 5; break;
case 4:
BlueTeamCount += 4; break;
case 5:
BlueTeamCount += 3; break;
case 6:
BlueTeamCount += 2; break;
case 7:
BlueTeamCount += 1; break;
}
}
}
else {
break;
}
}
if (RedTeamCount == BlueTeamCount) {
char * ptr = strstr(GameResult[0], "blue");
if (ptr == NULL) {
return "red";
}
else {
return "blue";
}
}
else if (RedTeamCount > BlueTeamCount) {
return "red";
}
else {
return "blue";
}
}
int TimeAttackToInt(char param1[]) {
/*
데이터 양식
red 2:01.12
*/
char Time[6] = { 0 };
char * ptr = strstr(param1, "blue");
if (ptr == NULL) {
ptr = strstr(param1, "red");
Time[0] = *(ptr + 4); //2
Time[1] = *(ptr + 6); //0
Time[2] = *(ptr + 7); //1
Time[3] = *(ptr + 9); //1
Time[4] = *(ptr + 10); //2
Time[5] = '\0';
}
else {
Time[0] = *(ptr + 5); //2
Time[1] = *(ptr + 7); //0
Time[2] = *(ptr + 8); //1
Time[3] = *(ptr + 10); //1
Time[4] = *(ptr + 11); //2
Time[5] = '\0';
}
int temp = atoi(Time);
return temp;
}
댓글