Blue Bridge Cup: What is the lucky number (notes) (answer)

topic

 Title: The number
  of tourists who traveled to planet x with the lucky number was given an integer as the tourist number. The king of X star has a quirk, he only likes the numbers 3, 5 and 7. The king stipulated that if the number of a visitor only contains factors: 3, 5, 7, they can get a prize. Let’s take a look at the first 10 lucky numbers: 3 5 7 9 15 21 25 27 35 45. Therefore, the 11th lucky number is: 49. Xiao Ming received a lucky number 59904709587505. When he went to receive the award, he was asked to accurately say This is the first lucky number, otherwise you will not receive the prize. Please help Xiaoming to calculate, 5904709587505 is the lucky number. What needs to be submitted is an integer, please do not fill in any extra content.

answer

  1905

Precautions

  1. If the visitor’s number contains only factors: 3, 5, 7, then he can get a prize
  2. Lucky number is too large, beyond the scope of int, need to use long

Code

import java.util.ArrayList;

public class Main {
    
     //蓝桥杯要求class命名为Main,且无package
    public static void main(String []args){
    
    
        long luck=59084709587505L;//幸运数,末尾是字母L而不是数字1,数字超出了int的范围,故而用long
        long san=0,wu=0,qi=0;
        ArrayList<Long> list = new ArrayList<Long>();//将3,5,7存入list集合
        list.add(3L); //L代表long类型
        list.add(5L);
        list.add(7L);
        while(!(list.get(list.size()-1)==(luck))){
    
     //如果不和幸运数相等就执行
            long sanbei = list.get((int)san)*3;
            long wubei = list.get((int)wu)*5;
            long qibei = list.get((int)qi)*7;
            long min = min(sanbei,wubei,qibei);
            if(min==sanbei){
    
    
                san++;
            }
            if(min==wubei){
    
    
                wu++;
            }
            if(min==qibei){
    
    
                qi++;
            }
            list.add(min);
        }
        System.out.println(list.size());
    }

    public static long min(long a,long b,long c){
    
     //返回最小值
        long m = a>b?b:a;
        return m>c?c:m;
    }
}


Guess you like

Origin blog.csdn.net/qq_47168235/article/details/108905236