蓝桥杯 星期计算

思路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);
    }
}
相关推荐
梁下轻语的秋缘8 小时前
每日c/c++题 备战蓝桥杯(P1049 [NOIP 2001 普及组] 装箱问题)
c语言·c++·学习·蓝桥杯
wuqingshun3141598 小时前
蓝桥杯 5. 交换瓶子
数据结构·c++·算法·职场和发展·蓝桥杯
Demons_kirit8 小时前
Leetcode 2845 题解
算法·leetcode·职场和发展
宝耶11 小时前
面试常问问题:Java基础篇
java·面试·职场和发展
triticale12 小时前
【蓝桥杯】画展布置
蓝桥杯
Y1nhl13 小时前
力扣hot100_链表(3)_python版本
python·算法·leetcode·链表·职场和发展
前端 贾公子13 小时前
详解 LeetCode 第 242 题 - 有效的字母组
算法·leetcode·职场和发展
triticale15 小时前
P12167 [蓝桥杯 2025 省 C/Python A] 倒水
java·蓝桥杯
Demons_kirit16 小时前
LeetCode 2799、2840题解
算法·leetcode·职场和发展
雾月5517 小时前
LeetCode 1292 元素和小于等于阈值的正方形的最大边长
java·数据结构·算法·leetcode·职场和发展