PTA IP地址转换

PTA IP地址转换
是不是还在为网络上没有java版本的PTA呢;哈哈
如有错误,还请指正啊。

一个IP地址是用四个字节(每个字节8个位)的二进制码组成。请将32位二进制码表
示的IP地址转换为十进制格式表示的IP地址输出。

输入格式:
输入在一行中给出32位二进制字符串。

输出格式:
在一行中输出十进制格式的IP地址,其由4个十进制数组成(分别对应4个8位的二进制数),中间用“.”分隔开。

输入样例:
11001100100101000001010101110010
输出样例:
204.148.21.114

package pta20;

import java.util.Scanner;

public class IP {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//Enter a binary number
	    Scanner scan = new Scanner(System.in);
		String two = scan.nextLine();
		for (int i=0;i<=24;i+=8)
		{
		       //Transform the number from binary number to decimal number
			int n=Integer.parseInt(two.substring(i, i+8),2);
			System.out.print(n+".");//Print IP.
		}
	}
}

输出结果是对的,哈哈

原创文章 20 获赞 144 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_43714332/article/details/85174793
PTA
今日推荐