zoukankan      html  css  js  c++  java
  • 一个小题目

    这个是求由1-9组成的9位数,各位数字不能重复。

    同时这个数字满足,前2位可以被2整除,前3位可以被3整除,前n位可以被n整除。

    package com.java.test;
    
    public class Number {
    public static void main(String[] args) {
    
    int thisNum = 0;
    for(thisNum = 123456780; thisNum < 987654322 ; thisNum++)   //查找123456789 - 987654322 所有数
    if(test(thisNum)){                    //test 判断各个位上数字是否满足为1-9且不重复。
    if(testPer(thisNum)){     //testPre判断前n位能否被n整除。
    System.out.println("this num:" + thisNum);
    }
    }
    }
    
    /**
    
    */test 判断各个位上数字是否满足为1-9且不重复。
    
    */
    public static boolean test(int num){
    boolean flag = true;
    for(int i = 0;i < 9;i++){
    for(int j = 0;j < i;j++){
    int n = num % (int)Math.pow(10, i+1) / (int)Math.pow(10, i);
    int m = num % (int)Math.pow(10, j+1) / (int)Math.pow(10, j);
    if(n==m || n==0 || m==0 ){
    flag = false;
    }else{
    flag = flag && true;
    }    
    }
    }
    return flag;
    }
    
    /**
    
    *testPre判断前n位能否被n整除。
    
    */
    
    public static boolean testPer(int num){
    boolean flag = true;
    for(int i = 2;i <= 9;i++){
    int perNum = num / (int)Math.pow(10, 9-i);
    if(perNum % i == 0){
    flag = flag && true;
    }else{
    flag = false;
    }
    }
    return flag;
    }
    }
  • 相关阅读:
    常用内建函数
    函数作用域
    异常处理语句
    迭代器---待延申扩展
    流程控制语句
    字典
    集合
    数据类型的可变与不可变
    Openstack keystone组件详解
    云计算openstack介绍(001)
  • 原文地址:https://www.cnblogs.com/playa13/p/3593213.html
Copyright © 2011-2022 走看看