HDU ACM 2207 IP的计算(二)

方法一:计算掩码位数有两种方法,使用LOG或位运算都可以;使用LOG要注意向上取整和+3,而不是+2,+2计算不对,通不过。换底公式log a(b)=log c(b)/log c(a);C语言只有log以e为底,log10以10为底。

#include<iostream>
#include<cmath>
using namespace std;

int GetNum(int x)
{
	double y;

	y=log10(x+3)/log10(2);
	return (int)ceil(y);
}

int GetNum1(int x)              //也可以用该函数
{
	int m=0,n=x+2;

	while(n)
    {
		m++;
        n=n>>1;
    }
	return m;
}

void g(int x,int& a,int& b,int& c,int& d)
{
	char temp[32];
	int i;

	for(i=31;i>31-GetNum(x);i--)
		temp[i]=0;
	for(;i>=0;i--)
		temp[i]=1;

	a=b=c=d=0;
	for(i=0;i<8;i++)
		a=a*2+temp[i];
	for(;i<16;i++)
		b=b*2+temp[i];

	for(;i<24;i++)
		c=c*2+temp[i];
	for(;i<32;i++)
		d=d*2+temp[i];
}

int main()
{
	int a,b,c,d,x;

	while(cin>>x)
	{
		g(x,a,b,c,d);
		cout<<a<<"."<<b<<"."<<c<<"."<<d<<endl;
	}
    return 0;
}

方法二:该种方法比较简便。

#include<iostream>
using namespace std;

union IPResult
{
	unsigned long IP;
	unsigned char Sub[4];
};

int main()
{
    long N;
    IPResult ipre;

    while (scanf("%ld",&N)!=EOF)
    {
        ipre.IP=0xFFFFFFFF;
        long m=1;
        N+=3;
        while (m<N)
        {
            m=m<<1;
        }
        ipre.IP=ipre.IP-(m-1);
        printf("%u.%u.%u.%u\n",ipre.Sub[3],ipre.Sub[2],ipre.Sub[1],ipre.Sub[0]);
    }
    return 0;
}
时间: 2024-11-09 03:50:31

HDU ACM 2207 IP的计算(二)的相关文章

HDU ACM 2206 IP的计算

一定要注意IP的长度不能超过15位. 方法一: #include<iostream> using namespace std; bool f(char* p) { int state,sum; state=sum=0; while(true) { switch(state) { case 0: case 1: case 2: if(*p==NULL) return false; if(*p<='9' && *p>='0' || *p=='.') { if(*p=='

hdu 2206 IP的计算 模拟

IP的计算 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7980    Accepted Submission(s): 1570 Problem Description 在网络课程上,我学到了很多有关IP的知识.IP全称叫网际协议,有时我们又用IP来指代我们的IP网络地址,现在IPV4下用一个32位无符号整数来表示,一般用点分方式来

HDU ACM 1005 Number Sequence

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 119732    Accepted Submission(s): 29072 Problem Description A number sequence is defined as follows:f(1) = 1, f(2) = 1, f(n) = (A

hdu acm 1166 敌兵布阵 (线段树)

敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 37903    Accepted Submission(s): 15985 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务

杭电OJ(HDU)-ACM Steps-Chapter Two-《Biker&#39;s Trip Odometer》《Climbing Worm》《hide handkerchief》《Nasty Hac》

1.2.1 Biker's Trip Odometer #include<stdio.h> #include<math.h> const double PI=acos(-1.0); /* 计算题,根据公式做就行,PI*d*r/(12*5280);res1/t*3600; Sample Input 26 1000 5 27.25 873234 3000 26 0 1000 Sample Output Trip #1: 1.29 928.20 Trip #2: 1179.86 1415

杭电HDU ACM Uncle Tom&#39;s Inherited Land*(二分图匹配 建模)

Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2496    Accepted Submission(s): 1028 Special Judge Problem Description Your old uncle Tom inherited a piece of land f

HDU ACM 1103 Flo&#39;s Restaurant

分析:借助STL的min_element实现.每次更新最先被占用的桌子,具体见注释. #include<iostream> #include<algorithm> using namespace std; int main() { int A,B,C; char s[10]; int a[102],b[102],c[102]; int curtime,count,ans; int *p; //桌子最先空闲时间 while(cin>>A>>B>>C

hdu acm 1425 sort(哈希表思想)

sort Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25803    Accepted Submission(s): 7764 Problem Description 给你n个整数,请按从大到小的顺序输出其中前m大的数. Input 每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且

hdu 4832 百度之星初赛二B

把横的和竖的分开考虑 //#pragma comment(linker, "/STACK:102400000,102400000") #include<iostream> #include<vector> #include<algorithm> #include<cstdio> #include<queue> #include<stack> #include<string> #include<ma