cppAcademy
FROM LEARNING TO COMPETING

Beginner.
Your first challenges.

Bulgarian Informatics Group E · 2007–2026

270 independently explained problems: 54 Municipal, 54 Regional, 105 National Olympiad tasks and 57 Autumn Tournament tasks. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group E also includes challenging national-level problems. Years follow Arena’s competition-season labels. Source clarifications are noted on affected tasks.
GROUP E2008 · EasyStrings

Lucky Registration

Autumn Tournament (2008)

Submission name: lucky.cpp

Problem summary

A registration consists of two uppercase Latin letters, four decimal digits, and two more uppercase Latin letters. It is lucky when the product of its four digits equals the sum of the four letters’ ASCII codes divided by ten using integer division.

Input

An eight-character registration in the stated format.

Output

If lucky, print Yes followed by a space and the digit product. Otherwise print No.

Constraints

Exactly eight characters: two uppercase Latin letters, four digits, two uppercase Latin letters.

Worked example

Original statement sample.

Input

AF4171QA

Output

Yes 28

Solution analysis

  1. Read the registration as a string so its letters and any zero digits are preserved. Multiply digit values at positions 2 through 5, using zero-based indexing.
  2. Add ASCII codes of letters at positions 0, 1, 6, and 7. The portable expression 65 + (letter − 'A') supplies the specified ASCII value for an uppercase Latin letter. Compare the product with the integer quotient of this sum by ten.
  3. For AF4171QA, the digit product is 4 × 1 × 7 × 1 = 28. The letter sum is 65 + 70 + 81 + 65 = 281, whose integer quotient by ten is 28.

Why it works

The two accumulations visit exactly the four digit positions and exactly the four letter positions. Consequently the comparison is precisely the lucky-number condition, and the two output branches print its required result.

Complexity: O(1) time and O(1) extra space.

Watch out. Convert each digit character by subtracting '0'. Use integer division, and respect the capitalization Yes and No.

Commented C++17 solution

An independently written complete program with standard headers.

#include<algorithm>
#include<iostream>
#include<limits>
#include<string>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    string registration;
    cin>>registration;
    int product=1;
    for(int position=2;position<=5;position++){
        product*=registration[position]-'0';
    }
    int asciiSum=0;
    for(int position:{0,1,6,7}){
        // Uppercase Latin letters have ASCII codes 65 through 90.
        asciiSum+=65+(registration[position]-'A');
    }
    if(product==asciiSum/10){
        cout<<"Yes "<<product<<'\n';
    }
    else{
        cout<<"No\n";
    }
}

Source: Arena Olimpiici ↗