(Short version) keyboard input range, solve all the powers in the range


package com.xiaozhao.coding;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner;

public class GetNarcissisticNumbers {

    @SuppressWarnings("resource")
    public static void main(String[] args) {         List<Integer> list1 = new ArrayList<Integer>();         Scanner scanner = new Scanner(System.in);         System.out.println(" Please enter the range you need to query from the power (for example, 100---n, the default starting position is 100):");         int n = scanner.nextInt();         for(int i=100;i<=n ;i++) {             getNarcissisticNumbers(i);             if(getNarcissisticNumbers(i)==i)             list1.add(getNarcissisticNumbers(i));         }          System.out.println("In this range, the power numbers you query are:" +"\n"+list1);     }     public static int GetCount(int n) {         // Determine how many digits n is         int s = n/10;
        


    


        
        

            

            








        int count = 1;
        while(s>0) {             s = s/10;             count++;         }         return count;     }     public static int sum(int n,int k) {         //Find the count order of each number         int sum = 1;         final int h = n;         for(int i=0;i<h;i++) {             sum = sum*k;         }         return sum;     }     public static int jiechen(int k) {         //What is needed for each number The factorial of division (10)         int num = 1;         while(k>0) {             num =10*num;             k--;         }         return num;     }














    







        


    public static int Getvalue(int n) {         //take the number obtained after         int value = 0;         int q = 1;         int a = GetCount(n)-1;         //System.out.println(a);         while( a>0) {             q = 10*q;             a--;         }         value = n/q;         //System.out.println(value);         return value;     }     public static int getNarcissisticNumbers(int n) {         //Return to pass The number after the power change         List<Integer> list = new ArrayList<Integer>();         int k = GetCount(n);         //System.out.println(k);         for(int i=1;i<2; i++){











        
    




        

        
      

        
        

        

            list.add(sum(k,Getvalue(n)));
            for(int j=k-1;j>0;j--) {
                n = n%jiechen(j);
                GetCount(n);
                list.add(sum(k,Getvalue(n)));    
            }
        }
        ListIterator<Integer> it =list.listIterator();
        int sum2 = 0;
        while(it.hasNext()){
            int sum1 = it.next();
            sum2 =sum2+sum1; 
        }
        //System.out.println(sum2);
        
        return sum2;
        }
}
 

Guess you like

Origin blog.csdn.net/weixin_43562937/article/details/103828039