字节笔试4/10

1

642624f446ad4acd84e130df375da10.jpg
3e3ed50707c70b8388a425341b079cf.jpg
52088409faa5776d95fb3639d55af2d.jpg
d03fdfa84f4708885bad2648711b620.jpg
aa5ba3e653c3a30dee959ccdad1b1ff.jpg
e08245c98c90d48367858167b7fc0ba.jpg

  1. #include <iostream>
  2. using namespace std;
  3. int dx[4] = {-1, 0, 1, 0}, dy[4] = {0,-1, 0, 1}; //上左下右
  4. int main() {
  5. int t;
  6. cin >> t;
  7. while(t --)
  8. {
  9. //输入
  10. int m, n;
  11. cin >> m >> n;
  12. int a[m][n];
  13. string row;
  14. //每一行用字符串读入,若用int读入,由于没有分隔符,会将一整行读入为一个数,如1011
  15. for(int i = 0; i < m; i ++)
  16. {
  17. cin >> row;
  18. for(int j = 0; j < n; j ++)
  19. {
  20. a[i][j] = row[j] - '0';
  21. }
  22. }
  23. /*
  24. 1011
  25. 0000
  26. 0010
  27. 可能存在先修改a[0][2]值为0,此时a[0][3]左下均为0,也要修改为0的问题
  28. 前面的修改会影响后面的判断
  29. 解决:不修改a[i][j]的值,直接输出0/1
  30. */
  31. for(int i = 0; i < m; i ++)
  32. {
  33. for(int j = 0; j < n; j ++)
  34. {
  35. if(a[i][j] == 0) cout << 0;
  36. if(a[i][j] == 1)
  37. {
  38. int cnt = 0;
  39. //遍历下左上右
  40. for(int k = 0; k < 4; k ++)
  41. {
  42. int nx = i + dx[k], ny = j + dy[k];
  43. if(nx >= 0 && nx < m && ny >= 0 && ny < n && a[nx][ny] == 0) cnt ++;
  44. }
  45. if(cnt >= 2) cout << 0;
  46. else cout << 1;
  47. }
  48. }
  49. cout << endl;
  50. }
  51. }
  52. return 0;
  53. }

2

e5b3aedb0b1496b0e8a828b4b4f53a8.jpg
0e6909612d4a83411cf8ded63886770.jpg
23aadf0c655ad144e455cfc0f6d2301.jpg
7a617b630289952e23b4336d93f23fb.jpg
cd680f9a5fd34cec1b86a868c97c780.jpg

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int main() {
  5. int n;
  6. cin >> n;
  7. vector<int> energy(n, 0);
  8. for(int i = 0; i < n; i ++)
  9. cin >> energy[i];
  10. int most_r = 0;
  11. bool flag = true;
  12. for(int i = 0; i < n; i ++)
  13. {
  14. if(i > most_r)
  15. {
  16. flag = false;
  17. break;
  18. }
  19. most_r = max(most_r, i + energy[i]);
  20. }
  21. if(flag) cout << "TRUE";
  22. else cout << "FALSE";
  23. return 0;
  24. }

字节笔试3/20

1
(MKF15[_@P){GDE]4HF889H.jpg

  1. #include <iostream>
  2. #include <unordered_map>
  3. using namespace std;
  4. int main() {
  5. string str;
  6. cin >> str;
  7. unordered_map<char, int> mp;
  8. bool ans = true;
  9. for (char& ch : str) {
  10. if (ch >= 'A' && ch <= 'Z')
  11. mp[ch + 32]++;
  12. else if (ch >= 'a' && ch <= 'z')
  13. mp[ch]++;
  14. else
  15. mp['*']++;
  16. }
  17. bool flag = true;
  18. for (auto it = mp.begin(); it != mp.end(); it++) {
  19. int count = it->second;
  20. if (count % 2 == 0) continue;
  21. if (flag && count % 2 == 1) flag = false;
  22. else {
  23. ans = false;
  24. break;
  25. }
  26. }
  27. if (ans) cout << "true";
  28. else cout << "false";
  29. return 0;
  30. }

2
T%X_RT9T%OUI%_Y2(86%5}D.jpg

![WR(3{IB57FC7L~$R281)4V.jpg![CL9VA06U05~UUJY6EY2IN6.jpg

#include <iostream>
#include <vector>

using namespace std;

int main() {
    int m, n;
    cin >> m >> n;
    vector<vector<int>> a(n, vector<int>(2));
    for (int i = 0; i < n; i++) {
        cin >> a[i][0] >> a[i][1];
    }
    int total = cost(m, a, n);
    return 
}

int cost(int m, vector<vector<int>>& a, int idx) {
    if (idx == 0) return 0;
    int totalprice = 0;
    int minidx = idx + 1, minprice = INT_MAX;
    for (int i = 0; i < idx; i++) {
        if (a[i][1] < minprice) {
            minidx = i;
            minprice = a[i][1];
        }
    }
    totalprice += (m - a[minidx][0]) * a[minidx][1] + cost(a[minidx][0], a, minidx);
    return totalprice;
}

3

TBXQQ(6PNGW@[JUVQ]8~8I9.jpg(F30YE}NFIKU29GVD0Y4F01.jpg

![2]_IU_9DCKY2T9`~{B93OP.jpg
4
9C){MYX47H6(1MQHASFE8)W.jpg