设为首页 收藏本站
查看: 1033|回复: 0

[经验分享] 华为机试题(4)

[复制链接]

尚未签到

发表于 2016-6-6 09:01:44 | 显示全部楼层 |阅读模式
  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}

[cpp] view plaincopyprint?





  • #include<stdio.h>


  • #include<string.h>



  • voidtake_num(constchar*strIn,int*n,unsignedint*outArray)
  • {
  • unsignedintres;
  • /*unsignedint*/
  • intm=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;
  • }


  • intmain(/*intargc,char**argv*/)
  • {
  • intnum;
  • inti;
  • constcharstrIn[50]="ab00cd+123fght456-253.005fgh";
  • unsignedintoutArray[50];
  • take_num(strIn,&num,outArray);
  • for(i=0;i<num;i++)
  • printf("%d",outArray);

  • system("pause");
  • return0;
  • }



#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);
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",
  
  ""
  }

[cpp] view plaincopyprint?





  • #include<stdio.h>


  • #include<string.h>



  • voidmax_prefix_match(constchar*ip_addr,constchar*net_addr_array[],int*n)
  • {
  • intip,subnet_ip,subnet_mask;
  • intip_pa[4],subnet_ip_pa[4],subnet_mask_pa[4];
  • intindex;
  • inti,result,cnt=0;
  • intmaxi=0;
  • sscanf(ip_addr,"%d.%d.%d.%d",&ip_pa[0],&ip_pa[1],&ip_pa[2],&ip_pa[3]);
  • ip=(ip_pa[0]<<24)+(ip_pa[1]<<16)+(ip_pa[2]<<8)+(ip_pa[3]);
  • *n=-1;
  • for(index=0;net_addr_array[index][0]!='\0';index++)
  • {
  • 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]);
  • subnet_ip=(subnet_ip_pa[0]<<24)+(subnet_ip_pa[1]<<16)+(subnet_ip_pa[2]<<8)+(subnet_ip_pa[3]);
  • subnet_mask=(subnet_mask_pa[0]<<24)+(subnet_mask_pa[1]<<16)+(subnet_mask_pa[2]<<8)+(subnet_mask_pa[3]);
  • if((ip&subnet_mask)==subnet_ip)
  • {
  • //统计子网掩码中1的个数

  • for(i=0;i<sizeof(subnet_mask)*8;i++)
  • {
  • result=subnet_mask&1;
  • subnet_mask=subnet_mask>>1;
  • cnt+=result;
  • }
  • if(cnt>maxi)
  • {
  • maxi=cnt;
  • *n=index;
  • }
  • }

  • }

  • }


  • intmain(/*intargc,char**argv*/)
  • {
  • intn;
  • constcharip_addr[50]="192.168.1.100";
  • constchar*net_addr_array[256]=
  • {

  • "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",
  • ""


  • };
  • max_prefix_match(ip_addr,net_addr_array,&n);

  • printf("n=%d\n",n);

  • system("pause");
  • return0;
  • }

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-226848-1-1.html 上篇帖子: 华为机试题(5) 下篇帖子: 华为机试题(3)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表