Problem of IP
Time Limit:1000MS Memory Limit:65536K
Total Submit:84 Accepted:74
Description
众所周知,计算机只能识别二进制数据,而我们却习惯十进制。所以人们发明了点分十进制来表示IP地址。即用以点分开的四个十进制数表示32位的二进制IP地址,每个数字代表IP地址中的8位。现在需要你编写程序实现二者之间的转换。
Input
输入包含多组测试数据。每组一行或为32位01字符串,或为一个点分十进制字符串。
Output
对于每一组输入,输出包含一行,为对应的另一种格式的IP地址
Sample Input
00000000000000000000000000000000
255.255.255.255
Sample Output
0.0.0.0
11111111111111111111111111111111
Source
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AK1155 {
class Program {
static int Fun1(string s) {//2 to 10
int n = int.Parse(s);
int ans = 0;
while (n > 0) {
ans = ans * 2 + n % 10;
n /= 10;
}
return ans;
}
static string Fun2(string s) {//10 to 2
int n = int.Parse(s);
int ans = 0;
while (n > 0) {
ans = ans * 10 + n % 2;
n = n >> 1;
}
return ans.ToString();
}
static void Main(string[] args) {
string s;
while ((s = Console.ReadLine()) != null) {
if (s.Length == 32) {//32??01??
string s1 = s.Substring(0 , 8);
Console.Write(Fun1(s1));
string s2 = s.Substring(8 , 8);
Console.Write("." + Fun1(s2));
string s3 = s.Substring(16 , 8);
Console.Write("." + Fun1(s3));
string s4 = s.Substring(24 , 8);
Console.Write("." + Fun1(s4));
} else {//4??10??????
string[] ss = s.Split('.');
Console.Write(Fun2(ss[0]) + Fun2(ss[1]) + Fun2(ss[2]) + Fun2(ss[3]));
}
Console.WriteLine();
}
}
}
}