秋招笔试题目

一.TCL

1.输入十进制的字符串,输出十六进制的字符串(会溢出)

void DecToBin()
{
    string s;
    while (cin >> s){
        int n = 0;
        // 将字符串转换为int类型
        for (int i = s.length() - 1, j = 1; i >= 0; i--) {
            n += (s[i] - '0') * j;
            j *= 10;
        }
        // 运用短除法不断除以16取余数,并将其加入字符串结果中
        string result = "";
        char _16[] = {
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
        };
        // 16进制,除以16
        const int radix = 2;
        while (n) {
            int i = n % radix;          // 余数
            result = _16[i] + result;   // 将余数对应的十六进制数字加入结果
            n /= radix;                 // 除以16获得商,最为下一轮的被除数
        }
        cout << result << endl;
    }
}

2.输入十进制整形,输出十六进制字符串

void DecToHex(int n )
{
        // 运用短除法不断除以16取余数,并将其加入字符串结果中
        string result = "";
        char _16[] = {
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
        };
        // 16进制,除以16
        const int radix = 16;
        while (n) {
            int i = n % radix;          // 余数
            result = _16[i] + result;   // 将余数对应的十六进制数字加入结果
            n /= radix;                 // 除以16获得商,最为下一轮的被除数
        }
        cout << result << endl;
}

3.输入十进制字符串,输出十六进制字符串,无溢出

void DocStrToHexStr(string& sSource,string& sDesc)
{
    sDesc.clear();

    std::string sTmp = sSource;// 被除数
    std::string sSubSrc;// 除法整数倍
    std::string sSubDesc;// 缓存16进制逆序结果
    while (sTmp.length() > 0) {
        int dwZhengShu = 0;// 整数
        int dwYuShu = 0;// 余数
        int dwSubSrc = 0;// 可被16整除的最小整数
        for (int i = 0; i < sTmp.length(); i++) {
            dwSubSrc *= 10;
            dwSubSrc += sTmp[i] - '0';
            dwYuShu = dwSubSrc;
            if (dwSubSrc >= 16) {
                dwZhengShu = dwSubSrc / 16;
                dwYuShu = dwSubSrc % 16;
                sSubSrc.push_back(dwZhengShu + '0');
                dwSubSrc = dwYuShu;
            } else if (sSubSrc.length()) {
                sSubSrc.push_back('0');
            }
        }

        if (dwYuShu >= 10)
            sSubDesc.push_back('A' + dwYuShu - 10);

        if (sSubSrc.empty()) {
            if (dwSubSrc >= 10)
                sSubDesc.push_back('A' + dwSubSrc - 10);
            else
                sSubDesc.push_back(dwSubSrc + '0');
        } else {
            if (dwYuShu < 10)
                sSubDesc.push_back(dwYuShu + '0');
        }

        sTmp = sSubSrc;
        sSubSrc.clear();
    }

    for (auto it = sSubDesc.rbegin()+1; it != sSubDesc.rend(); ++it) {
        sDesc.push_back(*it);// 逆序输出
    }
}

2.员工信息表

struct staff_info
{
    char name[40];
    int data;
    struct staff_info *next;
};
struct staff_info *compute_salary(const char *s)
{
    struct staff_info * head = NULL;
//    head->next = NULL;
//    head->data = 0;
//    memset( head->name,'\0',sizeof( head->name));
    struct staff_info *cur = head;
    struct staff_info *pre = head;

    string  maxname;

    int nameindex=0;

    string salary;

    int salaryint;
    int maxsalary = INT_MIN;

   while (*s != '\0')
   {
       struct staff_info * temp = new staff_info;
       temp->next = nullptr;
       memset( temp->name,'\0',sizeof( temp->name));
        if (*s=='>')
        {
            s ++;
            while (*s != '$' &&*s != '\0')
            {
                temp->name[nameindex] = *s;
                s++;
                nameindex ++;
            }
            s++;
            while (*s != '>'&&*s != '\0')
            {
                salary += *s;
                s++;
            }
            salaryint = atoi(salary.c_str());
            if (salaryint>maxsalary)
            {
                for (int i = 0; i < nameindex; ++i)
                {
                    maxname += temp->name[i];
                }

                maxsalary = salaryint;
            }
//            maxsalary = max(maxsalary,salaryint);

        }
        nameindex = 0;
        salary = "";
       temp->data =salaryint;
        if (!head)
        {
            head = temp;
            cur = temp;
            pre = cur;
        }
        else
        {
            cur->next = temp;
            pre = cur;
            cur = cur->next;
        }
   }
   cur->next = head;
   cout<<maxname<<endl;
   cout<<maxsalary;
   return head;
}
void  compute_salaryTest()
{
    char *ns= ">Zhang$12000>Wang$9114>Li$8546>Hu$0>Andy$7200";
    compute_salary(ns);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Michael.Scofield

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值