华为校招上机题

2 篇文章 0 订阅

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 0
int 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值