[백준] 15858. Simple Arighmetic 풀이

less than 1 minute read

title image

Problem

15858. Simple Arighmetic

Approach

double로 계산하면 50점
long double로 계산하면 75점
문자열을 parsing해서 한 자리씩 계산하도록 직접 구현하면 100점

Code

/**
 * author: jooncco
 * written: 2021. 1. 21. Thu. 22:26:54 [UTC+9]
 **/

#include <bits/stdc++.h>
using namespace std;
typedef long long     ll;

ll a,b,c;

int main() {

    cin >> a >> b >> c;
    ll n= a*b;
    string ans= to_string(n/c);
    ans.push_back('.');
    for (int i=0; i < 20; ++i) {
        n %= c;
        n *= 10;
        if (n < c) ans.push_back('0');
        else       ans.push_back((char)('0'+n/c));
    }
    cout << ans;
}

Complexity

  • Time: \(O(1)\)
  • Space: \(O(1)\)

Updated:

Leave a comment