Huawei Online Programming Questions Series-6-Prime Factor


Problem Description:

Problem Description
Problem Description

1. The question involves knowledge points.

  • Solving prime numbers (review leap years, daffodils, palindromes).

2. Solve it yourself.

  • loop to input
  • A single computation input. Each time finds the smallest prime that can be an integer.
  • Use stringBuilder to construct the structure.
package com.chaoxiong.niuke.huawei;
import java.util.Scanner;
public class HuaWei_6 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            long in = scanner.nextLong();
            String result = getResult(in);
            System.out.println(result);
        }
    }
    private static String getResult(long key) {
        long[] intArr = new long[64];
        int intArrIndex = 0;
        while (key != 1) {
            // 辗转的向下除
            if ((key & 1) == 0) {
                // 可以整数2的每次除以2
                intArr[intArrIndex] = 2;
                intArrIndex = intArrIndex + 1;
                key = key / 2;
            } else {
                // 不能整数2的寻找可以整除的素数
                for (int i = 3; i <= key; i = i + 2) {
                    if (isPrime(i)) {
                        if (key % i == 0) {
                            intArr[intArrIndex] = i;
                            intArrIndex = intArrIndex + 1;
                            key = key / i;
                            break;
                        }
                    }
                }
            }
        }
        StringBuilder tmp = new StringBuilder();
        for (int i = 0; i < intArrIndex; i++) {
            tmp.append(intArr[i]).append(" ");
        }
        return tmp.toString();
    }
    private static boolean isPrime(int key) {
        if(key==2)//2是最小的素数.
            return true;
        if (key > 2 && (key & 1) == 0)//大于2的偶数都不是素数
            return false;
        for (int i = 3; i * i <= (key); i += 2)//大于2的一直到根号下本身都没有约数的为素数.
            if (key % i == 0)
                return false;
        return true;
    }
}

3. Quality answers.

null

4. Summary of this question.

  • Prime number
    private static boolean isPrime(int key) {
        if(key==2)//2是最小的素数.
            return true;
        if (key > 2 && (key & 1) == 0)//大于2的偶数都不是素数
            return false;
        for (int i = 3; i * i <= (key); i += 2)//大于2的一直到根号下本身都没有约数的为素数.
            if (key % i == 0)
                return false;
        return true;
    }
  • Find the number of palindromes

    Let n be an arbitrary natural number. If the natural numbers n1 and n obtained by arranging the digits of n in reverse order are equal, then n is called a palindromic number. For example, if n=1234321, then n is called a palindrome; but if n=1234567, then n is not a palindrome.

    Solution 1: intConvert to string, get the length, use it directly and string.charAt(index)compare before and after.
    Solution 2: intConvert to string, and then use StringBuffer.reverse(), get the flip string. Then convert the string to int, and directly compare whether it is equal.
    Solution 3: intConvert to string, then Use StringBuffer.reverse(), to get the reversed string. Use string.charAt(index)contrasting characters.
    In general, it is simpler to use strings.

package com.chaoxiong.ClassicsProblem;
import java.util.Scanner;
/**
 * Create by tianchaoxiong on 18-5-2.
 * 回文数
 */
public class PalindromeNum {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()){
            int key = scanner.nextInt();
            boolean result = isPalindrome(key);
            System.out.println(result);
        }
    }
    private static boolean isPalindrome(int key) {
        String strKey = key+"";
        // 解法3
//        StringBuilder stringBuilder = new StringBuilder(strKey);
//        stringBuilder.reverse();
//        for(int i=0;i<strKey.length()/2;i++){
//            if(strKey.charAt(i)!=stringBuilder.charAt(i))
//                return false;
//        }
//        return true;
        //解法1
        for(int i=0;i<strKey.length()/2;i++){
            if(strKey.charAt(i)!=strKey.charAt(strKey.length()-1-i))
                return false;
        }
        return true;
    }
}


  • Find the number of daffodils

Narcissistic number, also known as pluperfect digital invariant (PPDI), narcissistic number, self-power number, Armstrong number or Armstrong number, is the An n-digit number (n≥3) whose sum of the n-th powers of the digits in each digit is equal to itself (eg: 1^3 + 5^3+ 3^3 = 153).
package com.chaoxiong.ClassicsProblem;
import java.util.Scanner;
/**
 * Create by tianchaoxiong on 18-5-2.
 * 水仙花数
 */
public class Narcissistic {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()){
            int key = scanner.nextInt();
            boolean result = isNar(key);
            System.out.println(result);
        }
    }
    private static boolean isNar(int key) {
        // 做一个n次幂的累加
        String strKey = key+"";
        int keyBack = key;
        int cout =0;
        for(int i=0;i<strKey.length();i++){
            int tmp = key%10;
            key = key/10;
            cout = (int) (cout+Math.pow(tmp,strKey.length()));
        }
        return keyBack==cout;
    }
}


  • Determining leap years

A leap year that is divisible by 4 and not divisible by 100 is also a leap year if it is divisible by 400
package com.chaoxiong.ClassicsProblem;
import java.util.Scanner;
/**
 * Create by tianchaoxiong on 18-5-2.
 */
public class LeapYear {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()){
            int key = scanner.nextInt();
            boolean result = isLeapYear(key);
            System.out.println(result);
        }
    }

    private static boolean isLeapYear(int key) {
        return key%4==0&&key%100!=0||key%400==0;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325968118&siteId=291194637