蓝桥杯 星期计算

思路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);
    }
}
相关推荐
JosieBook1 天前
【程序人生】有梦想就能了不起,就怕你没梦想
程序人生·职场和发展
scx201310041 天前
P13929 [蓝桥杯 2022 省 Java B] 山 题解
c++·算法·蓝桥杯·洛谷
data myth1 天前
力扣1210. 穿过迷宫的最少移动次数 详解
算法·leetcode·职场和发展
Greedy Alg2 天前
LeetCode 240. 搜索二维矩阵 II
算法·leetcode·职场和发展
墨染点香2 天前
LeetCode 刷题【68. 文本左右对齐】
算法·leetcode·职场和发展
GalaxyPokemon2 天前
LeetCode - 202. 快乐数
算法·leetcode·职场和发展
吃着火锅x唱着歌2 天前
LeetCode 522.最长特殊序列2
算法·leetcode·职场和发展
CoderYanger2 天前
MySQL数据库——3.2.1 表的增删查改-查询部分(全列+指定列+去重)
java·开发语言·数据库·mysql·面试·职场和发展
yh云想2 天前
《Java线程池面试全解析:从原理到实践的高频问题汇总》
jvm·面试·职场和发展
Miraitowa_cheems2 天前
LeetCode算法日记 - Day 34: 二进制求和、字符串相乘
java·算法·leetcode·链表·职场和发展