Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:自己的代码一直TLE,求大神帮忙检查一下

Posted by 08122224 at 2021-11-22 00:54:48 on Problem 3982
In Reply To:Re:自己的代码一直TLE,求大神帮忙检查一下 Posted by:08122224 at 2021-11-22 00:45:05
1、先对3个原始数据倒序存储,加的时候能简化很多
2、str不要一直拼接,先申请一块大的能提高效率。
3、两数按位加不需要使用%,最大为19,改用-10能优化

int Add(char *operand1, char *operand2, char *result)
{
    int tempSub = 2 * '0';
    int i = 0;

    for (i = 0; i < MAX_LEN; ++i) {
        result[i] = operand1[i] + operand2[i] + result[i];
        if (result[i] == 0) {
            break;
        }
        if(result[i] >= tempSub) {
            result[i] -= tempSub;
        } else if (result[i] >= '0') {
            result[i] -= '0';
        }
        if (result[i] > 9) {
            result[i+1] += 1;
            result[i] -= 10;
        }
        result[i] += '0';
    }

    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator