Java BASIC Programming 50 questions (4-6 questions) detailed

Source: Internet
Author: User
Tags greatest common divisor

First, describe

1, the decomposition of a positive integer factorization. For example: Enter 90 and print out 90=2*3*3*5.

Program Analysis: The decomposition of n factorization, should first find a minimum prime number k, and then the following steps to complete:

(1) If the prime number is exactly equal to N, then the decomposition factorization process has ended and all the factors before the output.

(2) if n!=k, but n can be divisible by K, the value of k should be printed, and the quotient of n divided by K as the new positive integer n, repeat the first step.

(3) If n cannot be divisible by K, the first step is repeated with k+1 as the value of K.

2, the use of the nesting of conditional operators to complete the problem: Academic scores >=90 points of the students with a, 60-89 between the use of B, 60 points below the C is indicated.

Program Analysis: (a>b)? A:b This is a basic example of conditional operators.

3, enter two positive integers m and N, seek their greatest common divisor and least common multiple.


Second, the source code

1. Procedure 1

Package Tong.yue.hong;import java.util.scanner;/* * Title: Decomposition of a positive integer factorization. For example: Enter 90 and print out 90=2*3*3*5. Program Analysis: The decomposition of n factorization, should first find a minimum prime number k, and then the following steps to complete: (1) If the prime number is exactly equal to N, then the decomposition of the factorization process has ended, printing can be. (2) if n<>k, but n can be divisible by K, the value of k should be printed, and n divided by the quotient of K, as a new positive integer n, repeat the first step. (3) If n cannot be divisible by K, the first step is repeated with k+1 as the value of K. */public class Zhiyinshu {public static void main (string[] args) {System.out.println ("Please input a number bigger than on E: "); Scanner scan = new Scanner (system.in); int num = Scan.nextint (); while (num<=1) {System.out.println ("Input number error, Please input again! ") num = Scan.nextint ();} Decompose (num);d ecompose2 (num);} /** * Use string concatenation * @param n */private static void decompose (int n) {System.out.print (n+ "="); for (int i=2;i<=n;i++) {while (n %i==0 && n!=i) {n/=i; System.out.print (i+ "*");} if (n==i) {System.out.println (i); break;}}} /** * StringBuilder Format OUTPUT results * @param num */private static void decompose2 (int num) {StringBuilder sbuilder = new Stringbuil Der (); Sbuilder.append (num+ "="); int i = 2;while (num!=1) {if (num%i==0) {sbuilder.append (i+ "*"); num/=i;} else {i++;}} String resulstring=sbuilder.tostring (); System.out.println (resulstring.substring (0, Resulstring.length ()-1));}}
Operation Result:



2. Procedure 2

Package Tong.yue.hong;import java.util.scanner;/** * Title: Use the nesting of conditional operators to complete this problem: Students with a score of >=90 A, 60-89 points between the use of B to express, 60 points or less is indicated in C. Program Analysis: (a>b)? A:b This is a basic example of conditional operators. * @author Administrator * */public class Gradelevel {public static void main (string[] args) {Scanner Scanner = new Scanner (system.in); SYSTEM.OUT.PRINTLN ("Please input a score: (0-100)"), int score = Scanner.nextint (); while (score<0| | score>100) {System.out.println ("Input score error,please input a score again:"); score = Scanner.nextint ();} Conditional operator nesting, if the output level A is greater than or equal to 90, otherwise continue to determine whether it is greater than or equal to 60, if it is B, otherwise, cstring levels = score>=90? " A ": score>=60?" B ":" C "; System.out.println (score+ "grade is" +level);}}
Operation Result:



3. Procedure 3

