0703-蔚来编程题

coding-1

查询一棵二叉树有多少个“既有左孩子也有有孩子”的节点

int maxSum(TreeNode* root) {
    int ans;   
    preOrder(root, ans);
    return ans;
    }
			
void preOrder(TreeNode* root, int& ans) {
        // 遍历结束条件
        if (root == nullptr) {
            return;
        }
        if (root->left != nullptr && root->right != nullptr) {
            ++ans;
        }
        preOrder(root->left);
        preOrder(root->right);
    }

coding-2

给出六根棍子,能否选出三根拼成一个三角形的同时,剩下三根也能组成一个三角形

/*
	judge函数:
	通过三次for循环嵌套,取出三条边temp1,temp2,temp3;
	将temp对应的nums置为0
	另外三条边放入v2
	恢复nums
*/
#include <iostream>
#include <algorithm>
bool judge(vector<int>& nums) {	
    for (int i = 0; i <= 3; ++i) {
        int temp1 = nums[i];
        nums[i] = 0;
        for (int j = i + 1; j <= 4; ++j) {
            int temp2 = nums[j];
            nums[j] = 0;
            for (int z = j + 1; z <= 5; ++z) {
                int temp3 = nums[z];
                nums[z] = 0;
                if (temp1 + temp2 > temp3) {
                    vector<int> v2;
                    for (auto& it : nums) {
                        if (it) {
                            v2.emplace_back(it);
                        }
                    }
                    if (v2[0] + v2[1] > v2[2]) {
                        return true;
                    }
                }
                nums[z] = temp3;
                break;
            }
            nums[j] = temp2;
        }
        nums[i] = temp1;
    }
    return false;
}
int main() {
    vector<int> nums(6);
    for (int count = 0; count <= 5; ++count) {
        cin >> nums[count];
    }
    sort(nums.begin(), nums.end());
    if (judge(nums)) {
        cout << "yes";
    }
    else{
        cout << "false";
    }
    return 0;
}
/*
	枚举判断,不断尝试,慢慢扩大
*/
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		int[] a = new int[6];
		for (int i = 0; i < t; i++) {
			for (int j = 0; j < 6; j++)
				a[j] = sc.nextInt();
			Arrays.sort(a);
			if (a[0] + a[1] > a[2] && a[3] + a[4] > a[5] || a[0] + a[2] > a[3] && a[1] + a[4] > a[5]
					|| a[0] + a[3] > a[4] && a[1] + a[2] > a[5] || a[0] + a[4] > a[5] && a[1] + a[2] > a[3])
				System.out.println("Yes");
			else
				System.out.println("No");
		}
		System.out.println();
	}
}

coding-3

有两个电梯,N人依次乘坐电梯,已知第i人到达目标层再返回一楼所用时间为ai

若无空闲电梯,则需要等待

现在所有人都到达目标层共需要多久?

#include <iostream>
using namespace std;
int main()
{
    int n;
    cin >> n;
    int lift1 = 0;
    int lift2 = 0;
    for (int i = 0; i < n; i++)
    {
        int m;
        cin >> m;
        if (lift1 <= lift2)
        {
            lift1 += m;
        }
        else
        {
            lift2 += m;
        }
    }
    int ans = max(lift1, lift2);
    cout << ans;
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值