Java教程

2021-12-03_学习B站Java视频Calendar对象_巴什博弈

本文主要是介绍2021-12-03_学习B站Java视频Calendar对象_巴什博弈,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1. day18,P217,Calendar对象。

get方法:

import java.util.*;
import java.text.*;
class CalendarDemo {
    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();

        sop(c.get(Calendar.YEAR) + "年");
        sop((c.get(Calendar.MONTH) + 1) + "月");
        sop(c.get(Calendar.DAY_OF_MONTH) + "日");

        // sop("星期" + c.get(Calendar.DAY_OF_WEEK));
        sop("c.getFirstDayOfWeek() " + c.getFirstDayOfWeek()); // 1
        sop("Calendar.SUNDAY " + Calendar.SUNDAY); // 1
        boolean isFirstDaySunday = (c.getFirstDayOfWeek() == Calendar.SUNDAY);
        sop("first day of week is sunday? " + isFirstDaySunday); //first ...is sunday? true
        sop("今天是一周的第" + c.get(Calendar.DAY_OF_WEEK) + "天(以星期日为第一天)");

        // 查表法
        String[] mons = {"一月","二月","三月","四月",
            "五月","六月","七月","八月",
            "九月","十月","十一月","十二月"};
        int indexOfMonth = c.get(Calendar.MONTH);
        sop("查表法:" + mons[indexOfMonth]);

        String[] dayOfWeeks = {"","星期日","星期一","星期二",
            "星期三","星期四","星期五","星期六"};
        sop("查表法:" + dayOfWeeks[c.get(Calendar.DAY_OF_WEEK)]);
    }
    public static void sop(Object obj) {
        System.out.println(obj);
    }
}

set、add方法:

import java.util.*;
class CalendarDemo2 {
    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();

        // c.set(2021, 10, 3);
        c.add(Calendar.DAY_OF_MONTH, -29);
        printCalendar(c);

        // 练习:获取任意年的二月有多少天
        // 思路:根据指定年设置一个时间就是c.set(year,2,1) //某一年的3月1号
        // c.add(Calendar.DAY_OF_MONTH, -1)
        sop("2021年2月有" + getFebDaysByYear(c, 2021) + "天");
    }
    public static int getFebDaysByYear(Calendar c, int year) {
        c.set(year, 2, 1);
        c.add(Calendar.DAY_OF_MONTH, -1);
        return c.get(Calendar.DAY_OF_MONTH);
    }
    public static void printCalendar(Calendar c) {
        sop(c.get(Calendar.YEAR));
        // 查表法
        String[] mons = {"一月","二月","三月","四月",
            "五月","六月","七月","八月",
            "九月","十月","十一月","十二月"};
        int indexOfMonth = c.get(Calendar.MONTH);
        sop(mons[indexOfMonth]);
        
        sop(c.get(Calendar.DAY_OF_MONTH));

        String[] dayOfWeeks = {"","星期日","星期一","星期二",
            "星期三","星期四","星期五","星期六"};
        sop(dayOfWeeks[c.get(Calendar.DAY_OF_WEEK)]);
    }
    public static void sop(Object obj) {
        System.out.println(obj);
    }
}

2. 巴什博弈。

学习他人提交的题解:

public class Solution {
    public boolean canWinBash(int n) {
        // 只要让对方取石头前,剩余的石头数是4的倍数即可。
        // 即只要能先取n%4个石头,然后不论对方取x个,我都再取4-x个,使得对方下次取石头时,仍是4的倍数。
        // 最终对方取前剩4个石头,我就一定赢了。
        // 所以,只要n不能整除4,那么我就会赢。
        return (n % 4) != 0;
    }
}

 

这篇关于2021-12-03_学习B站Java视频Calendar对象_巴什博弈的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!