Any number is called beautiful if it consists of 2N digits and the sum of the first N digits is equal to the sum of the last N digits. Your task is to find the count of beautiful numbers in the interval from L to R (including L and R).
Beautiful numbers do not have leading zeroes.
• The first line contains an integer T denoting the number of test cases.
• The first line of each test case contains two space-separated integers L and R denoting the range interval [L, R].
Output format
For each test case, print the count of beautiful numbers in a new line.
1 9
#include <bits/stdc++.h>
using namespace std;
int main()
{
int l, r;
cin >> l >> r;
for (int j = l; j <= r; j++)
{
string n = to_string(j);
int sm1 = 0, sm = 0;
for (int i = 0; i < n.size(); i++)
{
if (i < n.size() / 2)
sm1 += n[i] - '0';
sm += n[i] - '0';
}
if (sm1 == sm - sm1)
cout << "Beatiful\n";
else
cout << "Ugly\n";
}
}
Comments
Leave a comment