Maximum approx.

Source: Internet
Author: User

Import java. util. ArrayList;

Import java. util. List;

Import java. util. collections;


/**

* Define a class as a power function class. The class contains two parameters.

* @ Author LiuYong

* Algorithm idea: according to the theory that each number can be divided into one or more forms of product multiplied by quality factors,

* The number of factors in each number can be expressed as the number of occurrences of each prime factor plus a multiplication of 1

**/

Class Monomial {

Int base; // base number of the power function

Int index; // The exponent of the power function.

// No parameter Constructor

Public Monomial (){

}

// Constructor with Parameters

Public Monomial (int base, int index ){

This. base = base;

This. index = index;

}

// Calculate the number of factors in the input. Based on the algorithm concept described above

Public int Count (List <Monomial> list ){

Int count = 1; // The number of calculated Factors

For (int I = 0; I <list. size (); I ++) {// traverses the quality factor objects stored in the linked list.

Count = count * (list. get (I). index) + 1); // Add one to each quality factor index based on the algorithm description and then obtain the product

}

Return count; // number of return Factors

}

}



/**

* Defines an integer class. The class contains three parameters and a method for integer decomposition.

* @ Author LiuYong

* Algorithm idea: Each integer can be decomposed into one or more power functions multiplied by quality factors,

* Enter an integer to be processed and use the for loop to traverse from the smallest non-integer,

* When the remainder is zero, it indicates that the prime factor can be retained and saved as an object.

* Continue to divide by the current divisor. If the remainder is not the same, add a divisor and use the above description to cycle.

* The algorithm ends when the divisor is greater than the divisor.

* @ Param conunt indicates the number of times a certain integer has been divided by the same quality factor)

* @ Return returns a list of all quality factor objects carrying this integer.

*/

Class IntegerWhole {

List <Monomial> list; // defines a linked List to load all the quality factor objects, but it cannot be initialized because once initialized, the second number adds a quality factor to the linked list and inserts it after the initialized linked list, instead of starting from 0.

Int temp = 2; // Divisor

Monomial monomial; // uninstantiated object

Public List <Monomial> factorization (int number ){

List = new ArrayList <Monomial> ();

For (int I = 2; I <= number; I ++ ){

Int remainder = 0; // initialize the remainder. It can only be used as a local variable of this class. If it is a global variable, it will produce the same effect as the list.

Int count =-1; // The number of times the same prime factor of the initialization integer appears. The value can be-1 or zero. if the value is zero, the count value should be placed in the if statement.

While (remainder = 0 ){

Remainder = number % I; // obtain the remainder. If the remainder is 0, it indicates that I is a quality factor of number. Otherwise, it is not

If (remainder = 0) {// restriction condition: only when the remainder is zero, that is, I is a quality factor, the number is divided by the quality factor again to obtain the next number involved in the quality factor.

Number/= I; // assign the quotient to the number and continue to take the quality factor down.

}

Count ++;

}

Monomial = new Monomial (I, count); // initializes a power function object and transmits the base number and Index

List. add (I-2, monomial); // add the initialized power function object to the linked list

}

Return list;

}

}


/**

* As a judgment class, you have a judgment method used to determine the one with the most quality factors in a given positive integer and output it

* @ Author LiuYong

* @ Param a and B initialize the numbers of quality factors that receive numbers of min and max respectively.

* @ Version 2013-9-1 2:32:17 pm

*/

Class Judge {

IntegerWhole intwho = new IntegerWhole ();

Monomial mon = new Monomial ();

// Initialize a and B

Int a = 0;

Int B = 0;

/**

* Used to determine the maximum number of quality factors in two input numbers and return them

* @ Return returns a string, which can be either a prompt statement or a maximum number of quality factors.

*/

Public String judgeMax (int min, int max ){

If (min <= max) {// intercept all parameters that do not meet the question of a <B, and return a prompt statement

If (min <= 0 | max <= 0) {// intercept all parameters that are not positive integers and return a prompt statement

Return "the two numbers to be compared cannot have one non-positive integer! ";

} Else {

A = mon. Count (intwho. factorization (min); // obtain the number of quality factors with a small value.

B = mon. Count (intwho. factorization (max); // obtain the number of quality factors that are larger than others.

Return a <B? Integer. toString (B): Integer. toString (a); // returns the number of quality factors in the string format.

}

} Else {

Return "The question 'A <= B 'is not satisfied ";

}

}

}



/**

* Main idea: a power function is required to know the number of occurrence of a quality factor.> an integer is required to calculate the number of factors based on the quality factor index) --> compare the two obtained factors to obtain a judgment class required by the handler)

* @ Author LiuYong

* @ Version 2013-9-1 3:29:54 pm

*/

Public class Section1_3 {

Public static void main (String [] args ){

Judge judge = new Judge ();

Repeated scan = new partition (System. in );

System. out. println ("Enter the two numbers to be compared ");

Int SA1 = scan. nextInt ();

Int SCA 2 = scan. nextInt ();

System. out. println (judge. judgeMax (sca1, SCA 2 ));

}

}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.