Algorithm training 5-2 seeking exponential time limit: 1.0s memory Limit: 256.0MB problem description known N and m, print n^1,n^2,...,n^m. Required to be implemented with static variables. N^m represents the M-th side of N. Known N and m, print n^1,n^2,...,n^m. Required to be implemented with static variables. N^m represents the M-th side of N. (Each line shows 5 numbers, each with a width of 12, right-justified) the sample enters a sample input that satisfies the problem.
Cases:
3 8 Sample output corresponds to the output of the sample input above.
Cases:
Data size and convention the range of each number in the input data.
Example: N^m is less than the representation range of Int.
Topic Analysis:
This is a subject of violent cracking. You need to have a record of N's M-time num, make num = 1, then loop m times, give Num multiply n each time, and then use the printf formatted output.
you should avoid using (int) Math.pow (n, i) every time i + one loop to calculate the M-time of N, as this will result in a m-1 repeat calculation. When we calculate the 3 times of N, the 2 times of N has been calculated, so setting a record number will make the program more efficient to execute. Example code:
1 ImportJava.util.Scanner;2 3 Public classMain {4 Public Static voidMain (string[] args) {5Scanner sc =NewScanner (system.in);6 intn =sc.nextint ();7 intm =sc.nextint ();8 intnum = 1;//Record n's i-th square9 for(inti = 1; I <= m; i++){TenNum *=N; OneSystem.out.printf ("%12d", num); A if(i% 5 = = 0){ -System.out.print ("\ n"); - } the } - } -}
Blue Bridge Cup algorithm training ALGO-149 5-2 Evaluation Index