ง่าย 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

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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก