すべてのチャレンジ
簡単 0件解決済み 正答率0%

Interstate Highway Numbers

Classify an interstate highway number as a primary or auxiliary route and give its direction.

このチャレンジで学べること

  • Use % to find the last two digits and parity
  • Separate valid from invalid inputs
  • Build result strings from parts

問題

翻訳を表示

US interstate numbers follow a pattern. Numbers 1 to 99 are primary routes: odd numbers run north-south and even numbers run east-west. Three-digit numbers are auxiliary routes (loops and spurs) that serve the primary route given by their last two digits, so I-405 serves I-5 and I-290 serves I-90. An auxiliary route runs in the same direction as the route it serves. A three-digit number ending in 00 does not serve any route and is not valid, and neither is 0 or anything above 999.

Implement interstate_highway_numbers(number).

Input

number: The highway number.

Output

Return one of these strings, where D is north-south or east-west and P is the served primary route:

  • "Primary route, runs D" for 1 to 99
  • "Auxiliary route of I-P, runs D" for valid numbers from 101 to 999
  • "Invalid" for everything else

制約

−10,000 ≤ number ≤ 10,000

例

例 1
入力:
number = 95

期待される出力:
expected = "Primary route, runs north-south"
入力:
long long number = 95;

期待される出力:
std::string expected = "Primary route, runs north-south";
入力:
long number = 95;

期待される出力:
string expected = "Primary route, runs north-south";

解説

95 is odd and below 100, so the answer is "Primary route, runs north-south".

例 2
入力:
number = 290

期待される出力:
expected = "Auxiliary route of I-90, runs east-west"
入力:
long long number = 290;

期待される出力:
std::string expected = "Auxiliary route of I-90, runs east-west";
入力:
long number = 290;

期待される出力:
string expected = "Auxiliary route of I-90, runs east-west";

解説

The last two digits are 90, an even primary route, so the answer is "Auxiliary route of I-90, runs east-west".

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

Rule out 0, negatives, numbers above 999 and multiples of 100 first.

ヒント 2

number % 100 gives the primary route a three-digit number serves.

ヒント 3

The direction depends on whether that primary route number is odd or even.

目安の計算量

O(1) time, O(1) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

まだ合格した解答はありません。解いて 1 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。