Java俱乐部社团面试记录

Java俱乐部社团面试记录

1.申请表

自身能拿出手的太少了,能写在申请表上的除了自己的热情,近乎没有了

2.个人介绍

自己过于紧张,一进门脑子只剩空气了

3.询问知识点

知识点

知识面拓展有点少,问了两个还是三个不知道的名词,且没有将自己的优势展示出来

编程

问:如何对一个数开根号后上取整,不限语言?

答:c++ round(sqrt(x))

问:如何不用round函数而使用floor函数?

答:(没答,忘了)

Answer:floor(sqrt(x)+0.5f)

4.询问个人情况

记得也不写出来

5.编程题

给了四道编程题,都是leetcode原题,基本都是简单题,交上去回到宿舍之后在leetcode上写都是通过,但是当时的时候实在脑抽,思维完全跟不上私下写题

提交不能得到分数,类似OI赛事

虽然是leetcode原题,但是他没有给leetcode样式的提交类,导致我是写的输入输出,不知道能不能得分

第一题

2119. 反转两次的数字 - 力扣(LeetCode)

签到题,只需要判断最后一位是否为为0即可,注意特判当这个数为0时的值

第二题

172. 阶乘后的零 - 力扣(LeetCode)

前置(BY:leetcode官方题解)

n! 尾零的数量即为 n! 中因子 10 的个数,而 10=2×5,因此转换成求 n! 中质因子 2 的个数和质因子 5 的个数的较小值。

由于质因子 5 的个数不会大于质因子 2 的个数,我们可以仅考虑质因子 5 的个数。

错解

一眼质因子分解,怪自己的数论能力太差,场上只想到了统计5的个数,不知道怎么继续算了,最后只写了个应该是O(n)的算法,且会longlong溢出

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    int ans = 0;
    long long now = 1;
    for (int i = 1; i <= n; ++i) {
        now *= i;
        while (now % 10 == 0) ++ans, now /= 10;
    }
    cout << ans << endl;
    return 0;
}

正解

\[\because对于正整数x和p,在xp<=n的范围内,1p,2p,3p...xp均为[1,n]范围内是p的倍数的数 \]

\[\therefore x=\Big\lfloor\frac{n}{p}\Big\rfloor \]

\[\therefore[1,n]中是p的倍数的数的个数为\Big\lfloor\frac{n}{p}\Big\rfloor \]

\[[1, n]中p的倍数有n_{1}=\left\lfloor\frac{n}{p}\right\rfloor 个,这些数至少贡献出了n_{1}个质因子p。 \]

\[p^{2}的倍数有n_{2}=\left\lfloor\frac{n}{p^{2}}\right\rfloor个,由于这些数已经是 p 的倍数了,为了不重复统计 p 的个数,我们仅考虑额外贡献的 质因子个数 \]

\[即这些数额外贡献了至少n_{2}个质因子p。 \]

\[依此类推, [1, n] 中质因子 p 的个数为\sum_{k=1}\left\lfloor\frac{n}{p^{k}}\right\rfloor \]

所以可以得出以下代码

#include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin >> n;
    int ans = 0;
    for (int i = 5; i <= n; i *= 5) {
        ans += n / i;
    }
    cout << ans << endl;
    return 0;
}

\[又\because\Big\lfloor\frac{n}{p^k}\Big\rfloor=\Big\lfloor\frac{\frac{n}{p^{k-1}}}{p}\Big\rfloor(另一种理解就是每次将p除去,再计算p的倍数的个数) \]

所以可以得出以下更改后的代码

#include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin >> n;
    int ans = 0;
    while (n) ans += n /= 5;
    cout << ans << endl;
    return 0;
}

第三题

64. 最小路径和 - 力扣(LeetCode)

入门dp题,不过由于场上没给leetcode式的class类,导致不知道他给不给变量m和n,就这样吧

#include <bits/stdc++.h>
using namespace std;
int main() {
    int m, n;
    cin >> m >> n;
    vector<vector<int>> dp(m, vector<int>(n));
    for (int i = 0; i < m; ++i)
        for (int j = 0; j < n; ++j) cin >> dp[i][j];
    for (int i = 1; i < n; ++i) dp[0][i] += dp[0][i - 1];
    for (int i = 1; i < m; ++i) dp[i][0] += dp[i - 1][0];
    for (int i = 1; i < m; ++i)
        for (int j = 1; j < n; ++j)
            dp[i][j] += min(dp[i - 1][j], dp[i][j - 1]);
    cout << dp[m - 1][n - 1] << endl;
    return 0;
}

第四题

50. Pow(x, n) - 力扣(LeetCode)

非常简单的快速幂放在了最后一道,可是我不知道的是int类型的最小值取绝对值会超int范围,真·不开longlong见祖宗

#include <bits/stdc++.h>
using namespace std;
double qpow(double x, long long n) {
    double ans(1);
    for (long long b = abs(n); b; b >>= 1, x = x * x)
        if (b & 1) ans = x * ans;
    return n < 0 ? 1.0 / ans : ans;
}
int main() {
    double x;
    long long n;
    cin >> x >> n;
    cout << qpow(x, n) << endl;
    return 0;
}
posted @ 2022-10-07 21:57  Cattle_Horse  阅读(72)  评论(0编辑  收藏  举报