cswork / OA打开练习工作台

RipplingOA #2

Location Detection (IP Region Lookup)

估计简单评测数据准备中,可先阅读题面

考点(AI 标注)字符串

Implement a function to identify a user's location region based on their IP address.

IPv4 addresses are represented by four octets in the format "a.b.c.d" (e.g., "120.10.20.30"). These IPv4 addresses are classified into 5 different regions based on the first octet's value:

  • 1: 0.0.0.0 – 127.255.255.255
  • 2: 128.0.0.0 – 191.255.255.255
  • 3: 192.0.0.0 – 223.255.255.255
  • 4: 224.0.0.0 – 239.255.255.255
  • 5: 240.0.0.0 – 255.255.255.255

Given a list of ip_addresses representing the validity of each address and classify it into one of the 5 regions. Return an array of integers representing the region index for each corresponding IP address. Invalid IP addresses should be represented as -1.

An IPv4 address is valid if:

  • It contains exactly four octets separated by periods.
  • Each octet is an integer between 0 and 255, inclusive.
  • There are no leading zeros in any octet.