0%

无端科技笔试4月11日

岗位

游戏开发工程师-在研SOC项目(J10664)

题目分配

30道选择题,2道编程题。

作答时间

120分钟

做题感受

  • 选择题总体感觉良好,没有偏题怪题,考察基本数据结构、操作系统、面向对象、设计模式等知识。
  • 编程题包含两题,第一题很简单,一个类似于图片按倍数放大,求最终像素。第二题开始上强度了,输入一个字符串,形为"(num1)(operator)(num2)" (不含括号),其中 num1num2 是一个包含小数点的实数,operator+ - * / 的一种,让你算出表达式的值并保留4位小数返回,其中字符串长度 3<=N<=50
  • 分析:因为 N 最长能为 50 ,即数字的长度可能超过可表示的最大长度,所以这是一道大数运算题
  • 结果:写了大数的加减法,过了30%
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    #include <cstddef>
    #include <cstring>
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;

    struct StringNum
    {
    vector<char> nums;

    int dot;
    string to_string()
    {
    int size = nums.size();
    char* s = new char[size + 1];
    memcpy(s, nums.data(), size);
    size_t index = size - dot;
    s[size] = '\0';
    string str = s;
    str.insert(index, 1, '.');
    return str;
    }

    string add(StringNum& b)
    {
    int delta = dot - b.dot;
    if (delta > 0)
    {
    for (int i = 0; i < delta; i++)
    {
    b.nums.emplace_back('0');
    }
    }
    else if (delta < 0)
    {
    for (int i = 0; i < -delta; i++)
    {
    nums.emplace_back('0');
    }
    }
    int new_dot = max(dot, b.dot);
    dot = new_dot;
    b.dot = new_dot;
    auto it_a = nums.rbegin();
    auto it_b = b.nums.rbegin();
    vector<char> result;
    int carry = 0;
    while (it_a != nums.rend() || it_b != b.nums.rend())
    {
    int a = it_a == nums.rend() ? 0 : *it_a - '0';
    int bb = it_b == b.nums.rend() ? 0 : *it_b - '0';
    int sum = a + bb + carry;
    carry = sum / 10;
    result.emplace_back(sum % 10 + '0');
    if (it_a != nums.rend())
    it_a++;
    if (it_b != b.nums.rend())
    it_b++;
    }
    if (carry)
    result.emplace_back(carry + '0');
    StringNum result_num;
    for (int i = 0; i < result.size() / 2; i++)
    {
    swap(result[i], result[result.size() - i - 1]);
    }
    result_num.nums.swap(result);
    result_num.dot = dot;
    if (dot > 4)
    {
    for (int i = 0; i < dot - 4; i++)
    {
    nums.pop_back();
    b.nums.pop_back();
    result_num.nums.pop_back();
    }
    }
    else
    {
    for (int i = 0; i < 4 - dot; i++)
    {
    nums.emplace_back('0');
    b.nums.emplace_back('0');
    result_num.nums.emplace_back('0');
    }
    }
    dot = 4;
    b.dot = 4;
    result_num.dot = 4;
    return result_num.to_string();
    }

    string minus(StringNum& b)
    {
    int delta = dot - b.dot;
    if (delta > 0)
    {
    for (int i = 0; i < delta; i++)
    {
    b.nums.emplace_back('0');
    }
    }
    else if (delta < 0)
    {
    for (int i = 0; i < -delta; i++)
    {
    nums.emplace_back('0');
    }
    }
    int new_dot = max(dot, b.dot);
    dot = new_dot;
    b.dot = new_dot;
    auto it_a = nums.rbegin();
    auto it_b = b.nums.rbegin();
    vector<char> result;
    int carry = 0;
    while (it_a != nums.rend() || it_b != b.nums.rend())
    {
    int a = it_a == nums.rend() ? 0 : *it_a - '0';
    int bb = it_b == b.nums.rend() ? 0 : *it_b - '0';
    int sum = a - bb + carry;
    if (sum < 0)
    {
    sum += 10;
    carry = -1;
    }
    else
    {
    carry = 0;
    }
    result.emplace_back(sum + '0');
    if (it_a != nums.rend())
    it_a++;
    if (it_b != b.nums.rend())
    it_b++;
    }
    if (carry)
    result.emplace_back(carry + '0');
    StringNum result_num;
    for (int i = 0; i < result.size() / 2; i++)
    {
    swap(result[i], result[result.size() - i - 1]);
    }
    result_num.nums.swap(result);
    result_num.dot = dot;
    if (dot > 4)
    {
    for (int i = 0; i < dot - 4; i++)
    {
    nums.pop_back();
    b.nums.pop_back();
    result_num.nums.pop_back();
    }
    }
    else
    {
    for (int i = 0; i < 4 - dot; i++)
    {
    nums.emplace_back('0');
    b.nums.emplace_back('0');
    result_num.nums.emplace_back('0');
    }
    }
    dot = 4;
    b.dot = 4;
    result_num.dot = 4;
    return result_num.to_string();
    }
    };

    int main() {
    vector<char> a;
    vector<char> b;
    vector<char>* cur = &a;
    char c;
    string operator_type = "";
    int count = 0;
    auto* a_s = new StringNum;
    auto* b_s = new StringNum;
    while (cin >> c)
    {
    if (c >= '0' && c <= '9')
    {
    cur->emplace_back(c);
    count++;
    }
    else if (c == '+')
    {
    operator_type = "add";
    a_s->nums.swap(*cur);
    a_s->dot = count;
    count = 0;
    cur = &b;
    }
    else if (c == '-')
    {
    operator_type = "minus";
    a_s->nums.swap(*cur);
    a_s->dot = count;
    count = 0;
    cur = &b;
    }
    else if (c == '*')
    {
    operator_type = "multip";
    a_s->nums.swap(*cur);
    a_s->dot = count;
    count = 0;
    cur = &b;
    }
    else if (c == '/')
    {
    operator_type = "div";
    a_s->nums.swap(*cur);
    a_s->dot = count;
    count = 0;
    cur = &b;
    }
    else if (c == '.')
    {
    count = 0;
    }
    else
    {
    operator_type = "Invalid";
    }
    }
    b_s->nums.swap(*cur);
    b_s->dot = count;
    //cout << a_s->to_string() << endl;
    //cout << b_s->to_string() << endl;
    if (operator_type == "Invalid")
    cout << "Invalid operation!";
    else if (operator_type == "add")
    {
    auto ans = a_s->add(*b_s);
    cout << a_s->to_string() << '+' << b_s->to_string() << '=' << ans;
    }
    else if (operator_type == "div")
    {

    }
    else if (operator_type =="minus")
    {
    auto ans = a_s->minus(*b_s);
    cout << a_s->to_string() << '-' << b_s->to_string() << '=' << ans;
    }
    else if (operator_type == "multip")
    {

    }
    }
    // 64 位输出请用 printf("%lld")