작성일 :

문제 링크

10419번 - 지각

설명

지각 시간과 수업을 일찍 마치는 시간 사이에 제곱 관계가 있습니다. 수업시간이 주어질 때 지각 가능한 최대 시간을 구하는 문제입니다.


접근법

지각 시간을 0부터 증가시키며 지각 시간과 그 제곱의 합이 수업시간 이하인지 확인합니다.

조건을 만족하는 최대값을 출력합니다.



Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;

class Program {
  static void Main() {
    var t = int.Parse(Console.ReadLine()!);
    while (t-- > 0) {
      var d = int.Parse(Console.ReadLine()!);
      var maxLate = 0;
      for (var late = 0; late <= d; late++) {
        if (late + late * late <= d) maxLate = late;
        else break;
      }
      Console.WriteLine(maxLate);
    }
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int t; cin >> t;
  while (t--) {
    int d; cin >> d;
    int maxLate = 0;
    for (int late = 0; late <= d; late++) {
      if (late + late * late <= d) maxLate = late;
      else break;
    }
    cout << maxLate << "\n";
  }

  return 0;
}

Tags: 10419, arithmetic, BOJ, C#, C++, 구현, 백준, 브루트포스, 수학, 알고리즘

Categories: ,