Counting Pretty Numbers Codechef Solution| Problem Code: NUM239

Counting Pretty Numbers Codechef Solution

Problem

Vasya likes the number 239239. Therefore, he considers a number pretty if its last digit is 22, 33 or 99.

Vasya wants to watch the numbers between LL and RR (both inclusive), so he asked you to determine how many pretty numbers are in this range. Can you help him?

Input

  • The first line of the input contains a single integer TT denoting the number of test cases. The description of TT test cases follows.
  • The first and only line of each test case contains two space-separated integers LL and RR.

Output

For each test case, print a single line containing one integer — the number of pretty numbers between LL and RR.

Constraints

  • 1≤T≤1001≤T≤100
  • 1≤L≤R≤1051≤L≤R≤105

Subtasks

Subtask #1 (100 points): original constraints

Input

2

1 0

11 33

Output

3

8

Explanation

Example case 1: The pretty numbers between 11 and 1010 are 22, 33 and 99.

Example case 2: The pretty numbers between 1111 and 3333 are 1212, 1313, 1919, 2222, 2323, 2929, 3232 and 3333.

Counting Pretty Numbers- CodeChef Solution in CPP

#include<iostream>
using namespace std;
bool isPrettyNumber(int number)
{
    // Get last digit of the number
    int single_digit = number % 10;
    if (single_digit == 2 || single_digit == 3 || single_digit == 9)
        return true;
    else
        return false;
}
int main()
{
    int test_cases;
    int a, b, counter;
    cin >> test_cases;
    while (test_cases--)
    {
        counter = 0;
        cin >> a >> b;
        for (int i = a; i <= b; i++)
        {
            if (isPrettyNumber(i))
                counter++;
        }
        cout << counter << endl;
    }
    return 0;
}

Disclaimer: The above Problem (Counting Pretty Numbers ) is generated by CodeChef but the solution is provided by  Chase2learn. This tutorial is only for Educational and Learning purpose.

Leave a Comment