NYOJ 630 Problem of IP

Problem of IP

时间限制:1000 ms  |  内存限制:65535 KB

难度:2

描述
众所周知,计算机只能识别二进制数据,而我们却习惯十进制。所以人们发明了点分十进制来表示IP地址。即用以点分开的四个十进制数表示32位的二进制IP地址,每个数字代表IP地址中的8位。现在需要你编写程序实现二者之间的转换。

输入
输入包含多组测试数据。每组一行或为32位01字符串,或为一个点分十进制字符串。
输出
对于每一组输入,输出包含一行,为对应的另一种格式的IP地址
样例输入
00000000000000000000000000000000
255.255.255.255 
样例输出
0.0.0.0
11111111111111111111111111111111
本题要多注意点分十进制转换为32位01字符串时,01的字符串的存储顺序。
AC码:
#include<stdio.h>
#include<string.h>
int f[8]={1,2,4,8,16,32,64,128};
int main()
{
	int i,sum,len,count,j;
	char str[35],ch[33];
	while(~scanf("%s",str))
	{
		len=strlen(str);
		if(len==32)
		{
			sum=(str[0]-‘0‘)*f[7];
			for(i=1;str[i]!=‘\0‘;i++)
			{
				if(i%8==0)
				{
					printf("%d.",sum);
					sum=(str[i]-‘0‘)*f[7];
				}
				else
				{
					sum+=(str[i]-‘0‘)*f[7-(i%8)];
				}
			}
			printf("%d\n",sum);
		}
		else
		{
			ch[32]=‘\0‘;
			j=0;
			for(i=0;str[i]!=‘\0‘;i++)
			{
				if((str[i]>=‘0‘)&&(str[i]<=‘9‘))
				{
					sum=0;
					while((str[i]>=‘0‘)&&(str[i]<=‘9‘))
					{
						sum=sum*10+(str[i]-‘0‘);
						i++;
					}
					count=j+7;
					while(count>=j)
					{
						ch[count]=((sum%2)+‘0‘);
						sum=sum/2;
						count--;
					}
					j=j+8;
				}
				if(str[i]==‘\0‘)
					break;
			}
			printf("%s\n",ch);
		}
	}
	return 0;
}

NYOJ 630 Problem of IP,布布扣,bubuko.com

时间: 2024-10-03 06:39:51

NYOJ 630 Problem of IP的相关文章

NYOJ Problem of IP

Problem of IP 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 众所周知,计算机只能识别二进制数据,而我们却习惯十进制.所以人们发明了点分十进制来表示IP地址.即用以点分开的四个十进制数表示32位的二进制IP地址,每个数字代表IP地址中的8位.现在需要你编写程序实现二者之间的转换. 输入 输入包含多组测试数据.每组一行或为32位01字符串,或为一个点分十进制字符串. 输出 对于每一组输入,输出包含一行,为对应的另一种格式的IP地址 样例输入 000000

NYOJ A-B Problem

A-B Problem 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 A+B问题早已经被大家所熟知了,是不是很无聊呢?现在大家来做一下A-B吧. 现在有两个实数A和B,聪明的你,能不能判断出A-B的值是否等于0呢? 输入 有多组测试数据.每组数据包括两行,分别代表A和B. 它们的位数小于100,且每个数字前中可能包含+,- 号. 每个数字前面和后面都可能有多余的0. 每组测试数据后有一空行. 输出 对于每组数据,输出一行. 如果A-B=0,输出YES,否则输出NO

On-demand diverse path computation for limited visibility computer networks

In one embodiment, a source device detects a packet flow that meets criteria for multi-path forwarding, and forwards a probe packet on a primary path from the source device to a destination device, the probe packet carrying an indication to cause a p

hdu 5791 (DP) Two

hdu 5791 Two Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1421    Accepted Submission(s): 630 Problem Description Alice gets two sequences A and B. A easy problem comes. How many pair of sequ

HDU2121 Ice_cream’s world II 【最小树形图】+【不定根】

Ice_cream's world II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2744    Accepted Submission(s): 630 Problem Description After awarded lands to ACMers, the queen want to choose a city be he

hdu5802 Windows 10 贪心

Windows 10 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2096    Accepted Submission(s): 630 Problem Description Long long ago, there was an old monk living on the top of a mountain. Recently,

hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup

http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1610    Accepted Submission(s): 630 Problem Description (From wikipedia) For bina

ping程序 &nbsp; 源代码

cat ping.c  /*  * Copyright (c) 1989 The Regents of the University of California.  * All rights reserved.  *  * This code is derived from software contributed to Berkeley by  * Mike Muuss.  *  * Redistribution and use in source and binary forms, with

hdu 4647

Another Graph Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1691    Accepted Submission(s): 630 Problem Description Alice and Bob are playing a game on an undirected graph with n (n is e