Blue bridge practice BASIC-14-BASIC-18 (time conversion - rectangular area intersection)
BASIC14: time conversion
Problem description
Given a time t in seconds, it is required to use the format of "::" to represent this time. Represents time, minutes, and seconds. They are all integers without leading "0". For example, if t=0, the output should be "0:0:0"; If t=3661, output "1:1:1".
Input format
The input has only one line, which is an integer t (0 < = T < = 86399).
Output format
The output has only one line, which is the time expressed in the format of "::" without quotation marks.
sample input
0
sample output
0:0:0
sample input
5436
sample output
1:30:36
Train of thought: for the above problems, observation can be obtained. 3600 seconds for an hour and 60 seconds for a minute. So we can use the input T to represent H:M:S
The code is as follows:
import java.util.Scanner; public class Time { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); int H = t / 3600; int M = (t % 3600) / 60; int S = ((t % 3600) % 60); System.out.println(H + ":" + M + ":" + S); } }
BASIC15: string comparison
Problem description
Given two strings of uppercase or lowercase letters only (length between 1 and 10), the relationship between them is one of the following 4 cases:
1: two strings have different lengths. For example, Beijing and Hebei
2: the two strings are not only equal in length, but also the characters at the corresponding positions are exactly the same (case sensitive), such as Beijing and Beijing
3: the length of two strings is equal, and the characters in the corresponding positions can be completely consistent only on the premise of case insensitive (that is, it does not meet case 2). For example, BEIjing and BEIjing
4: the two strings are equal in length, but even case insensitive cannot make the two strings consistent. For example, Beijing and Nanjing
Program to judge which of the four categories the relationship between the two input strings belongs to, and give the number of the class to which it belongs.
Input format
Includes two lines, each of which is a string
Output format
There is only one number indicating the relationship number of the two strings
sample input
BEIjing
beiJing
sample output
3
Idea: judge whether the position of each character is equal, case, etc. Judge 1234 corresponding to output 1
tip: 1.java Math class methods can help solve problems
2.equals() determines whether the number object is equal to the parameter.
3. The equalsignorecase() method is used to compare the string with the specified object, regardless of case.
public class Compare { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s1 = in.nextLine(); String s2 = in.nextLine(); if (s1.length() != s2.length()) { System.out.println(1); } else if (s1.equals(s2)) { System.out.println(2); } else if (s1.equalsIgnoreCase(s2)) { System.out.println(3); } else { System.out.println(4); } } }
BASIC16: decomposition quality factor
Resource constraints
Time limit: 1.0s memory limit: 512.0MB
Problem description
Find the prime factor decomposition of all integers in the interval [a,b].
Input format
Enter two integers a, b.
Output format
Each row outputs the decomposition of a number, such as k=a1a2a3... (A1 < = A2 < = A3..., K is also from small to large) (see the example for details)
sample input
3 10
sample output
3=3
4=22
5=5
6=23
7=7
8=222
9=33
10=25
Tips
Sift out all primes before decomposing.
Data scale and agreement
2<=a<=b<=10000
Idea: prime number refers to the number greater than 1 Natural number In, there is no other than 1 and itself factor Natural number of.
import java.util.Scanner; public class Third{ public static void main(String[] args) { //Enter the interval [a,b] Scanner in = new Scanner(System.in); int a = in.nextInt(); int b = in.nextInt(); in.close(); for(int i = a;i <= b;i++) { Decompose(i); } } //Define a decomposition method private static void Decompose(int n) { //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.print(i); System.out.println(); break; } } } }
BASIC17: matrix multiplication
Problem description
Given an N-order matrix A, output the M-power of a (M is a nonnegative integer)
For example:
A =
1 2
3 4
The second power of A
7 10
15 22
Input format
The first row is a positive integer N, M (1 < = N < = 30, 0 < = M < = 5), representing the order of matrix A and the required idempotent
Next, N rows, N non negative integers with absolute value no more than 10 in each row, describe the value of matrix A
Output format
Output a total of N rows, n integers per row, representing the matrix corresponding to the M-power of A. Adjacent numbers are separated by a space
sample input
2 2
1 2
3 4
sample output
7 10
15 22
Idea: matrix The most important method of multiplication is the general matrix product. It only makes sense when the number of columns of the first matrix is the same as the number of rows of the second matrix [1]. General matrix product refers to general matrix product. One m × The matrix of n is m × A number matrix in which n numbers are arranged in M rows and N columns. Because it compactly gathers many data together, it can easily represent some complex models, such as power system network model
import java.util.Scanner; public class Fourth { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); long[][] a = new long[n][n]; long[][] b = new long[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { a[i][j] = b[i][j] = in.nextLong(); } } if (m == 0) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) { System.out.print(1 + " "); } else { System.out.print(0 + " "); } } System.out.println(); } } else if (m == 1) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(a[i][j]); } System.out.println(); } } else { for (int z = 1; z < m; z++) { long[][] tmp = new long[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { long add = 0; for (int y = 0; y < n; y++) { add += a[i][y] * b[y][j]; } tmp[i][j] = add; } } b = tmp; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(b[i][j] + " "); } System.out.println(); } } in.close(); } }
BASIC18: rectangular area intersection
Problem description
There are two rectangles on the plane, and their edges are parallel to the X or Y axis of the rectangular coordinate system. For each rectangle, we give its coordinates of a pair of relative vertices. Please program to calculate the area of the intersection of the two rectangles.
Input format
The input contains only two lines, each describing a rectangle.
In each row, the coordinates of a pair of relative vertices of the rectangle are given, and the coordinates of each point are represented by two real numbers with absolute values of no more than 10 ^ 7.
Output format
The output contains only one real number, which is the area of intersection and is reserved to two decimal places.
sample input
1 1 3 3
2 2 4 4
sample output
1.00
Idea: abs() returns the absolute value of the parameter. Parameters can be int, float, long, double, short, byte types.
Use of Math class
import java.util.Scanner; public class Fifth { public static void main(String[] args) { Scanner in = new Scanner(System.in); //Rectangle one double x1 = in.nextDouble(); double y1 = in.nextDouble(); double x2 = in.nextDouble(); double y2 = in.nextDouble(); //Rectangular two double x3 = in.nextDouble(); double y3 = in.nextDouble(); double x4 = in.nextDouble(); double y4 = in.nextDouble(); //Find the maximum and minimum x,y of rectangle one and two //Rectangle one x double OnexMax = Math.max(x1, x2); double OnexMin = Math.min(x1, x2); //Rectangle one y double OneyMax = Math.max(y1, y2); double OneyMin = Math.min(y1, y2); //Rectangular 2x double TwoxMax = Math.max(x3, x4); double TwoxMin = Math.min(x3, x4); //Rectangular DIY double TwoyMax = Math.max(y3, y4); double TwoyMin = Math.min(y3, y4); //Judge whether two rectangles do not intersect if (OnexMax <= TwoxMin || OnexMin >= TwoxMax || OneyMax <= TwoyMin || OneyMin >= TwoyMax) { System.out.println("0.00"); } else { //Find intersection coordinates double x = Math.max(OnexMin, TwoxMin); double y = Math.min(OneyMax, TwoyMax); double xx = Math.min(OnexMax, TwoxMax); double yy = Math.max(OneyMin, TwoyMin); System.out.printf("%.2f", Math.abs(yy - y) * Math.abs(xx - x)); } } }