华为校招上机题

来源:互联网 发布:慢走丝编程 编辑:程序博客网 时间:2024/04/30 11:27

1.兔子爬洞问题:兔子白天爬出5米,晚上又掉下去2米。问给定洞的深度,兔子要爬多少天

#include<iostream>#include<memory.h>using namespace std;int str2int(const char *str);int main(int argc,char **argv){//    char *ch=argv[1];    //int n = (int)(*ch);    //n=n-48;    int n=str2int(argv[1]);    cout<<"n="<<n<<endl;    int l=0;    int day=1;    while(l<n)    {        //白天        l=l+5;        if(l>=n)        {            cout<<"l="<<l<<endl;            break;        }        else        {            l=l-2;        }        day=day+1;        cout<<"day="<<day<<endl;    }    cout<<"一共用"<<day<<"天";    return 0;}int str2int(const char *str){    int temp=0;    const char *ptr=str;//ptr保存str字符串头    if(*str == '-' || *str =='+' )    {        str++;    }    while(*str !=0)    {        if((*str <'0')||(*str >'9'))        {            break;//当当前字符不是数字,退出循环        }        temp=temp*10+(*str-'0');//如果当前字符是数字就当数值计算        str++;    }    if(*ptr =='-')//如果字符串是以'-'开头,转换成其相反数    {        temp=-temp;    }    return temp;}

2.将给定字符串中的大写字母变小写,小写字母变大写,数字不变,其它字符和空格不算。然后把改好的存在另外一个数组里。

#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAXLEN 200#define bool int#define TRUE 1#define FALSE 0int main(){    unsigned int an1[MAXLEN+10];    unsigned int an2[MAXLEN+10];    unsigned int result[MAXLEN*2+10];    char szline1[MAXLEN+10];    char szline2[MAXLEN+10];    bool bstartoutput=FALSE;    int i,j;    scanf("%s",szline1);    scanf("%s",szline2);    memset(an1,0,sizeof(an1));    memset(an2,0,sizeof(an2));    memset(result,0,sizeof(result));    j=0;    for(i=strlen(szline1)-1;i>=0;i--)    {        an1[j++]=szline1[i]-'0';    }    j=0;    for(i=strlen(szline2)-1;i>=0;i--)    {        an2[j++]=szline2[i]-'0';    }    for(i=0;i<strlen(szline2);i++)    {        for(j=0;j<strlen(szline1);j++)        {            result[i+j]+=an2[i]*an1[j];            // printf("%d",result[i+j]);        }    }    for(i=0;i<MAXLEN*2;i++)    {        if(result[i]>=10)        {            result[i+1] +=result[i]/10 ;            result[i]%=10;        }    }    for(i=MAXLEN*2;i>=0;i--)    {        if(bstartoutput)        {            printf("%d",result[i]);        }        else if(result[i])        {            printf("%d",result[i]);            bstartoutput=TRUE;        }        // if(!bstartoutput)        // {            // printf("");        // }    }    printf("\n");return 0;}

3.高精度乘法,一个是0-64位的数,一个是0至9间的数,求相乘结果。

#include<stdio.h>#include<stdlib.h>#include<string.h>void strchange(const char *src,char *des){    while(*src)    {        if (*src>='a'&& *src<='z')        {            *des = *src - 'a' + 'A';        }        else if(*src>='A'&& *src<='Z')        {            *des= *src + 'a'-'A';        }        des++;        src++;    }}int main(){    //printf("%d",'a'-'A');    char strTest[100];    scanf("%s",strTest);    char strResult[100];    strchange(strTest,strResult);    printf("%s",strResult);    return 0;}


原创粉丝点击