记录编号 284814 评测结果 AAAAAAAAA
题目名称 [USACO 2.4.5]分数化小数 最终得分 100
用户昵称 GravatarTCtower 是否通过 通过
代码语言 C++ 运行时间 0.012 s
提交时间 2016-07-19 23:38:44 内存使用 1.07 MiB
显示代码纯文本
/*
痛苦缠绕过你
欲望纠缠过你
悲伤击败过你
你的一切都是海滩
我曾经让黑暗的大墙后退
也比欲望走得更远
*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <ctime>
#include <queue>
#include <float.h>
#include <limits.h>
#define LOCAL
const int MAXN = 100000 + 5;
using namespace std;
int vis[MAXN], mark[MAXN];
int a, b, ok, A, B;
int cnt = 0;//count char

void work(){
    ok = 0;
    memset(vis, 0, sizeof(vis));
    memset(mark, 0, sizeof(mark));
    a = a % b;
    while (1){
        if (vis[a]) {mark[a] = ok = 1; break;}
        if (a == 0) {ok = 2; break;}
        vis[a] = 1;
        a = (a * 10) % b;
    }
}
void Count(int n){
    if (cnt == 76) {cnt = 0; printf("\n");}
    while (n != 0) {
        cnt++;
        if (cnt == 76) {cnt = 0; printf("\n");}
        n /= 10;
    }
    return;
}
void print(){
    //Count(A / B);
    //cnt++;//.
    a = A; b = B;
    a = a % b;
    if (ok == 2) {
        while (1){
            if (a == 0) break;
            a = a * 10;
            printf("%d", a / b);
            //Count(a / b);
            a = a % b;
        }
    }else {// ok = 1
        while (mark[a] == 0) {printf("%d", (a * 10) / b); /*Count((a * 10) / b);*/ a = (a * 10) % b;}
        printf("(%d", (a * 10) / b);
        //Count((a * 10) / b);

        a = (a * 10) % b;
        while (!mark[a]){
            printf("%d", (a * 10) / b);
            //Count((a * 10) / b);

            a = (a * 10) % b;
        }
        printf(")");
        //cnt++;
    }
}
int main(){
    #ifdef LOCAL
    freopen("fracdec.in", "r", stdin);
    freopen("fracdec.out", "w", stdout);
    #endif // LOCAL
    scanf("%d%d", &a, &b);//a 分子 b 分母
    A = a; B = b;
    printf("%d.", a / b);
    if (a % b == 0) printf("0");
    else{
        work();
        print();
    }
    return 0;
}