zoukankan      html  css  js  c++  java
  • 算法提高 连接乘积

    问题描述
      192这个数很厉害,用它分别乘以1、2、3,会得到:
      192 x 1 = 192
      192 x 2 = 384
      192 x 3 = 576
      把这三个乘积连起来,得到192384576,正好是一个1~9的全排列
      我们把上面的运算定义为连接乘积:
      m x (1 ... n) = k(其中m > 0 且 n > 1,对于上例,m = 192、n = 3、k = 192384576)
      即k是把m分别乘以1到n的乘积连接起来得到的,则称k为m和n的连接乘积。
      按字典序输出所有不同的连接乘积k,满足k是1~9的全排列
    输出格式
      每个k占一行
    样例输出
    显然,结果中应包含一行:
    192384576
     1 import java.util.ArrayList;
     2 import java.util.Arrays;
     3 import java.util.Scanner;
     4 
     5 import java.util.*;
     6 import java.lang.Math;
     7 class Main {
     8     static char[] a = {'1','2','3','4','5','6','7','8','9'};
     9     public static void main(String[] args) {
    10         List<String> list = new ArrayList<>();
    11         for(int i=1;i<10000;i++){
    12                 String s = "";
    13                 int h = 1;
    14                 while(s.length()<9){
    15                     s = s+i*h;
    16                     h++;
    17                 }
    18                 //System.out.println(s);
    19                 if(s.length()==9&&f(s)){
    20                     list.add(s);
    21             }
    22         }
    23         Collections.sort(list);
    24         for(String s:list){
    25             System.out.println(s);
    26         }
    27     }
    28     static boolean f(String s){
    29         int i;
    30         char[] b = s.toCharArray();
    31         Arrays.sort(b);
    32         for(i=0;i<s.length();i++){
    33             if(b[i]!=a[i]){
    34                 break;
    35             }
    36         }
    37         if(i==s.length()){
    38             return true;
    39         }else{
    40             return false;
    41         }
    42     }
    43     
    44 }
  • 相关阅读:
    Stepping Number
    Replace String
    String Permutation
    Clock Angle
    Keypad Permutation
    Replace Words
    1、奉加微 PHY6202 Get started
    Python3 修改二进制文件
    Google Fast Pair
    python 校验 BLE resolvable private address
  • 原文地址:https://www.cnblogs.com/lolybj/p/6658252.html
Copyright © 2011-2022 走看看