UVa 12602 – Nice Licence Plates
Alberta license plates currently follow the format ABC-0123 (three letters followed by four digits).
A license plate is considered "nice" if the absolute difference between the values of the first and second parts is at most 100.
The value of the first part is calculated as the value of the letters [A-Z] in base 26.
For example, if the first part is "ABC", its value would be 28 (0 * (26^2)) + (1 * (26^1)) + (2 * (26^0)).
Thus, the license plate "ABC-0123" is nice because |28-123| ≤ 100.
Your program will be given a list of license plate numbers, and it should determine whether each license plate is "nice" or "not nice".
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The first line of input contains an integer N (1 ≤ N ≤ 100), which represents the number of license plates. The following N lines each contain a license plate in the format "LLL-DDDD", where L represents a letter and D represents a digit. | For each license plate, output "nice" if the absolute difference between the values of the first and second parts is at most 100, otherwise output "not nice". |
2 ABC-0123 AAA-9999 | nice not nice |
Thought Process
Set the numerical part and the alphabetical part as different variables. Use a for loop to add up the alphabetical part, and be mindful that it should be "alphabetical character order * (26^reversed position)"—this part is not very clear in the question, so pay attention to it.
Sample Code-ZeroJudge E505: Nice Licence Plates
#include <iostream>
#include <math.h>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
for (int i = 0; i<N; i++)
{
string str;
cin >> str;
int one = 0, two, count = 2;
for (int i = 0; i<3; i++)
{
one += int(str[i] - 'A') * pow(26, count);
count--;
}
two = stoi(str.substr(4, str.length()));
if (abs(one - two) <= 100)
{
cout << "nice\n";
continue;
}
cout << "not nice\n";
}
}
//ZeroJudge E505
//Dr. SeanXD