蓝桥杯 星期计算

思路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);
    }
}
相关推荐
ocean21031 小时前
2025-2026年AI提效与实践大厂面试高频问题
人工智能·面试·职场和发展·提示词工程·ai提效
YonyouHRSaaS4 小时前
AI视频面试系统定义、功能作用、品牌推荐、选择攻略
人工智能·面试·职场和发展·ai面试·视频面试·ai视频面试
zihan5185 小时前
理论:构建“基于客观事实、可量化、闭环自给自足”的个人操作系统
笔记·职场和发展·学习方法
小鱼爱吃草灬灬6 小时前
实时面试辅助排查清单:音频来源、问题输入与上下文
面试·职场和发展·音视频
颜挺锐9 小时前
如何轻松通过性能测试面试之第九篇:RPO、RTO是什么意思
面试·职场和发展
Nil2081 天前
leetcode 17电话号码的字母组合
算法·leetcode·职场和发展
artificiali1 天前
拼好卷 05
职场和发展
AIsoft_86881 天前
最好用的会议纪要APP怎么选?功能横评
面试·职场和发展·iphone