시간 초과 문제를 만났다.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String text = in.nextLine();
String[] words = text.split(" ");
double a = Double.parseDouble(words[0]);
double b= Double.parseDouble(words[1]);
double c =Double.parseDouble(words[2]);
int day = 0;
double total=0;
while(a-b<c)
{
day++;
total+=a;
if ( total >=c)
{
break;
}
total-=b;
}
System.out.println(day);
}
} 이건 내가 처음에 시간 초과가 걸렸을때의 코드이다. 지피티에 물어보니 이건 while문이나 for문을 사용하면 자연스럽게 시간초과가 걸리게 만든 문제이므로 수학적 공식을 이용하라고 하였다. 그래서 지피티한테 물어보니
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String text = in.nextLine();
String[] words = text.split(" ");
double a = Double.parseDouble(words[0]);
double b= Double.parseDouble(words[1]);
double c =Double.parseDouble(words[2]);
int day = (int) Math.ceil((c-b) / (a-b));
System.out.println(day);
}
}라는 코드가 나왔다. 여기서는 곱셈,나눗셈,올림을 사용해서 문제를 풀었다. Math.ceil()은 올림이라는 새로운 함수도 알게되었다. 시간이 주어졌다면 while이나 for이 아니라 수학적 공식을 새로 이용해야겠다라는걸 깨달았다/