MST

星途 面试题库

面试题:Rust布尔类型在复杂条件判断中的运用

假设你有一个布尔类型的函数`check_condition`,它接受三个参数:一个整数`num`,一个字符串`str`,一个布尔值`bool_val`。当`num`大于10,并且`str`的长度大于5,同时`bool_val`为`true`时,函数返回`true`,否则返回`false`。请实现这个函数,并在`main`函数中调用它,传入不同的参数进行测试。
36.2万 热度难度
编程语言Rust

知识考点

AI 面试

面试题答案

一键面试
def check_condition(num, str, bool_val):
    return num > 10 and len(str) > 5 and bool_val

def main():
    test1_result = check_condition(15, "hello world", True)
    test2_result = check_condition(5, "hello", False)
    test3_result = check_condition(12, "longer string", True)

    print(f"Test 1 result: {test1_result}")
    print(f"Test 2 result: {test2_result}")
    print(f"Test 3 result: {test3_result}")

if __name__ == "__main__":
    main()
class Main {
    static boolean check_condition(int num, String str, boolean bool_val) {
        return num > 10 && str.length() > 5 && bool_val;
    }

    public static void main(String[] args) {
        boolean test1_result = check_condition(15, "hello world", true);
        boolean test2_result = check_condition(5, "hello", false);
        boolean test3_result = check_condition(12, "longer string", true);

        System.out.println("Test 1 result: " + test1_result);
        System.out.println("Test 2 result: " + test2_result);
        System.out.println("Test 3 result: " + test3_result);
    }
}
#include <iostream>
#include <string>

bool check_condition(int num, const std::string& str, bool bool_val) {
    return num > 10 && str.length() > 5 && bool_val;
}

int main() {
    bool test1_result = check_condition(15, "hello world", true);
    bool test2_result = check_condition(5, "hello", false);
    bool test3_result = check_condition(12, "longer string", true);

    std::cout << "Test 1 result: " << (test1_result? "true" : "false") << std::endl;
    std::cout << "Test 2 result: " << (test2_result? "true" : "false") << std::endl;
    std::cout << "Test 3 result: " << (test3_result? "true" : "false") << std::endl;

    return 0;
}

以上分别给出了Python、Java、C++三种语言实现该功能的代码,以满足不同语言的面试要求。

  1. Python代码
    • check_condition函数通过逻辑与(and)连接三个条件判断是否满足要求。
    • main函数调用check_condition函数并传入不同参数进行测试,最后打印测试结果。
  2. Java代码
    • check_condition方法同样使用逻辑与(&&)连接条件判断。
    • main方法进行测试并打印结果。
  3. C++代码
    • check_condition函数使用逻辑与(&&)判断条件。
    • main函数进行测试,通过条件运算符将bool值转换为字符串输出。