zoukankan      html  css  js  c++  java
  • java实现猜算式

    题目:猜算式
    
    你一定还记得小学学习过的乘法计算过程,比如:
    x   15
    ------
      273
    ------
      
    请你观察如下的乘法算式
    
        ***
    x   ***
    --------
        ***
       ***
      ***
    --------
      *****
      
    星号代表某位数字,注意这些星号中,
    0~9中的每个数字都恰好用了2次。
    (如因字体而产生对齐问题,请参看图p1.jpg)
    
    请写出这个式子最终计算的结果,就是那个5位数是多少?
    
    注意:只需要填写一个整数,不要填写任何多余的内容。比如说明文字。
    
    import java.util.ArrayList;
    import java.util.Collections;
    
    public class Main {
        
        public boolean judge(int temp1, int temp2, int temp3, int temp4, int temp5, int temp6) {
            ArrayList<Integer> list = new ArrayList<Integer>();
            while(temp1 > 0) {
                list.add(temp1 % 10);
                temp1 = temp1 / 10;
            }
            while(temp2 > 0) {
                list.add(temp2 % 10);
                temp2 = temp2 / 10;
            }
            while(temp3 > 0) {
                list.add(temp3 % 10);
                temp3 = temp3 / 10;
            }
            while(temp4 > 0) {
                list.add(temp4 % 10);
                temp4 = temp4 / 10;
            }
            while(temp5 > 0) {
                list.add(temp5 % 10);
                temp5 = temp5 / 10;
            }
            while(temp6 > 0) {
                list.add(temp6 % 10);
                temp6 = temp6 / 10;
            }
            Collections.sort(list);
            if(list.size() == 20) {
                int j = 0;
                for(int i = 0;i < 20;i = i + 2, j++) {
                    if(list.get(i) == j && list.get(i + 1) == j)
                        continue;
                    else 
                        return false;
                }
                if(j == 10)
                    return true;
            }
            return false;
        }
        
        public boolean judge1(int n) {
            if(n >= 1000 || n < 100)
                return false;
            return true;
        }
        
        
        public void printResult() {
            for(int i = 100;i <= 999;i++) {
                int temp1 = i;
                for(int j = 100;j <= 999;j++) {
                    int temp2 = j;
                    int temp3 = temp2 % 10 * temp1;
                    int temp4 = temp2 / 10 % 10 * temp1;
                    int temp5 = temp2 / 100 * temp1;
                    int temp6 = temp2 * temp1;
                    if(judge1(temp3) && judge1(temp4) && judge1(temp5) && temp6 > 9999 && temp6 < 100000) {
                        if(judge(temp1, temp2, temp3, temp4, temp5, temp6)) {
                            System.out.println("temp1 = "+temp1+", temp2 = "+temp2+", temp6 = "+temp6);
                        }
                    } else {
                        continue;
                    }
                }
            }
        }
        
        public static void main(String[] args) {
            Main test = new Main();
            test.printResult();
        }
    }
    
  • 相关阅读:
    Veritas NetBackup™ for OpenStack
    Win 810系统安装软件报错
    Netbackup驱动器常用命令vmoprcmd
    NBU服务端生成证书/客户端获取、更新证书方式/7656、7654、7640、76XX报错处理
    centos7之系统优化方案【转】
    pip的安装、以及使用方法。
    Python模块(导入,内置,自定义,开源)
    数据库的高可用 及 Mycat的引入
    LeetCode 第21题 合并有序链表
    Docker容器技术
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12947930.html
Copyright © 2011-2022 走看看