栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

文章目录
  • 选择题
  • 第一题 折扣
  • 第二题 兑换零钱

选择题

很多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;
    }
}
第二题 兑换零钱

力扣原题:零钱兑换

思路:动态规划


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];
    }
}
暑期编程PK赛 得CSDN机械键盘等精美礼品!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1016364.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号