RevealTriangle
SRM 404 · 2008-06-05 · by boba5551
Problem Statement
Suppose there is a triangle of digits like the following:
74932 1325 457 92 1
Each digit, with the exception of those in the top row, is equal to the last digit of the sum of its upper and upper-right neighboring digits.
You will be given a
Constraints
- questionMarkTriangle will contain between 1 and 50 elements, inclusive.
- Element i (0 indexed) of questionMarkTriangle will contain exactly n-i characters, where n is the number of elements in questionMarkTriangle.
- Each element of questionMarkTriangle will contain exactly one digit ('0'-'9') and all others characters will be '?'s.
{"4??",
"?2",
"1"}
Returns: {"457", "92", "1" }
Let's substitute '?'s with unknown variables: 4ab c2 1 Having done that, we start solving for the variables from the bottom to the top. First, we know that the last digit of (c + 2) is 1. Therefore, c must be 9: 4ab 92 1 Now we know that the last digit of (4 + a) is 9, which means a is 5: 45b 92 1 And, finally, the last digit of (5 + b) is 2, so b is 7.
{"?????1?", "2?????", "??4??", "?8??", "6??", "?2", "4"}
Returns: {"1111111", "222222", "44444", "8888", "666", "22", "4" }
{"1"}
Returns: {"1" }
{"1????????", "?2??????", "??3????", "???4??", "????5", "6???", "?7?", "8?", "9"}
Returns: {"184090732", "92499705", "1638675", "791432", "60575", "6522", "174", "81", "9" }
{"???2", "??2", "?2", "2"}
Returns: {"0002", "002", "02", "2" }
Submissions are judged against all 67 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class RevealTriangle with a public method vector<string> calcTriangle(vector<string> questionMarkTriangle) · 67 test cases · 2 s / 256 MB per case