7th Week Programming Questions
View Help
return
7th Week Programming Questions
In accordance with the terms of academic integrity, I guarantee that this work is done independently.
Warm tips:
1. This assignment belongs to the online Judge topic and is enigmatic grading by the system immediately after submission.
2. Students can submit an unlimited number of answers before the deadline , and the system will take the highest score as the final result.
1 decomposition factorization (5 points)
Topic content:
Each non-prime (composite) can be written as a number of primes (also known as prime numbers) in the form of multiplication, and these primes are called the composite's mass factor. For example, 6 can be decomposed into 2x3, and 24 can be decomposed into 2x2x2x3.
Now, your program reads an integer in a [2,100000] range, then outputs its mass factorization, and when it reads a prime number, it outputs itself.
Input format:
An integer that is within [2,100000].
Output format:
Shaped like:
N=axbxcxd
Or
N=n
There are no spaces between all the symbols, and x is the lowercase letter x.
Input Sample:
18
Sample output:
18=2x3x3
time limit: 500ms memory limit: 32000kb
Import Java.util.scanner;public class Main {public static void main (string[] args) {Scanner sc = new Scanner (system.in); T count = 0;//counter int n = 0;//input int nbeifen;//backup Nint prime = 2;//Prime, first prime number is 2n = Sc.nextint (); if (prime (n)) {SYSTEM.OUT.P rintf ("%d=%d\n", N, N),//n=n} else {System.out.printf ("%d=", n);//N=axbxcxdnbeifen = N;while (Nbeifen > 1) {if (Nbeif En% Prime = = 0) {if (Count > 0) {System.out.printf ("X");} Nbeifen = Nbeifen/prime; System.out.printf ("%d", prime); count++;} else {prime++;p rime = NextPrime (prime),///Next prime}}system.out.printf ("\ n");}} public static Boolean prime (int n) {//To determine if prime boolean IsPrime = true;//default is prime if (n = = 1 | | (n% 2 = = 0 && n! = 2)) {//Judge 1 or even isprime except 2 = false;} else if (n = = 2) {//Judgment 2isPrime = true;} else {//to judge other for (int i = 3; i < MATH.SQRT (n); i + = 2) {if (n = = = 0) {Ispri me = False;break;}}} return isprime;} public static int NextPrime (int n) {//Next prime number while (true) {if (prime (n)) {return n;} else {n++;}}}}
China MOOC_ 0 Basic Java language _ 7th week function _1 decomposition factorization