Algorithm conversion and modulo operation

insert image description here
First of all, let's take a look at the law.
When AA=27, 27/26=1...1, the divisor 1 represents A, and the remainder 1 also represents A.
The same is true when AB=27.
Let's look at the three-digit number
LQ=329
329/26=12...17, the divisor 12 represents L, and the remainder 17 represents Q.
2019/26=77...17, the divisor 77 is greater than 26 and cannot be found and needs to be divided by 26, and the remainder 17 represents Q.
77/26=2…25, the divisor 2 represents B, and the remainder 25 represents Y,
so 2019 represents the string BYQ

import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    
    
    public static void main(String[] args) {
    
    
        // Scanner scan = new Scanner(System.in);
        // //在此输入您的代码...
        // scan.close();
        String s="";
        char[] arr = new char[]{
    
    'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','N','W','X','Y','Z'};
        int n=2019;
        while(n>0){
    
    
          s=s+arr[n%26-1];//数组从0开始才是A,而我们时A代表1,所以减去1
          n=n/26;
        }
        //输出QYB
        //反转字符串
        StringBuilder sb=new StringBuilder(s);
        System.out.println(sb.reverse());//BYQ
    }
}

insert image description here
20190324 This number is too large, neither recursive nor iterative programs can run normally, and int, long, BigDecimal, etc. cannot carry such a large number. The
problem requires us to take only the last four digits, so we can calculate the result of each operation %10000 so that the last four digits are kept in each result

import java.util.Scanner;

public class Main {
    
    
      public static void main(String[] args) {
    
    
        int a = 1,b = 1,c = 1,d = 0;
        for(int i = 4;i<=20190324;i++){
    
    
            d = (a+b+c)%10000;
            a = b;
            b = c;
            c = d;
        }
        System.out.println(d);

    }
}

Guess you like

Origin blog.csdn.net/ChenYiRan123456/article/details/129685449