蓝桥杯 星期计算

思路1

由于2022太大,用double来存储,即(5+2022 % 7) % 7即可

java 复制代码
        int num = 5;
        int t = (int)(Math.pow(20,22)%7);
        num +=t;
        num%=7;
        System.out.println(num+1);

思路2

你需要知道 (a * b ) % p = a % p * b % p

java 复制代码
		Scanner scan = new Scanner(System.in);
        int num = 1;
        for(int i = 1; i <= 22; i++)
        // (a * b ) % p = a % p * b % p
        // 这里再加上一个%p是担心 乘积会溢出
          num = (num %7 * 20 % 7) % 7;
        num = (num + 5) % 7 ;
        System.out.println(num+1);
        scan.close();

思路3

java中有一个类是专门用来进行大整数运算的:BigInteger

java 复制代码
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
        BigInteger res = new BigInteger("5");
        BigInteger product = new BigInteger("20");
        BigInteger twenty_two  = new BigInteger("22");
        BigInteger seven = new BigInteger("7");
        //等同于 (20 ^22^ % 7 + 5)%7
        int i = product.modPow(twenty_two, seven)
        .add(res).mod(seven).intValue();
        System.out.println(i+1);
    }
}
相关推荐
qq_459234426 天前
【题库】| 商用密码应用安全性评估从业人员考核题库(四十)
职场和发展·密码学·学习方法·考核·商用密码·商用密码应用安全性评估·密评
敲敲了个代码6 天前
[特殊字符] 空数组的迷惑行为:为什么 every 为真,some 为假?
前端·javascript·react.js·面试·职场和发展
诚思报告YH6 天前
视频面试软件市场洞察:2026 - 2032年复合年均增长率(CAGR)为10.3%
面试·职场和发展
重生之后端学习6 天前
74. 搜索二维矩阵
开发语言·数据结构·算法·职场和发展·深度优先
tyb3333336 天前
leetcode:吃苹果和队列
算法·leetcode·职场和发展
Pitiless-invader6 天前
MySQL 相关知识及面试问题汇总
面试·职场和发展
重生之后端学习6 天前
35. 搜索插入位置
java·数据结构·算法·leetcode·职场和发展·深度优先
逆境不可逃6 天前
【从零入门23种设计模式08】结构型之组合模式(含电商业务场景)
线性代数·算法·设计模式·职场和发展·矩阵·组合模式
筱昕~呀6 天前
冲刺蓝桥杯-DFS板块(第二天)
算法·蓝桥杯·深度优先
zheshiyangyang6 天前
前端面试基础知识整理【Day-10】
前端·面试·职场和发展