华为C语言机试题面试题汇总[共38页]

上传人:gfy****yf 文档编号:40560311 上传时间:2021-11-16 格式:DOC 页数:38 大小:150KB
返回 下载 相关 举报
华为C语言机试题面试题汇总[共38页]_第1页
第1页 / 共38页
华为C语言机试题面试题汇总[共38页]_第2页
第2页 / 共38页
华为C语言机试题面试题汇总[共38页]_第3页
第3页 / 共38页
点击查看更多>>
资源描述
1第一题的题目大概是输入整型数组求数组的最小数和最大数之和,例如输入1,2,3,4则输出为5,当输入只有一个数的时候,则最小数和最大数都是该数,例如只输入1,则输出为2;另外数组的长度不超过50#include<stdio.h>main()         int num50=0;         int i,n;          printf("请输入整型数组的长度(150):");         scanf("%d",&n);                    printf("请输入整型数组的元素:");         for (i=0;i<n;i+)                            scanf("%d",&numi);                        int min_num=num0;         int max_num=num0;         for(int j=0;j<n;j+)                            if(max_num<numj)                            max_num=numj;                   else if(min_num>numj)                            min_num=numj;                  int sum=min_num+max_num;         printf("数组中最大与最小值之和:%dn",sum);         return 0; 2求两个长长整型的数据的和并输出,例如输入1233333333333333 。 3111111111111111111111111.。,则输出。#include<stdio.h>#include<string.h>#include<malloc.h>main()         char *num1,*num2;  /两个长长整型数据         char *sum;/      int temp;int len_num1,len_num2; / 两个长长整型数据的长度         int len_max,len_min;         num1=(char*)malloc(sizeof(char);         num2=(char*)malloc(sizeof(char);         printf("输入两个长长整型数据:");         scanf("%s",num1);         printf("输入两个长长整型数据:");         scanf("%s",num2);         len_num1=strlen(num1);         len_num2=strlen(num2);         len_max=(len_num1>=len_num2)? len_num1:len_num2;         len_min=(len_num1<=len_num2)? len_num1:len_num2;         int len_max1=len_max;         sum=(char*)malloc(sizeof(char)*len_max);         memset(sum,0x00,len_max+1);/切忌初始化         for(;len_num1>0&&len_num2>0;len_num1-,len_num2-)                  sumlen_max-=(num1len_num1-1-'0')+(num2len_num2-1-'0');                  if(len_num1>0)                            sumlen_max-=num1len_num1 - 1 -'0'                   len_num1-;                  if(len_num2>0)                            sumlen_max-=num1len_num2 - 1-'0'                   len_num2-;                  for(int j=len_max1;j>=0;j-) /实现进位操作                  /      temp=sumj-'0'                   if(sumj>=10)                                      sumj-1+=sumj/10;                            sumj%=10;                                     char *outsum=(char*)malloc(sizeof(char)*len_max1);         j=0;         while(sumj=0)  /跳出头部0元素                   j+;         for(int m=0;m<len_max1;j+,m+)                   outsumm=sumj+'0'         outsumm='0'    printf("输出两长长整型数据之和:%sn",outsum);         return 0; 3.通过键盘输入一串小写字母(az)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。比如字符串“abacacde”过滤结果为“abcde”。要求实现函数:void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);【输入】 pInputStr:输入字符串lInputLen:输入字符串长度【输出】 pOutputStr:输出字符串,空间已经开辟好,与输入字符串等长;#include <stdio.h>#include<string.h>#include<malloc.h>void stringFilter(const char *p_str, long len, char *p_outstr)int array256=0;const char *tmp = p_str;for(int j=0;j<len;j+)          if(arraytmpj=0)                   *p_outstr+= tmpj;         arraytmpj+;          *p_outstr = '0' void main()         char  *str = "cccddecc"         int len = strlen(str);                   char * outstr = (char *)malloc(len*sizeof(char);         stringFilter(str,len,outstr);         printf("%sn",outstr);         free(outstr);         outstr = NULL; 4.通过键盘输入一串小写字母(az)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。压缩规则:1. 仅压缩连续重复出现的字符。比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc".2. 压缩字段的格式为"字符重复的次数+字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz" 要求实现函数:void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr); 【输入】 pInputStr: 输入字符串lInputLen: 输入字符串长度【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;#include <stdio.h>#include<string.h>#include<malloc.h> void stringZip(const char *p_str, long len, char *p_outstr)         int count=1;         for(int i=0;i<len;i+)                            if(p_stri=p_stri+1)                                               count+;                                      else                                               if(count>1)                                                                 *p_outstr+ = count +'0'                                     *p_outstr+ =p_stri;                                                        else                                                                 *p_outstr+ =p_stri;                                                        count = 1;/注意其位置                                     *p_outstr = '0' void main()         char *str = "cccddecc"    printf("压缩之前的字符串为:%sn",str);         int len = strlen(str);         char * outstr = (char*)malloc(len*sizeof(char);         stringZip(str,len,outstr);         printf("压缩之后的字符串为:%sn",outstr);         free(outstr);         outstr = NULL;5.通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。 补充说明:1. 操作数为正整数,不需要考虑计算结果溢出的情况。2. 若输入算式格式错误,输出结果为“0”。 要求实现函数:void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr); 【输入】 pInputStr: 输入字符串lInputLen: 输入字符串长度【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;#include <stdio.h>#include<string.h>#include<stdlib.h>void arithmetic(const char *input, long len, char *output)         char s110;         char s210;         char s310;         int cnt = 0;         int len_input=strlen(input);         for(int i=0;i<len_input;+i)                            if(inputi=' ')                            cnt+;                   if(cnt!=2)                            *output+ = '0'                   *output = '0'                   return;                   sscanf(input,"%s %s %s",s1,s2,s3);         if(strlen(s2)!=1|(s20!='+'&&s20!='-')                            *output+ = '0'                   *output = '0'                   return;                    int len_s1=strlen(s1);         for(i=0;i<len_s1;i+)                            if(s1i<'0'|s1i>'9')                                               *output+ = '0'                            *output = '0'                            return;                                      int len_s3=strlen(s3);         for(i=0;i<len_s3;i+)                            if(s3i<'0'|s3i>'9')                                               *output+ = '0'                            *output = '0'                            return;                                      int x = atoi(s1);         int y = atoi(s3);         if(s20='+')                            int result = x+y;                   itoa(result,output,10);                  else if(s20='-')                            int result = x-y;                   itoa(result,output,10);                  else                            *output+ = '0'                   *output = '0'                   return;           void main()         char str = "10 - 23"         char outstr10;         int len = strlen(str);         arithmetic(str,len,outstr);         printf("%sn",str);         printf("%sn",outstr);      6.一组人(n个),围成一圈,从某人开始数到第三个的人出列,再接着从下一个人开始数,最终输出最终出列的人(约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3.n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。)#include <stdio.h>#include<string.h>#include<stdlib.h>#include<malloc.h> typedef struct Node         int data;         struct Node *next;LinkList; LinkList *create(int n)         LinkList *p,*q,*head;         int i=1;         p=(LinkList*)malloc(sizeof(LinkList);         p->data=i;         head=p;          for(i=1;i<=n;i+)                            q=(LinkList*)malloc(sizeof(LinkList);                   q->data=i+1;                   p->next=q;                   p=q;                  p->next=head;  /使链表尾连接链表头,形成循环链表         return head;         free(p);         p=NULL;         free(q);         q=NULL; void deletefun(LinkList *L,int m)         LinkList *p,*q,*temp;         int i;         p=L;          while(p->next!=p)                            for(i=1;i<m;i+)                                               q=p;                            p=p->next;                                      printf("%5d",p->data);                   temp=p;                   q->next=p->next;                   p=p->next;                   free(temp);                  printf("%5dn",p->data); int main()         int n=7,m=3;         LinkList *head1;         head1=create(n);         deletefun(head1,m);         return 0;7.输入一串字符,只包含“0-10”和“,”找出其中最小的数字和最大的数字(可能不止一个),输出最后剩余数字个数。如输入 “3,3,4,5,6,7,7”#include<stdio.h>#include<stdlib.h>#include<string.h> void main()         char str100;         printf("输入一组字符串:n");         scanf("%s",&str);          int len=strlen(str);         int array100;         int count=0;         for(int i=0;i<len;i+)                            if(stri>='0'&&stri<='9')                            arraycount+=stri-'0'                          arraycount='0'         int result=count;         int min=array0;         int max=array0;         for(int j=0;j<count;j+)                            if(max<arrayj)                            max=arrayj;                   else if(min>arrayj)                            min=arrayj;                  for(int k=0;k<count;k+)                            if(arrayk=min)                            result-;                   if(arrayk=max)                            result-;                  printf("%dn",result);8.输入一组身高在170到190之间(5个身高),比较身高差,选出身高差最小的两个身高;若身高差相同,选平均身高高的那两个身高;从小到大输出;如输入 170 181 173 186 190输出 170 173#include<stdio.h>#include<stdlib.h>#define N 5 int main()         int HeightN;         int dmin;         int H1,H2;         int i,j,temp;          printf("请输入一组身高在170到190之间的数据(共5个):n");         for(int k=0;k<N;k+)         scanf("%d",&Heightk);         printf("n");          for(i=0;i<N;i+)                   for(j=1;j<N-i&&Heightj-1>Heightj;j+)                                               temp=Heightj-1;                            Heightj-1=Heightj;                            Heightj=temp;                             H1=Height0;         H2=Height1;         dmin=H2-H1;         for(int m=2;m<N;m+)                            if(Heightm-Heightm-1<=dmin)                                               H1=Heightm-1;                            H2=Heightm;                            dmin=Heightm-Heightm-1;                                     printf("身高差最小的两个身高为:n");         printf("%d,%dn",H1,H2);         return 0;9.删除子串,只要是原串中有相同的子串就删掉,不管有多少个,返回子串个数。#include <stdio.h>#include <stdlib.h>#include <assert.h>#include <string.h>int delete_sub_str(const char *str,const char *sub_str,char *result)         assert(str != NULL && sub_str != NULL);         const char *p,*q;         char *t,*temp;         p = str;         q = sub_str;         t = result;         int n,count = 0;         n = strlen(q);         temp = (char *)malloc(n+1);         memset(temp,0x00,n+1);         while(*p)                            memcpy(temp,p,n);                   if(strcmp(temp,q) = 0 )                                               count+;                            memset(temp,0x00,n+1);                            p = p + n;                                      else                                                      *t = *p;                            p+;                            t+;                            memset(temp,0x00,n+1);                                            free(temp);         return count;void main()         char s100 = 0;         int num = delete_sub_str(“123abc12de234fg1hi34j123k”,”123”,s);         printf(“The number of sub_str is %drn”,num);         printf(“The result string is %srn”,s);10. 要求编程实现上述高精度的十进制加法。要求实现函数:void add (const char *num1, const char *num2, char *result)【输入】num1:字符串形式操作数1,如果操作数为负,则num10为符号位'-'num2:字符串形式操作数2,如果操作数为负,则num20为符号位'-'【输出】result:保存加法计算结果字符串,如果结果为负,则result0为符号位。#include<stdio.h> #include<stdlib.h> #include<string.h>   void move(char *str, int length)    /移除字母前的"-"符号        if(str0 != '-')         return;     int i;     for(i = 0; i < length-1; i+)         stri = stri+1;     stri = '0'   int remove_zero(char *result, int length)      int count = 0;     for(int i = length-1; i > 0; i-)    /从最后开始移除0,直到遇到非0数字,只对最初位置上的0不予判断              if(resulti = '0')                      resulti = '0'             count+;         else             return length-count;          return length - count;   void reverse(char *result, int length)        /将字符串倒转      char temp;     for(int i = 0; i <= (length-1)/2; i+)              temp = resulti;         resulti = resultlength-1-i;         resultlength-1-i = temp;        int real_add(char *str1, char *str2, char *result, const bool flag)      int len1 = strlen(str1);     int len2 = strlen(str2);     int n1, n2, another = 0;    /another表示进位     int cur_rs = 0;        /表示result的当前位数     int i, j;     int curSum;     for(i = len1-1, j = len2-1; i >= 0 && j >= 0; i-, j-)              n1 = str1i - '0'         n2 = str2j - '0'         curSum = n1 + n2 + another;         resultcur_rs+ = curSum % 10 + '0'         another = curSum / 10;           if(j < 0)              while(i >= 0)        /遍历str1剩余各位                      n1 = str1i- - '0'             curSum = n1 + another;             resultcur_rs+ = curSum % 10 + '0'             another = curSum / 10;                  if(another != 0)        /如果还有进位未加上             resultcur_rs+ = another + '0'               else                   while(j >= 0)                      n2 = str2j- - '0'             curSum = n2 + another;             resultcur_rs+ = curSum % 10 + '0'             another = curSum / 10;                  if(another != 0)             resultcur_rs+ = another + '0'           resultcur_rs = '0'      cur_rs = remove_zero(result, cur_rs);     if(!flag)              resultcur_rs+ = '-'         resultcur_rs = '0'          reverse(result, strlen(result);     return cur_rs;    int real_minus(char *str1, char *str2, char *result)    /使用str1减去str2      char big100, small100;     int big_len, sml_len;      int len1 = strlen(str1);     int len2 = strlen(str2);     bool flag = false;        /用于标记str2是否比str1大      if(len1 < len2)         flag = true;     else if(len1 = len2)              if(strcmp(str1, str2) = 0)                      result0 = '0'             result1 = '0'             return 1;         else if(strcmp(str1,str2) < 0)             flag = true;           if(flag)    /将str1和str2交换,确保str1指向的值是其中较大者,最后通过flag确定要不要给前面加-号              char *temp = str1;         str1 = str2;         str2 = temp;         len1 = strlen(str1);         len2 = strlen(str2);           int n1, n2, another = 0;    /another表示是否有借位     int i, j;     int cur_rs = 0;     int curMinus;  for(i = len1-1, j = len2-1; i>=0 && j>=0; i-,j-)              n1 = str1i - '0'         n2 = str2j - '0'         if(n1 >= n2+another)                      resultcur_rs+ = (n1-n2-another) +'0'             another = 0;                  else                      resultcur_rs+ = (n1+10-n2-another) + '0'             another = 1;                    while(i >= 0)              n1 = str1i- - '0'         if(another != 0)                      n1 -= another;             another = 0;                  resultcur_rs+ = n1 + '0'           resultcur_rs = '0'     cur_rs = remove_zero(result, cur_rs);     if(flag)              resultcur_rs+ = '-'         resultcur_rs = '0'          reverse(result, cur_rs);      return cur_rs;   void addi(const char *num1, const char *num2, char *result)      int len1 = strlen(num1);     int len2 = strlen(num2);     int rs_len;     if(!len1 | !len2)             return;     char str1100, str2100;     strncpy(str1, num1, len1);     str1len1 = '0'     strncpy(str2, num2, len2);     str2len2 = '0'      if(str10 = '-' && str20 = '-')              move(str1, len1);         move(str2, len2);         rs_len = real_add(str1, str2, result, false);     else if(str10 = '-')              move(str1, len1);         rs_len = real_minus(str2, str1, result);          else if(str20 = '-')              move(str2, len2);         rs_len = real_minus(str1, str2, result);     else         rs_len = real_add(str1, str2, result, true);   /int main(int argc, char *argv) int main()      char num1100,num2100;          printf("请输入两个整型数据:n");          scanf("%s%s",num1,num2);           char result100;     memset(result, 0, 100);     addi(num1,num2, result);     printf("%sn", result);      return 0; 11.描述:10个学生考完期末考试评卷完成后,A老师需要划出及格线,要求如下:(1) 及格线是10的倍数;(2) 保证至少有60%的学生及格;(3) 如果所有的学生都高于60分,则及格线为60分输入:输入10个整数,取值0100输出:输出及格线,10的倍数#include<stdio.h> void bubblesort(int arr)         int i,j,temp;         for(i=0;i<10;i+)                   for(j=0;j<9-i&&arrj>arrj+1;j+)                                               temp=arrj;                            arrj=arrj+1;                            arrj+1=temp;                    int GetPassLine(int a)         bubblesort(a);         if(a0>=60)                   return 60;         else                   return (int)a4/10)*10); main()         int a10=0;         int result;         printf("请随机输入10个成绩(0-100):n");         scanf("%d%d%d%d%d%d%d%d%d%d",&a0,&a1,&a2,&a3,&a4,&a5,&a6,&a7,&a8,&a9);         printf("n");         result=GetPassLine(a);         printf("及格线为:%dn",result);         return 1; 12.描述:一条长廊里依次装有n(1 n 65535)盏电灯,从头到尾编号1、2、3、n-1、n。每盏电灯由一个拉线开关控制。开始,电灯全部关着。有n个学生从长廊穿过。第一个学生把号码凡是1的倍数的电灯的开关拉一下;接着第二个学生把号码凡是2的倍数的电灯的开关拉一下;接着第三个学生把号码凡是3的倍数的电灯的开关拉一下;如此继续下去,最后第n个学生把号码凡是n的倍数的电灯的开关拉一下。n个学生按此规定走完后,长廊里电灯有几盏亮着。注:电灯数和学生数一致。输入:电灯的数量输出:亮着的电灯数量样例输入:3样例输出:1 #include<stdio.h>#define Max_Bubl_Num 65535 int GetLightLampNum(int n)         int BublNumMax_Bubl_Num=0;  /0表示灯灭,1表示灯亮         unsigned int i,j;         unsigned int count=0;         for(i=1;i<=n;i+)                   for(j=i;j<=n&&j%i=0;j+)                                               BublNumj-1+=1;                            BublNumj-1=BublNumj-1%2;                             for(int k=0;k<n;k+)                            if(BublNumk=1)                            count+;                  return count;int main()         int n,result;         printf("请输入灯的数量(1-65535):n");         scanf("%d",&n);         result=GetLightLampNum(n);         printf("最后亮灯的数量为:%dn",result);   
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 课件教案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!