zoukankan      html  css  js  c++  java
  • TOJ4413: IP address

    传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=4413

    时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte

    描述

    An IP address can be described as dotted decimal string format or 32-bit binary number format.

    Now, given an IP address, if the IP address is in dotted decimal, output the 32-bit binary format ,On the contrary, output dotted decimal format.for example,the dotted decimal IP address 192.168.0.1,192->11000000,168->10101000,0->00000000,1->00000001,and the result is11000000101010000000000000000001 。

    输入

    Multiple inputs, each input has a string,the length of the string is less than 33;

    输出

    Output requirements of the subject, if you enter an IP address in dotted decimal, then the output 32-bit binary IP addresses, vice versa, output dotted decimal IP address.

    样例输入

    192.168.0.1

    样例输出

    11000000101010000000000000000001

    题目意思是,如果给的是32位的01串,那么让你把它转化为十进制IP地址的形式,如果是十进制IP地址的形式,则转化为01串形式输出。

    本题用java的BigInteger以及String可以快速实现十进制和二进制相互转换。于是就偷懒用java啦。注意java写十进制的时候读取的是一个字符串,分割到字符串数组里面可以调用split()函数,具体的看代码!

    代码:

    import java.math.BigDecimal;
    import java.math.BigInteger;
    import java.util.*;
    public class Main {
        public static void main(String[] args){
            Scanner input = new Scanner(System.in);
            while(input.hasNext()){
                String str = input.nextLine();
                String []s= str.split("\.");
                if(s[0].length() > 8){
                    StringBuilder sb=new StringBuilder();
                    sb.append(str);
                    sb.insert(8,".");
                    sb.insert(17,".");
                    sb.insert(26,".");
                    str = sb.toString();
                    String []s1 = str.split("\.");
                    for(int i = 0 ; i < s1.length; i++){
                        String num = new BigInteger(s1[i],2).toString(10);
                        System.out.print(num);
                        if(i != s1.length-1) System.out.print(".");
                    }
                    System.out.println();
                }
                else{
                    for(int i = 0 ; i < s.length; i++){
                        String num = new BigInteger(s[i]).toString(2);
                        for(int i1 = 0 ; i1 < 8 - num.length() ; i1++){
                            System.out.print("0");
                        }
                        System.out.print(num);
                    }
                    System.out.println();
                }    
            }    
        }
    }
  • 相关阅读:
    Elasticsearch Windows下安装及配置集群
    .Net文件压缩
    DateHelper
    lambda Helper
    Log4net的使用
    python3之rabbitMQ
    python3之协程
    python3之paramiko模块
    python3之redis
    redis
  • 原文地址:https://www.cnblogs.com/Esquecer/p/8443256.html
Copyright © 2011-2022 走看看