zoukankan      html  css  js  c++  java
  • 计算水仙花数

    //数据结果课 老师布置了计算水仙花数程序转载如下:

    package com.WildCat.Shuixianhuan;
    import java.math.BigInteger;
    import java.util.Arrays;
    public class a {
      private static BigInteger[] table = new BigInteger[10];
    
      public static void main(String[] args) {
    	  long time = System.nanoTime();
    	  find(21);//计算21位水仙花数
    	  time = System.nanoTime() - time;
    	  System.out.println(time/1000000000.0 + "s");
      }
    
      public static void find(int n) {
      for (int i = 0; i < 10; i++)
    	  table[i] = BigInteger.valueOf(i).pow(n);
      int[] nums = new int[n];
      int index = 0;
      int num = 0;
      while (true) {
    	  if (index < nums.length && num < 10) {
    	  nums[index] = num;
    	  index++;
    	  continue;
          } else if (index >= nums.length) {
    	  BigInteger big = sum(nums);
    	  int[] temp = getArray(big);
    	  if (check(nums, true, temp, false))
    	  System.out.println(big);
    	  } else if (index <= 0) {
    	  break;
          }
    	  index--;
    	  num = nums[index] + 1;
    	  }
      }
    //判断两个大整数是否相等
      public static boolean check(int[] a1, boolean copy1, int[] a2, boolean copy2) {
    	  if (a1.length != a2.length)
    	  return false;
    	  if (copy1)
    	    a1 = a1.clone();
    	  if (copy2)
    	    a2 = a2.clone();
    	  Arrays.sort(a1);
    	  Arrays.sort(a2);
    	  return Arrays.equals(a1, a2);
      }
    //求和函数
      public static BigInteger sum(int[] nums) {
    	  BigInteger sum = BigInteger.ZERO;
    	  for (int i = 0; i < nums.length; i++)
    	  sum = sum.add(table[nums[i]]);
    	  return sum;
      }
      //把大整数变成数组
      public static int[] getArray(BigInteger big) {
    	  String s = String.valueOf(big);
    	  int length = s.length();
    	  int[] res = new int[length];
    	  for (int i = 0; i < length; i++)
    	  res[i] = s.charAt(i) - '0';
    	  return res;
      }
    }
    


  • 相关阅读:
    Unet网络
    反卷积、上采样、上池化
    深度学习中的正则化
    BN_batch normalization
    导入numpy时,出错怎么解决?
    faster rcnn相关内容
    卷积神经网络的结构总结
    卷积神经网络的结构及对卷积的理解
    双系统中卸载Ubuntu后又efi系统分区删除方法
    ubuntu18.04中安装和卸载cuDNN
  • 原文地址:https://www.cnblogs.com/lixingle/p/3313023.html
Copyright © 2011-2022 走看看