You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

面试中Java代码功能困惑及count0函数逻辑疑问

面试后是否应告知面试官代码逻辑问题?

我面试数据分析师实习生岗位时,被问及一段Java代码的功能。该岗位仅要求掌握Python,作为物理专业学生,我混淆了Java与Python的除法规则,误判count1函数存在问题。对于count0函数,面试官称其应返回0或1,但我按代码逻辑用Python实现测试后,结果并非如此。现咨询是否应礼貌告知面试官count0函数可能存在逻辑错误。

Java原代码

class Case {

    static int count1(int n) {
        if (n == 0) {
            return 0;
        }
        int m = n / 2;
        if (2 * m != n) {
            return 1 + count1(m);
        }
        return count1(m);
    }

    static int count0(int n) {
        if (n == 0) {
            return 1;
        }
        if (n == 1) {
            return 0;
        }
        int m = n % 2;
        return count0((n - m) / 2) + 1 - m;
    }

    static boolean equivalent(int n, int m) {
        return ((count1(n) == count1(m)) && (count0(n) == count0(m)));
    }
}

Python测试代码及结果

测试代码

# 按照Java代码逻辑实现count0函数
def count0(n):
    if n == 0:
        return 1
    if n == 1:
        return 0
    m = n % 2
    return count0((n - m) // 2) + 1 - m

# 计算n从0到20的count0结果
results_count0 = {n: count0(n) for n in range(21)}

results_count0

测试结果

{0: 1, 1: 0, 2: 1, 3: 0, 4: 2, 5: 1, 6: 1, 7: 0, 8: 3, 9: 2, 10: 2, 11: 1, 12: 2, 13: 1, 14: 1, 15: 0, 16: 4, 17: 3, 18: 3, 19: 2, 20: 3}

内容的提问来源于stack exchange,提问作者Tomy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 00:05:10