Question part
topic | Convert IP address to integer |
difficulty | easy |
Question description | There is a virtual IPv4 address, which consists of 4 sections. Each section ranges from 0 to 255, separated by #. The virtual IPv4 address can be converted to a 32-bit integer, for example: 128#0#255#255, converted to The result of the 32-bit integer is 2147549183 (0x8000FFFF) 1#0#0#0. The result of conversion to a 32-bit integer is 16777216 (0x01000000). Now a virtual IPv4 address is given in the form of a string, limiting the range of section 1 to 1 ~ 128, that is, the range of each section is (1~128)#(0~255)#(0~255)#(0~255). It is required that each IPv4 address can only correspond to a unique integer. If it is an illegal IPv4, invalid IP is returned. |
Enter description | Enter one line, virtual IPv4 address format string. |
Output description | Output the IPv4 corresponding integer, or "invalid IP" as required. |
Additional information | When entering an IPv4 address that may be illegal, you need to identify illegal IPv4 (empty strings, characters that do not exist in the IP address, illegal #, decimal integers not within the legal range, etc.). |
------------------------------------------------------ | |
Example | |
Example 1 | |
enter | 100#101#1#5 |
output | 1684340997 |
illustrate | none |
Example 2 | |
enter | 1#2#3 |
output | invalid IP |
illustrate | |
Interpretation and analysis
Item explanation:
Input a string and convert it into the corresponding integer; if there is an illegal input, output "invalid IP".
Analysis Yoshiro:
The idea of this question is relatively simple:
1. Determine whether it is an illegal input.
2. If it is illegal, output invalid IP. If it is legal, use "#" as the delimiter and divide it into 4 integers. The first integer * 256 * 256 * 256, plus the second integer * 256 * 256, plus the third integer * 256, plus The 4th integer. Output the final result.
Code
Java code
import java.util.Scanner;
/**
* IPv4转换成整数
*
* @since 2023.11.20
* @version 0.1
* @author Frank
*
*/
public class IPv4Convert2Int {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String input = sc.nextLine();
processIPv4Convert2Int(input);
}
}
private static void processIPv4Convert2Int(String input) {
String invalidIP = "invalid IP";
if( input == null || input.length() == 0 )
{
System.out.println(invalidIP);
return;
}
String[] ipSegments = input.split( "#" );
if( ipSegments.length != 4 )
{
System.out.println(invalidIP);
return;
}
int result = 0;
for( int i = 0; i < ipSegments.length; i ++ )
{
int tmpNum;
try {
tmpNum = Integer.parseInt( ipSegments[i] );
}catch( NumberFormatException e)
{
System.out.println(invalidIP);
return;
}
if( i == 0 && ( tmpNum < 1 || tmpNum > 128 ))
{
System.out.println(invalidIP);
return;
}
if( i >= 1 && ( tmpNum < 0 || tmpNum > 255 ))
{
System.out.println(invalidIP);
return;
}
result = result * 256 + tmpNum;
}
System.out.println( result );
}
}
JavaScript code
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void async function() {
while (line = await readline()) {
processIPv4Convert2Int(line);
}
}();
function processIPv4Convert2Int( line ) {
var invalidIP = "invalid IP";
if( line == null || line.length == 0)
{
console.log( invalidIP );
return;
}
var ipSegments = line.split("#");
if( ipSegments.length != 4 )
{
console.log( invalidIP );
return;
}
var result = 0;
for( var i = 0; i < ipSegments.length; i ++ )
{
var tmpNum = parseInt( ipSegments[i] );
if( isNaN( tmpNum ))
{
console.log( invalidIP );
return;
}
if( i == 0 && ( tmpNum < 1 || tmpNum > 128 ))
{
console.log( invalidIP );
return;
}
if( i >= 1 && ( tmpNum < 0 || tmpNum > 255 ))
{
console.log( invalidIP );
return;
}
result = result * 256 + tmpNum;
}
console.log( result );
}
(over)