蓝桥杯 星期计算

思路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);
    }
}
相关推荐
第七种黄昏12 小时前
前端面试-箭头函数
前端·面试·职场和发展
代码充电宝14 小时前
LeetCode 算法题【简单】20. 有效的括号
java·算法·leetcode·面试·职场和发展
海琴烟Sunshine14 小时前
leetcode 119. 杨辉三角 II python
算法·leetcode·职场和发展
235161 天前
【LeetCode】146. LRU 缓存
java·后端·算法·leetcode·链表·缓存·职场和发展
Swift社区2 天前
从 0 到 1 构建一个完整的 AGUI 前端项目的流程在 ESP32 上运行
前端·算法·职场和发展
Swift社区2 天前
LeetCode 395 - 至少有 K 个重复字符的最长子串
算法·leetcode·职场和发展
Espresso Macchiato2 天前
Leetcode 3710. Maximum Partition Factor
leetcode·职场和发展·广度优先遍历·二分法·leetcode hard·leetcode 3710·leetcode双周赛167
前端架构师-老李2 天前
面试问题—你接受加班吗?
面试·职场和发展
熬了夜的程序员2 天前
【LeetCode】69. x 的平方根
开发语言·算法·leetcode·职场和发展·动态规划