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();
        }
    }
    
  • 相关阅读:
    数组是个好东西
    排列(permutation) 用1,2,3,…,9组成3个三位数abc,def和ghi,每个数字恰好使用一次,要 求abc:def:ghi=1:2:3。按照“abc def ghi”的格式输出所有解,每行一个解。
    子序列的和
    韩信点兵
    水仙花数
    阶乘之和
    3n+1问题
    MongoDB 安装
    mysql中bigint在php中表示
    Android之NDK开发
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12947929.html
Copyright © 2011-2022 走看看