Price Sign
Autumn Tournament (2012)
Submission name: sign.cpp
Problem summary
Write three two-digit prices consecutively in their input order. At each boundary, if the previous price ends with the digit that starts the next price, write that digit once and put an apostrophe after it to mark the repetition. Produce the resulting sign.
Source clarification. The PDF typesets the apostrophe as the right quotation mark U+2019 (its sample is 1317’2). The code and displayed sample normalize that punctuation to the standard ASCII apostrophe U+0027; official judge byte-level expectations were not available.
Input
Three integers cena1, cena2 and cena3.
Output
The sign text, with no spaces.
Constraints
9 < cena1, cena2, cena3 < 100.
Worked example
Original numeric sample; the PDF’s typographic apostrophe is normalized to ASCII as disclosed in sourceNote.
Input
13 17 72
Output
1317'2
Solution analysis
- Start the output with the first price. Then process each following price using the last digit of the preceding original price.
- If the boundary digits match, append an apostrophe and only the final digit of the new price. Otherwise append the complete new price. Compare the original prices, since an apostrophe may occur in the constructed text.
- For 13, 17 and 72, the first boundary does not match, giving 1317. At the next boundary both digits are 7, so append an apostrophe and 2, yielding 1317'2.
Why it works
Initially the output correctly represents the first price. At each subsequent boundary the program either preserves both unequal digits or represents the equal pair by one digit and its repetition mark, exactly as required. Processing both boundaries therefore constructs the complete sign in the given order.
Watch out. Do not reorder the prices. A match replaces only the first digit of the following price. Both boundaries may match.
Commented C++17 solution
An independently written complete program with standard headers.
#include<iostream>
#include<string>
using namespace std;
int main(){
string prices[3];
cin>>prices[0]>>prices[1]>>prices[2];
cout<<prices[0];
for(int i=1;i<3;i++){
if(prices[i-1][1]==prices[i][0]){
// The shared digit has already been printed.
cout<<"'"<<prices[i][1];
}
else{
cout<<prices[i];
}
}
cout<<'\n';
}