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