1
0

More than 3 years have passed since last update.

浮動小数点数の問題をどう回答するか

Last updated at Posted at 2020-06-03

AtcoderのABC169にて素晴らしい問題が出ました。
浮動小数点数に関する問題です。

業務では

業務では「decimal型」を使用しています。
実際の製品開発では処理速度がいらないなら、動作の安定したフレームワーク、ライブラリを使うのが正解です。
ミスを少なく、素早く組めるとは素晴らしいです。
人件費削減=利益率です。

競技プログラミングでは

タイムアタックである以上、直感で組めることは大事です。
これからは「decimal型」がさっと使える言語で回答します。
流行ではpythonかなと思います。
C#はマイクロソフト環境なのでmac使いの自分には向いていないのと求人の数が少ないです。

B - Multiplication 2

問題文はAtCoderを参照してください。

C++
#include<iostream>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<utility>
#include<iomanip>
#include<map>
#include<cmath>
#include<cstdio>

#define rep(i,n) for(int i=0; i<(n); ++i)
#define pai 3.1415926535897932384

using namespace std;
using ll =long long;
using P = pair<int,int>;

#define INF 1000000000000000000
//1000000000000000000
//2147485794483647
int main(int argc, const char * argv[]) {
    ll n;
    cin >> n;

    unsigned long long ans=1;
    rep(i, n){
        unsigned long long m;
        cin >> m;
        if(m==0) ans = 0;
        else if(ans>INF/m) ans=INF+1;
        else ans = ans * m;
    }

    if(ans>INF) cout << -1;
    else cout << ans;

    return 0;
}
python
N = int(input())
A=list(map(int, input().split()))

ans=1
c=0

while c<N:
    if A[c]==0:
        ans=0
    elif ans*A[c]>1000000000000000000:
        ans=1000000000000000001
    else :
        ans=ans*A[c]

    c+=1

if ans>1000000000000000000: print(-1)
else: print(ans)

C - Multiplication 3

問題文はAtCoderを参照してください。

C++
#include<iostream>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<utility>
#include<iomanip>
#include<map>
#include<cmath>
#include<cstdio>

#define rep(i,n) for(int i=0; i<(n); ++i)
#define pai 3.1415926535897932384

using namespace std;
using ll =long long;
using P = pair<int,int>;

int main(int argc, const char * argv[]) {
    ll a;
    string b;
    cin >> a >> b;

    for(ll i=b.size()-1; i>=0; i--){
        if(b[i]=='.'){
            b.erase(i, 1);
        }
    }
    long long bb = stoi(b);
    a = a * bb;

    cout << a / 100;
    return 0;
}
python
from math import floor
from decimal import Decimal

A, B = list(input().split())

ans = int(A) * Decimal(B)

print(floor(ans))
1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0