java淘宝网店


题目描述

NowCoder在淘宝上开了一家网店。他发现在月份为素数的时候,当月每天能赚1元;否则每天能赚2元。
现在给你一段时间区间,请你帮他计算总收益有多少。
eg:

输入:
2000 1 1 2000 1 31
2000 2 1 2000 2 29

输出:
62
29


以下是本篇文章正文内容,下面案例可供参考

解题思路

1993~2013
1993剩余的收益 + [1994,2012] 全年的收益 + 2013年有的收益
1993~1993
只有1993年的收益

profit = profitOfYear(year1) - profitOfThisYear(year1, month1, day1 - 1);

1993-5-7 = 1993的全年收益 - 从1993年1月1日到1993年5月6日的收益(day1 - 1)

代码如下

// write your code here
import java.util.*;
public class Main {
    private static boolean isLeapYear(int year) {
        return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
    }
    
    private static int profitOfYear(int year) {
        return 2 * 31 
            + 1 * 28 
            + 1 * 31 
            + 2 * 30 
            + 1 * 31 
            + 2 * 30 
            + 1 * 31 
            + 2 * 31 
            + 2 * 30 
            + 2 * 31 
            + 1 * 30 
            + 2 * 31 
            +(isLeapYear(year) ? 1:0);
    }
    
    private static boolean isPrime(int month) {
        return month == 2 || month == 3 || month == 5 || month == 7 || month == 11;
    }
    
    private static int profitOfThisYear(int year,int month,int day){
        int profit = 0;
        if(!isPrime(month)) {
            profit = day * 2;
        }else{
            profit = day;
        }
        
        while(--month>0) {
            switch(month) {
                case 1:case 8:case 10:case 12: profit += 62;
                    break;
                case 3:case 5:case 7: profit += 31;
                    break;
                case 4:case 6:case 9: profit += 60;
                    break;
                case 11: profit += 30;
                    break;
                default:
                    profit += (28+(isLeapYear(year)?1:0));
                    break;
            }
        }
        return profit;
    }
    
    public static void main(String[] args) {
        int year1,month1,day1,year2,month2,day2;
        int profit = 0;
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            year1 = sc.nextInt();
            month1 = sc.nextInt();
            day1 = sc.nextInt();
            year2 = sc.nextInt();
            month2 = sc.nextInt();
            day2 = sc.nextInt();
            profit = profitOfYear(year1) - profitOfThisYear(year1,month1,day1-1);
            profit += profitOfThisYear(year2,month2,day2);
            if(year1 == year2) {
                profit -= profitOfYear(year1);
            }
            for(int i = year1+1;i<year2;i++) {
                profit += profitOfYear(i);
            }
            System.out.println(profit);
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值