Package Tong.yue.hong;import java.util.scanner;/** * Enter two positive integers m and N to find their greatest common divisor and least common multiple. * @author Tong * */public class Commondivmulpi {public static void main (string[] args) {Scanner Scanner = new Scanner (Syst em.in); SYSTEM.OUT.PRINTLN ("Please input the first number (num>0):"), int firstnum = Scanner.nextint (); while (firstnum<0) { SYSTEM.OUT.PRINTLN ("Input error,please input the first number again (num>0):"); firstnum = Scanner.nextint ();} SYSTEM.OUT.PRINTLN ("Please input the second number (num>0):"), int secondnum = Scanner.nextint (); while (secondnum< 0) {System.out.println ("input error,please input the second number again (num>0):"); secondnum = Scanner.nextint ();} If the two numbers are equal, then least common multiple is one of them, without calling the method to determine if (firstnum==secondnum) {System.out.println (firstnum+ "and" +secondnum+ "least common multiple as" + Firstnum); Greatcommondivisor (firstnum,secondnum);} else {leastcommonmultiple (firstnum,secondnum); Greatcommondivisor (firstnum,secondnum);} Max_min (firstnum,secondnum);} /** * Least common multiple: first get the larger of the two number, if the larger of the two is a multiple of the smaller, then the larger is two number least common multiple* This code uses the enumeration method: If the two do not multiply the number of relations, then take the larger number of multiples (starting from 2 to increase), * with this number divided by the smaller number, if divisible, the number is the demand, otherwise continue to multiply repeat the above part * For example: 12 and 20 least common multiple, 20 can not divide 12, Will 20*2=40,40 not divisible by 12, will 20*3=60, at this time 60 divisible 12, so the least common multiple for the two * @param firstnum * @param secondnum */private static void Leastcommo Nmultiple (int firstnum, int secondnum) {//Determine the larger of the two if (Firstnum>secondnum) {if (firstnum%secondnum==0) { The least common multiple of System.out.println (firstnum+ "and" +secondnum+ ") is" +firstnum ";} else {//If two numbers coprime, then the worst case is that common multiple is the product of both for (int i = 2; I <= secondnum; i++) {if (firstnum*i%secondnum==0) {System.out.println (firstnum+ "and" +secondnum+ "least common multiple" +firstnum*i "); else {if (secondnum%firstnum==0) {System.out.println (firstnum+ "and" +secondnum+ "least common multiple as" +secondnum ");} else {//If two numbers coprime, then the worst case is that common multiple is the product of both for (int i = 2; I <= firstnum; i++) {if (secondnum*i%firstnum==0) {System.out.println ( firstnum+ "and" +secondnum+ "least common multiple for" +secondnum*i "; break;}}}} /** * Greatest Common divisor: if the number of double-digit relationship, then the smaller number is greatest common divisor * Otherwise, the smaller number of cycles divided by the smaller number of all factors (small to large) results to cycle apart from the larger number, if divisible, the number is greatest common divisor, otherwise continue to loop * For example: 36 and 10 for greatest common divisor, 36 is not a multiple of 10, it is removed with a factor of less than 10Decimal, except after the result is removed 36, then with 36% (10/2) =36%5 cannot be divisible, 36% (10/5) =36%2==0 divisible, so greatest common divisor is 2 * @param firstnum * @param secondnum */private static void Greatcommondivisor (int firstnum, int secondnum) {//Determine the larger of the two if (Firstnum>secondnum) {if (firstnum%secondnum==0 {System.out.println (firstnum+ "and" +secondnum+ "greatest common divisor" +secondnum ");} else {//If two numbers coprime, then the worst case is that greatest common divisor is 1for (int i = 2; I <= secondnum; i++) {//Remove the factor if the smaller number (s econdnum/i) ==0) {System.out.println (firstnum+ "and" +secondnum+ "greatest common divisor as" + (secondnum/i) "); else {if (secondnum%firstnum==0) {System.out.println (firstnum+ "and" +secondnum+ "least common multiple as" +firstnum ");} else {//If two numbers coprime, then the worst case is that greatest common divisor is 1for (int i = 2; I <= secondnum; i++) {//Remove the factor if the smaller number (firstnum%i==0&&secondnum% (f irstnum/i) ==0) {System.out.println (firstnum+ "and" +secondnum+ "greatest common divisor for" + (firstnum/i)); break;}}}} /** * for greatest common divisor and least common multiple, using a split division, simultaneously seeking greatest common divisor and least common multiple * where greatest common divisor is the product of all the same factors in the two numbers, and least common multiple is the product of all the same decomposition factors and their own specific factors * @param m * @param n */PR ivate static void max_min (int firstnum, int secondnUM) {int m = firstnum;int N = Secondnum;int temp = 1;int Yinshu = 1;int Beishu = m*n;//Place The lesser of the two numbers in front if (n<m) {temp = N;n = M;m = temp;} Use a m!=0 while () {temp = N%m;n = M;m = temp;} Yinshu = N;beishu/= n; The greatest common divisor of System.out.println (firstnum+ "and" +secondnum+ ") is" +yinshu "; The least common multiple of System.out.println (firstnum+ "and" +secondnum+ ") is" +beishu ";}}

Operation Result:



Java BASIC Programming 50 questions (4-6 questions) detailed

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.