卡车司机肇事逃跑被三个人看见了车牌号但是没看全甲说车牌前两位一样的乙说车牌后两位一样的与前两位不一样丙说车牌是一个数的平方程序计算车牌号车牌号为4位数

卡车司机肇事逃跑被三个人看见了车牌号但是没看全甲说车牌前两位一样的乙说车牌后两位一样的与前两位不一样丙说车牌是一个数的平方程序计算车牌号车牌号为4位数

思路比较简单:

1、首先用一个for循环遍历1000到9999之间所有的数,即遍历所有的四位数,在for循环内寻找目标车牌号,寻找过程如下:

2、首先提取四位数的个十百千数位上的数,然后把他们组成前两位数字和后两位数字,再用一个if判断是否是目标车牌号,如果是的则退出循环再打印车牌号。

参考链接:
https://jingyan.baidu.com/article/22fe7cedc19e267103617f06.html
【Java】强制类型转换_chengwei1128的博客-CSDN博客_java强制类型转换

代码如下:



public class FindLicensePlate {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        int licensePlate = 0;
        for(int i=1000;i<=9999;i++) {
            
            int thousand = i/1000; //提取千位
            int hundred  = i/100%10; //提取百位
            int ten = i/10%10;  //提取十位
            int one = i%10;  //提取个位
            
            int firstTwoNumber =  thousand*10+hundred;  //前两位数字
            int secondTwoNumber = ten*10+one; //后两位数字
            
            //判断是否是目标车牌号
            if(thousand==hundred&&ten==one&&firstTwoNumber!=secondTwoNumber&&findSquareRoot(i)==true) {
                licensePlate = i;
                break;
            }    
        }
        
        //打印车牌号
        if(licensePlate!=0) {
            System.out.println("车牌号是:"+licensePlate);
        }else {
            System.out.println("没有找到目标车牌号!");
        }
    }
    
    //判断num是否是一个数的平方
    public static boolean findSquareRoot(int num) {
        
        //https://jingyan.baidu.com/article/22fe7cedc19e267103617f06.html
        //https://blog.csdn.net/cw616729/article/details/123024977
        int squareRoot = (int)(Math.sqrt(num));
        for(int i=1;i<=squareRoot;i++) {
            
            if(i*i==num) {
                return true;
            }
        }
        
        return false;
    }

}


img