题解:整数与IP地址间的转换(Conversion between integer and IP address)

描述
原理:ip地址的每段可以看成是一个0-255的整数,把每段拆分成一个二进制形式组合起来,然后把这个二进制数转变成
一个长整数。
举例:一个ip地址为10.0.3.193
每段数字 相对应的二进制数
10 00001010
0 00000000
3 00000011
193 11000001

组合起来即为:00001010 00000000 00000011 11000001,转换为10进制数就是:167773121,即该IP地址转换后的数字就是它了。

数据范围:保证输入的是合法的 IP 序列
输入描述:
输入
1 输入IP地址
2 输入10进制型的IP地址

输出描述:
输出
1 输出转换成10进制的IP地址
2 输出转换后的IP地址

输入:
10.0.3.193
167969729
输出:
167773121
10.3.3.193
#include<iostream>
using namespace std;
int main()
{
    long long int a,b,c,d;
    long long int num;
    scanf("%lld.%lld.%lld.%lld",&a,&b,&c,&d);
    cin>>num;
    cout<<(a<<24)+(b<<16)+(c<<8)+d<<endl;
    a=num>>24;
    num-=(a<<24);
    b=num>>16;
    num-=(b<<16);
    c=num>>8;
    num-=(c<<8);
    d=num;
    cout<<a<<'.'<<b<<"."<<c<<'.'<<d<<endl;
}

————————

describe
Principle: each segment of IP address can be regarded as an integer from 0 to 255. Each segment is divided into a binary form and combined, and then the binary number is transformed into
A long integer.
Example: an IP address is 10.0.3.193
Binary number corresponding to each segment
10 00001010
0 00000000
3 00000011
193 11000001

The combination is: 0000100 00000000 00000011 11000001, which is converted to hexadecimal number: 167773121, that is, the converted number of the IP address is it.

Data range: ensure that the input is a legal IP sequence
Enter Description:
input
1 enter the IP address
Binary IP address input

Output Description:
output
1 output IP address converted to hexadecimal
2 output the converted IP address

输入:
10.0.3.193
167969729
输出:
167773121
10.3.3.193
#include<iostream>
using namespace std;
int main()
{
    long long int a,b,c,d;
    long long int num;
    scanf("%lld.%lld.%lld.%lld",&a,&b,&c,&d);
    cin>>num;
    cout<<(a<<24)+(b<<16)+(c<<8)+d<<endl;
    a=num>>24;
    num-=(a<<24);
    b=num>>16;
    num-=(b<<16);
    c=num>>8;
    num-=(c<<8);
    d=num;
    cout<<a<<'.'<<b<<"."<<c<<'.'<<d<<endl;
}