描述 | |
---|---|
知识点 | 字符串,位运算 |
运行时间限制 | 10M |
内存限制 | 128 |
输入 | 输入 |
输出 | 输出 |
样例输入 | 10.0.3.193 167969729 |
样例输出 | 167773121 10.3.3.193 |
#include<iostream>
#include<string>
using namespace std;
string IntoStr(int n)
{
char p[100],str[100];
string s;
if(n==0)
return "0";
else
{
int i=0;
while(n)
{
p[i]=n%10+'0';
n=n/10;
i++;
}
int j=0;
i--;
while(i>=0)
{
str[j]=p[i];
i--;
j++;
}
str[j]='\0';
s=str;
return s;
}
}
bool IntoIp(unsigned int n)
{
string ip,str;
int temp;
temp=n%256;
ip=IntoStr(temp);
str=ip+str;
n=n/256;
for(int i=0;i<3;i++)
{
temp=n%256;
ip=IntoStr(temp);
ip+=".";
str=ip+str;
n=n/256;
}
if(n!=0)
return false;
cout<<str<<endl;
return true;
}
bool IptoInt(string s)
{
string str;
int iptmp,ipsum=0;
for(int i=0;i<3;i++)
{
int pos=s.find_first_of('.',0);
str=s.substr(0,pos);
s=s.substr(pos+1,s.size()-pos-1);
iptmp=atoi(str.c_str());
if(iptmp<0||iptmp>255)
return false;
ipsum=ipsum*256+iptmp;
}
iptmp=atoi(s.c_str());
if(iptmp<0||iptmp>255)
return false;
else
ipsum=ipsum*256+iptmp;
cout<<ipsum<<endl;
return true;
}
int main()
{
string s;
unsigned int n;
cin>>s>>n;
IptoInt(s);
IntoIp(n);
//system("pause");
return 0;
}