Browse Source

IP转化,Java版

爱玲姐姐 5 years ago
parent
commit
3268d8b16b
1 changed files with 41 additions and 0 deletions
  1. 41 0
      F.java

+ 41 - 0
F.java

@@ -0,0 +1,41 @@
+import java.util.Scanner;
+  
+public class Main {
+    public static void main(String[] args) {
+        Scanner cin = new Scanner(System.in);
+        while (cin.hasNext()) {
+            String s = cin.next();
+            if (s.length() == 32) {
+                int s1 = F(s.substring(0, 8));
+                int s2 = F(s.substring(8, 16));
+                int s3 = F(s.substring(16, 24));
+                int s4 = F(s.substring(24, 32));
+                System.out.println(s1 + "." + s2 + "." + s3 + "." + s4);
+            } else {
+                String[] c = s.split("\\W+");
+                for (int i = 0; i < c.length; i++) {
+                    System.out.print(S(c[i]));
+                }
+                System.out.println();
+            }
+        }
+    }
+  
+    private static String S(String s) {
+        Integer x = Integer.parseInt(s, 10);
+        String k = Integer.toString(x, 2);
+        if (k.length() < 8) {
+            String t = "";
+            for (int i = 0; i < 8 - k.length(); i++) {
+                t += "0";
+            }
+            String q = t + k;
+            return q;
+        } else
+            return k;
+    }
+  
+    private static int F(String s1) {
+        return Integer.parseInt(s1, 2);
+    }
+}