본문 바로가기

백준

11564 점프왕 최준민

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cmath>
#include <cstring>
#include <bitset>

#define xx first
#define yy second
#define all(x) (x).begin(), (x).end()

using namespace std;
using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;

int     main()
{
    int k, a, b;
    cin >> k >> a >> b;

    int ans = 0;

    for (int i = a; i <= b; i++)
    {
        if (i % k == 0)
            ans++;
    }
    cout << ans << endl;

    return 0;
}

 

처음에 조건 생각 안 하고 그냥 짜다가 틀렸다

 

a, b, k 전부 10^18이 범위라 시간초과나고 오버플로우나고 난리남

 

그래서 a와 b 사이 몇 개의 값이 들어가는지 따로 계산해서 더해주었다. 

 

a랑 b의 부호가 다르다면 둘 다 양수로 바꿔준 뒤 몫을 더했고

 

 

부호가 같다면 양수로 만들어준 뒤 큰 수 몫에서 작은 수 몫을 뺀다.

 

보면 a가 k로 나눠떨어지는 위치에 있으면 1개를 추가적으로 더 빼게 되므로 이때는 답에 1을 더해준다.

 

근데 틀렸다ㅠ

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cmath>
#include <cstring>
#include <bitset>

#define xx first
#define yy second
#define all(x) (x).begin(), (x).end()

using namespace std;
using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;

int     main()
{
    i64 k, a, b;
    cin >> k >> a >> b;

    if ((a < 0 && b > 0) || (a > 0 || b < 0))
    {
        a = abs(a);
        b = abs(b);

        cout << a / k + b / k + 1;
        return 0;
    }

    a = abs(a);
    b = abs(b);
    if (a > b)
        swap(a, b);

    i64 ans = b / k - a / k;
    if (a % k == 0)
        ans++;
    cout << ans << endl;

    return 0;
}

 

코드는 이럼

 

ㅇㄴ 틀린거 찾음

조건 잘못 적었다. 

 

if ((a < 0 && b > 0) || (a > 0 && b < 0))

 

이렇게 되어야 하는거 잘못 적었네; 피휴

'백준' 카테고리의 다른 글

2217 로프  (0) 2020.09.23
10814 나이순 정렬  (0) 2020.09.21
10821 정수의 개수  (0) 2020.09.21
1107 리모컨 [미완]  (0) 2020.09.20
15900 나무 탈출  (0) 2020.09.20