tje8746   1년 전

다음과 같이 코딩을 했습니다.

예제 입출력은 잘 되고 있으나, 시간초과로 인해 틀렸다고 나옵니다.

while문 때문에 시간초과가 이루어지는것 같은데 이를 해결할 방법이 궁급합니다!

또한, for문도 간략한 코드로 바꿀수 있는지 여줘보고 싶습니다!

rocket2357   1년 전

#include

int positive(int now, int num){

return (now+num)%num;

}

void ballGame(int num, int end, int plus){

int people[num];

people[0] = 1;

for(int i = 1; i < num; i++){

people[i] = 0;

}

int sum = 0;

int max = 1;

int nowPosition = 0;

while(end > max){

if((people[nowPosition] % 2) == 0){

nowPosition += plus;

nowPosition = positive(nowPosition, num);

people[nowPosition] += 1;

} else {

nowPosition -= plus;

nowPosition = positive(nowPosition, num);

people[nowPosition] += 1;

}

if(max < people[nowPosition]){

max = people[nowPosition];

}

sum += 1;

}

printf("%d", sum);

}

int main() {

int N, M, L;

scanf("%d %d %d", &N, &M, &L);

ballGame(N, M , L);

return 0;

}

이런 식으로 고치면 될 것 같습니다.

1. positive 함수를 나머지 연산을 사용해서 좀 더 짧게 만들었습니다.

2. while문 조건 : end>max로 혹시나 max가 한 번에 많이 커질 경우를 대비했습니다.

3. max=1,sum=0로 초기값을 정했습니다.

댓글을 작성하려면 로그인해야 합니다.