华为机试题(4)

本文介绍了如何识别并转换字符串中的整数,并通过实例展示了如何实现IP地址的最大前缀匹配算法,对于网络路由模块的设计具有实际应用价值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、识别字符串中的整数并转换为数字形式

void take_num(const char *strIn, int *n, unsigned int *outArray)

【输入】 strIn: 输入的字符串

【输出】 n: 统计识别出来的整数个数

outArray:识别出来的整数值,其中outArray[0]是输入字符串中从左到右第一个整数,

outArray[1]是第二个整数,以此类推。数组地址已经分配,可以直接使用

【返回】 无

注:

I、 不考虑字符串中出现的正负号(+, -),即所有转换结果为非负整数(包括0和正整数)

II、 不考虑转换后整数超出范围情况,即测试用例中可能出现的最大整数不会超过unsigned int可处理的范围

III、 需要考虑 '0' 开始的数字字符串情况,比如 "00035" ,应转换为整数35;

"000" 应转换为整数0;"00.0035" 应转换为整数0和35(忽略小数点:mmm.nnn当成两个数mmm和nnn来识别)

IV、 输入字符串不会超过100 Bytes,请不用考虑超长字符串的情况。

示例

输入:strIn = "ab00cd+123fght456-25 3.005fgh"

输出:n = 6

outArray = {0, 123, 456, 25, 3, 5}

  1. #include<stdio.h>
  2. #include<string.h>
  3. voidtake_num(constchar*strIn,int*n,unsignedint*outArray)
  4. {
  5. unsignedintres;
  6. /*unsignedint*/
  7. intm=0;
  8. while(*strIn!='\0')
  9. {
  10. while(!(*strIn>='0'&&*strIn<='9')&&*strIn!='\0')
  11. strIn++;
  12. if(*strIn!='\0')
  13. {
  14. res=*strIn-'0';
  15. strIn++;
  16. while(*strIn>='0'&&*strIn<='9')
  17. {
  18. res=10*res+(*strIn-'0');
  19. strIn++;
  20. }
  21. outArray[m]=res;
  22. m++;
  23. }
  24. }
  25. *n=m;
  26. }
  27. intmain(/*intargc,char**argv*/)
  28. {
  29. intnum;
  30. inti;
  31. constcharstrIn[50]="ab00cd+123fght456-253.005fgh";
  32. unsignedintoutArray[50];
  33. take_num(strIn,&num,outArray);
  34. for(i=0;i<num;i++)
  35. printf("%d",outArray[i]);
  36. system("pause");
  37. return0;
  38. }
#include <stdio.h>
#include <string.h>

void take_num(const char *strIn, int *n, unsigned int *outArray)
{
	unsigned int res;
	/*unsigned int */
	int m=0;
	while(*strIn != '\0')
	{
		while(!(*strIn>='0' && *strIn <= '9') && *strIn != '\0')
			strIn++;
		if(*strIn != '\0')
		{
			res= *strIn-'0';
			strIn++;
			while(*strIn>='0' && *strIn <= '9')
			{
				res = 10*res+(*strIn-'0');
				strIn++;
			}
			outArray[m] =res;
			m++;
		}
	}
	*n = m;
}

int main(/*int argc, char **argv*/)
{
	int num;
	int i;
	const char strIn[50]="ab00cd+123fght456-25  3.005fgh";
	unsigned int outArray[50];
	take_num(strIn,&num,outArray);
	for(i=0;i<num;i++)
		printf("%d ",outArray[i]);

	system("pause");
	return 0;
}


1、 IP地址匹配(60分)

问题描述:

在路由器中,一般来说转发模块采用最大前缀匹配原则进行目的端口查找,具体如下:

IP地址和子网地址匹配:

IP地址和子网地址所带掩码做AND运算后,得到的值与子网地址相同,则该IP地址与该子网匹配。

比如:

IP地址:192.168.1.100

子网:192.168.1.0/255.255.255.0,其中192.168.1.0是子网地址,255.255.255.0是子网掩码。

192.168.1.100&255.255.255.0 = 192.168.1.0,则该IP和子网192.168.1.0匹配

IP地址:192.168.1.100

子网:192.168.1.128/255.255.255.192

192.168.1.100&255.255.255.192 = 192.168.1.64,则该IP和子网192.168.1.128不匹配

最大前缀匹配:

