The task:
You have some set of tickets. First ticket is numbered M, last - N. M and N have the following limitations: 10,000,000 ≤ M <N ≤ 99999999. We say that the ticket is "lucky", if the sum is the first 4 digits.
(approx. edits) Translation:
You have some set of tickets. The first ticket has the number M, the last - N.
M and N are within: 10,000,000 ≤ M <N ≤ 99999999.
Your task: to find the number of lucky tickets in a given range.
That ticket is called lucky, the sum of the first 4 digits of which is equal to the sum of the last 4 digits.
Input:
Contains the number of the tickets.
At the entrance you have the first and last ticket number (range).
Output:
Total number of "lucky" tickets in given range.
Print the total number of lucky tickets in the given range.
#include <iostream> using namespace std; int lucky(int *a, int *b) { int count = 0, temp, i, res1 = 0, res2 = 0; int var1 = *a; for (i = 0;i < 4;i++) { temp = var1 % 10; var1 /= 10; res1 += temp; } for (i = 0;i < 4;i++) { temp = var1 % 10; var1 /= 10; res2 += temp; } if (res1 == res2) { count++; } return count; } int main() { int a, b; int *aP, *bP; aP = &a; bP = &b; int count2=0,count3=0; cin >> a >> b; for(int i = a;i<=b;i++) { if(lucky(&a,&b)>count2) { count3++; } a++; } cout << count3 << endl; system("pause"); return 0; } I would like to reduce the execution time from 8 seconds to at least 3, with input data 10000000 and 99999999 .