欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

秋招笔试题目

程序员文章站 2022-06-09 09:46:24
...

一.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);
}
相关标签: 笔试 算法