任何一个IPv4地址都可以看作一个32bit的二进制数,比如192.168.1.100可以表示为:11000000.10101000.00000001.01100100,

192.168.1.0可以表示为11000000.10101000.00000001.00000000

最大前缀匹配要求IP地址同子网地址匹配的基础上,二进制位从左到右完全匹配的位数尽量多(从左到右子网地址最长)。比如:

IP地址192.168.1.100,同时匹配子网192.168.1.0/255.255.255.0和子网192.168.1.64/255.255.255.192,

但对于子网192.168.1.64/255.255.255.192,匹配位数达到26位,多于子网192.168.1.0/255.255.255.0的24位,

因此192.168.1.100最大前缀匹配子网是192.168.1.64/255.255.255.192。

请编程实现上述最大前缀匹配算法。

要求实现函数:

void max_prefix_match(const char *ip_addr, const char *net_addr_array[], int *n)

【输入】ip_addr:IP地址字符串,严格保证是合法IPv4地址形式的字符串

net_addr_array:子网地址列表,每一个字符串代表一个子网,包括子网地址和掩码,

表现形式如上述,子网地址和子网掩码用’/’分开,严格保证是

合法形式的字符串;如果读到空字符串,表示子网地址列表结束

【输出】n:最大前缀匹配子网在*net_addr_array[]数组中对应的下标值。如果没有匹配返回-1

示例

输入:

ip_addr = "192.168.1.100"

net_addr_array[] =

{

"192.168.1.128/255.255.255.192",

"192.168.1.0/255.255.255.0",

"192.168.1.64/255.255.255.192",

"0.0.0.0/0.0.0.0",

""

}

  1. #include<stdio.h>
  2. #include<string.h>
  3. voidmax_prefix_match(constchar*ip_addr,constchar*net_addr_array[],int*n)
  4. {
  5. intip,subnet_ip,subnet_mask;
  6. intip_pa[4],subnet_ip_pa[4],subnet_mask_pa[4];
  7. intindex;
  8. inti,result,cnt=0;
  9. intmaxi=0;
  10. sscanf(ip_addr,"%d.%d.%d.%d",&ip_pa[0],&ip_pa[1],&ip_pa[2],&ip_pa[3]);
  11. ip=(ip_pa[0]<<24)+(ip_pa[1]<<16)+(ip_pa[2]<<8)+(ip_pa[3]);
  12. *n=-1;
  13. for(index=0;net_addr_array[index][0]!='\0';index++)
  14. {
  15. sscanf(net_addr_array[index],"%d.%d.%d.%d/%d.%d.%d.%d",&subnet_ip_pa[0],&subnet_ip_pa[1],&subnet_ip_pa[2],&subnet_ip_pa[3],&subnet_mask_pa[0],&subnet_mask_pa[1],&subnet_mask_pa[2],&subnet_mask_pa[3]);
  16. subnet_ip=(subnet_ip_pa[0]<<24)+(subnet_ip_pa[1]<<16)+(subnet_ip_pa[2]<<8)+(subnet_ip_pa[3]);
  17. subnet_mask=(subnet_mask_pa[0]<<24)+(subnet_mask_pa[1]<<16)+(subnet_mask_pa[2]<<8)+(subnet_mask_pa[3]);
  18. if((ip&subnet_mask)==subnet_ip)
  19. {
  20. //统计子网掩码中1的个数
  21. for(i=0;i<sizeof(subnet_mask)*8;i++)
  22. {
  23. result=subnet_mask&1;
  24. subnet_mask=subnet_mask>>1;
  25. cnt+=result;
  26. }
  27. if(cnt>maxi)
  28. {
  29. maxi=cnt;
  30. *n=index;
  31. }
  32. }
  33. }
  34. }
  35. intmain(/*intargc,char**argv*/)
  36. {
  37. intn;
  38. constcharip_addr[50]="192.168.1.100";
  39. constchar*net_addr_array[256]=
  40. {
  41. "192.168.1.128/255.255.255.192",
  42. "192.168.1.0/255.255.255.0",
  43. "192.168.1.64/255.255.255.192",
  44. "0.0.0.0/0.0.0.0",
  45. ""
  46. };
  47. max_prefix_match(ip_addr,net_addr_array,&n);
  48. printf("n=%d\n",n);
  49. system("pause");
  50. return0;
  51. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值