【菜鸡求助】第四个数据超时了,改的想哭

#include<iostream>
#include<math.h>
#include<stdio.h>
#pragma warning( disable : 4996)
using namespace std;
long int up = 1, down;
long int a[105] = { 0 };
int n;
int gcd(int a, int b)
{
    if (b > a)
    {
        int temp = a;
        a = b;
        b = temp;
    }
    return b == 0 ? a : gcd(b, a%b);
}
void tofensu(long a[105], int n, int _up,int _down)
{
    if (n > 1)
    {
        up = _up;
        down = _down;
        up += a[n - 2] * down;
        tofensu(a, n - 1, down, up);
    }
}
void to_continue()
{
    int _gcd;
    _gcd = gcd(up, down);
    up /= _gcd;
    down /= _gcd;
    int i = 0;
    while (1)
    {
        a[i] = up / down;
        up %= down;
        if (up == 0)
        {
            cout << "[" << a[0] << "]" << endl;
            return;
        }
        if (up == 1)
        {
            a[i + 1] = down;
            break;
        }
        int temp = up;
        up = down;
        down = temp;
        i++;
    }
    n = i + 2;
    cout << "[" << a[0] << ";";
    int flag = 1;
    for (i = 1; i < n; i++)
    {
        if (flag)
            flag = 0;
        else
            cout << ",";
        cout << a[i];
    }
    cout << "]" << endl;
}
int main()
{
    char s[105];
    int i, j, _gcd;
    while (scanf("%s", s) != EOF)
    {
        if (s[0] == '[')
        {
            for (i = 0; i < 105; i++)
            {
                if (s[i] == ']')
                {
                    n = i;
                    break;
                }
            }
            for (i = 0; 2 * i + 1 < n; i++)
            {
                a[i] = s[2 * i + 1] - '0';
            }
            if (n == 3)
            {
                cout << "[" << a[0] << "]" << endl;
                continue;
            }
            n = i;
            if (n == 1)
            {
                cout << a[0] << endl;
                continue;
            }
            tofensu(a, n, up, a[n - 1]);
            _gcd = gcd(up, down);
            up /= _gcd;
            down /= _gcd;
            cout << up << "/" << down << endl;
        }
        else
        {
            for (i = 0; i < 105; i++)
                a[i] = 0;
            for (i = 0; i < 105 && s[i] != '/'; i++);
            if (i>101)
            {
                for (j = 0; s[j] != '\0'; j++);
                up = 0;
                for (int k = 0; k < j; k++)
                    up += (s[k] - '0')*(int)pow(10, j - k - 1);
                cout << "[" << up << "]" << endl;
                continue;
            }
            up = 0;
            for (j = 0; j < i; j++)
                up += (s[j] - '0')*(int)pow(10, i - j - 1);
            for (j = i + 1; s[j] != '\0'; j++);
            down = 0;
            int t = j - i - 1;
            for (j = 0; j < t; j++)
                down += (s[i + 1 + j] - '0')*(int)pow(10, t - j - 1);
            if (down == 1 && up == 1)
            {
                cout << "[" << 1 << "]" << endl;
                continue;
            }
            if (down == 1)
            {
                cout << "[" << up << "]" << endl;
                continue;
            }
            to_continue();
        }
    }
    return 0;
}

求大佬看看

0 条评论

目前还没有评论...

信息

ID
1696
难度
7
分类
其他 | 数学模拟 点击显示
标签
递交数
5160
已通过
1141
通过率
22%
被复制
3
上传者