简体   繁体   中英

How can i make my code different to remove the time limit?

The question is: There is a strip of tickets with numbers of 8 digits. The first ticket has number M , the last - N . Magnitude M and N meet the following relationship: 10000000 ≤ M < N ≤ 99999999. You are required to determine the number of "lucky" ticket between the given numbers. A ticket is considered "lucky" if the sum of the first four digits equals the sum of the last four digits. And here is my code:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int calcSumDigits(int n)
{
    int sum=0;
    while (n!=0)
    {
        sum+=n%10;
        n/=10;
    }
    return sum;
}
int main(void)
{
    int a,b,cnt=0,x,y;
    cin>>a>>b;
    for (int i=a;i<=b;i++)
    {
        x=i%10000;
        y=(i-x)/10000;
        if (calcSumDigits(x)==calcSumDigits(y)) cnt++;
    }
    cout<<cnt;
    return 0;
}

The results are right but it takes a little bit long time from the program to give the result. For ex when i try from 10000000 to 99999999 the result shows 4379055 but it takes more than 6 seconds

You just need to compare the two sets of sums generated by all the permutations of each half of your numbers - to simplify I rounded the numbers off a bit:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int calcSumDigits(int n)
{
    int sum=0;
    while (n!=0)
    {
        sum+=n%10;
        n/=10;
    }
    return sum;
}
int SlowVersion(int a, int b) {
    int cnt=0,x,y;
    for (int i=a;i<=b;i++)
    {
        x=i%10000;
        y=(i-x)/10000;
        if (calcSumDigits(x)==calcSumDigits(y)) cnt++;
    }
    return cnt;
}
int main()
{
    int lower;
    int upper;
    int original_lower;
    int original_upper;
    cout<<"enter lower:";
    cin>>original_lower;
    cout<<"enter upper:";
    cin>>original_upper;

    lower = original_lower - (original_lower%10000);
    upper = original_upper + (9999 - (original_upper%10000));

    cout<<"to simplify the calculations the lower was changed to:" << lower << endl;
    cout<<"to simplify the calculations the upper was changed to:" << upper << endl;

    int cnt=0;
    const int b=lower%10000;
    const int a=(lower-b)/10000;
    const int b_top=upper%10000;
    const int a_top=(upper-b_top)/10000;
    int a_sums[a_top-a];
    int b_sums[b_top-b];


    int counter = 0;
    for (int i=a;i<=a_top;i++)
    {
        a_sums[counter] = calcSumDigits(i);
        counter++;
    }
    counter = 0;
    for (int x=b;x<=b_top;x++)
    {
        b_sums[counter] = calcSumDigits(x);
        counter++;
    }

    int countera = 0;
    int counterb = 0;
    for (int i=a;i<=a_top;i++)
    {
        counterb = 0;
        for (int x=b;x<=b_top;x++)
        {
            if (a_sums[countera]==b_sums[counterb]) cnt++;
            counterb++;
        }
        countera++;
    }

    cnt = cnt - SlowVersion(lower,original_lower-1);
    cnt = cnt - SlowVersion(original_upper+1,upper);

    cout << "The total \"lucky numbers\" are " << cnt << endl;

    cout << "a is " << a << endl;
    cout << "b is " << b << endl;
    cout << "a_top is " << a_top << endl;
    cout << "b_top is " << b_top << endl;
    system("PAUSE");
    return 0;
}

which with your inputs results in 4379055 (the same result you got) and runs extremely quicker.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM