【笔试记录】22/07/14 蔚来测开笔试(java)

3 篇文章 0 订阅
1 篇文章 0 订阅

选择题

很多Linux相关和SQL相关,有些忘了.jpg

第一题 折扣

去网上买东西,双十一打七折,双十二打八折,要是有优惠券(1/有 0没有)就再减50,问价格(输出保留两位小数)
输入:四个数,代表价格、月、日、有无优惠券(日月只可能是1111或1212)
输出:保留两位小数

思路

  1. 无脑暴力,但是输出要注意小数怎么保留
double res = money(price, month, day, ifSale);
DecimalFormat df = new DecimalFormat("0.00");
  1. 在*0.7的时候会丢失精度,用BigDecimal维持精度
BigDecimal count1 = new BigDecimal("0.7");
res = res1.multiply(count1).doubleValue();

程序

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        double price = in.nextDouble();
        int month = in.nextInt();
        int day = in.nextInt();
        int ifSale = in.nextInt();

        double res = money(price, month, day, ifSale);
        DecimalFormat df = new DecimalFormat("0.00");
        System.out.println(df.format(res));
    }
    public static double money(double price, int month, int day, int ifSale) {
        double res = price;
        BigDecimal res1 = new BigDecimal(Double.toString(res));
        if(month == 11 && day == 11) {
            BigDecimal count1 = new BigDecimal("0.7");
            res = res1.multiply(count1).doubleValue();
            if(ifSale == 1) res = (res - 50 >= 0) ? res - 50 : 0;
        }
        if(month == 12 && day == 12) {
            BigDecimal count2 = new BigDecimal("0.8");
            res = res1.multiply(count2).doubleValue();
            // res = (long) (res * 0.8f);
            if(ifSale == 1) res = (res - 50 >= 0) ? res - 50 : 0;
        }
        return res;
    }
}

第二题 兑换零钱

力扣原题:零钱兑换

思路:动态规划

/**
dp[j]: 凑足j所需要最少的硬币数
dp[j] = min(dp[j-coins[i]]+1, dp[j])
初始化:首先凑足总金额为0所需钱币的个数一定是0,那么dp[0] = 0;
考虑到递推公式的特性,dp[j]必须初始化为一个最大的数,否则就会在min(dp[j - coins[i]] + 1, dp[j])比较的过程中被初始值覆盖。
 */

class Solution {
    public int coinChange(int[] coins, int amount) {
        int[] dp = new int[amount + 1];
        dp[0] = 0;
        for(int i = 1; i < dp.length; i++){
            dp[i] = Integer.MAX_VALUE;
        }
        for(int i = 0; i < coins.length; i++){
            for(int j = coins[i]; j <= amount; j++){
                if(dp[j - coins[i]] < Integer.MAX_VALUE){
                    dp[j] = Math.min(dp[j], dp[j - coins[i]] + 1);
                }
            }
        }
        return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
    }
}
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值