本文主要是介绍顺子日期 蓝桥杯,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
调用API
思路: 设置Calendar的属性,获取Calendar的毫秒数,转换成指定格式的字符串(yyyyMMdd),判断字符串中是否包含符合条件的,若有就+1,
迭代: 每次循环给Calendar加上一天即可
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
// 1:无需package
// 2: 类名必须Main, 不可修改public class Main {public static void main(String[] args) {Calendar calendar = Calendar.getInstance();calendar.set(Calendar.YEAR, 2022);calendar.set(Calendar.MONTH, 0);calendar.set(Calendar.DAY_OF_MONTH, 1);//"234","345","456","567","678","789"// 上面的这么多不可能出现String[] strs = {"012","123"};int cnt = 0;SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");while (calendar.get(Calendar.YEAR) == 2022) {String s = sdf.format(calendar.getTimeInMillis());for(String str: strs){if(s.contains(str)){cnt++;break;}}calendar.add(Calendar.DAY_OF_MONTH,1);}System.out.println(cnt);}
}
模拟
前提是要知道闰年的条件
- 能被400整除
- 是4的倍数而不是100的倍数
满足上面条件的其中一个即可
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
// 1:无需package
// 2: 类名必须Main, 不可修改public class Main {//1 3 5 7 8 10 12//2 4 6 9 11static int[] days = {0,31,28,31,30,31,30,31,31,30,31,30,31,0};static int day = 1, month =1 ,year = 2022;public static boolean check(){int num = year * 100 + month+day;String s = num+"";// 2022 10 11int steps = 1;for(int i=0;i<s.length()-1;i++) {int pre = s.charAt(i);int next = s.charAt(i+1);if(next == pre+1){steps ++;}else {steps = 1;}if(steps == 3)return true;}return false;}public static void main(String[] args) {int ans = 0;while(year==2022){if(check()){ans++;}day++;if(month==2){if(year%400==0 || year%4==0 && year%100!=0){if(day>29){day=1;month++;}}else{if(day>28){day=1;month++;}}}else if(day>days[month]){//不加的话这里会溢出day=1;month++; // 当day =32,month = 12的时候,month会加1=>13,走到day>days[month]的时候会下标越界}if(month>12)year++;}System.out.println(ans);}
}
这篇关于顺子日期 蓝桥杯的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!