結果

問題 No.25 有限小数
ユーザー pessimist
提出日時 2025-06-12 21:28:25
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,061 bytes
コンパイル時間 10,776 ms
コンパイル使用メモリ 216,848 KB
実行使用メモリ 8,044 KB
最終ジャッジ日時 2025-06-12 21:30:01
合計ジャッジ時間 11,809 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 28 WA * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class Solution{
public:
    string frac(long long a, long long b){
        string ans="";
        if((a<0)^(b<0)) ans+='-';
        long long num=abs(a);
        long long den=abs(b);
        ans+=to_string(num/den);
        if(num%den==0) return ans;
        ans+=".";
        unordered_map<int, int> seen;
        for(unsigned long long rem=num%den;rem;rem%=den){
            if(seen.count(rem)){
                ans.insert(ans.begin()+seen[rem], '(');
                ans+=')';
                break;
            }
            seen[rem]=ans.size();
            rem*=10;
            ans+=to_string(rem/den);
        }
        return ans;
    }
}sol;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    long long a, b; cin >> a >> b;
    auto s = sol.frac(a, b);
    if(s.find('(') != string::npos) cout<<-1;
    else{
        auto idx = s.find_last_not_of('0');
        if(idx == string::npos) cout<<'0';
        else cout<<s[idx];
    }
    cout<<endl;
    return 0;
}
0