# Computer exercises

Keywords: Java Algorithm data structure leetcode

13. Given two strings containing only lowercase letters, calculate the length of the maximum common substring of the two strings.
Note: the definition of substring refers to a string formed after deleting some prefixes and suffixes (or not deleted).

```import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
String s1=sc.nextLine();
String s2=sc.nextLine();
String max=s1.length()>=s2.length()?s1:s2;
String min=s1.length()>=s2.length()?s2:s1;
int l=0;
String s="";
for (int i = 0; i < min.length(); i++) {
for (int j = i+1; j <= min.length(); j++) {
if (max.contains(min.substring(i,j)) && j-i>l) {
l=j-i;
s=min.substring(i,j);
}
}
}
System.out.println(s.length());
}
sc.close();
}
}
```

14. Verify nikochus theorem, that is, the cube of any integer m can be written as the sum of m consecutive odd numbers.
For example:
1^3=1
2^3=3+5
3^3=7+9+11
4^3=13+15+17+19
Input a positive integer m (m ≤ 100), and write the cube of m as the sum of m consecutive odd numbers for output.

```import java.io.BufferedReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException{
String s;
long m = Integer.valueOf(s);
long a = m*m-m+1;
System.out.print(a);
for(int i=1; i<m; i++){
System.out.print("+"+(a+=2));
}
System.out.println();
}
bf.close();
}
}
```

15. Find the number of uppercase characters (i.e. 'A' -'Z ') in A given string

```import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
char[] ch = sc.nextLine().toCharArray();
int nums = 0;
for(int i=0;i<ch.length;i++){
if(ch[i]>='A' && ch[i]<='Z'){
nums++;
}
}
System.out.println(nums);
}
}
}
```

16. Given a string containing only lowercase letters, find the length of its longest palindrome substring.
The so-called palindrome string refers to a left-right symmetrical string. The so-called substring refers to a string in which some prefixes and suffixes are deleted (or not deleted)

```import java.util.*;
public class Main {
// Linear complexity O(n), find the longest palindrome substring
public static int getLongestPalindromeLength(String str) {
// Process string first
StringBuilder sb = new StringBuilder();
sb.append("#");
for (int i = 0; i < str.length(); i++) {
sb.append(str.charAt(i));
sb.append("#");
}
int maxlen = 0;
for (int i = 0; i < sb.length(); i++) {
int r = 1;
while(i-r>=0 &&i+r<sb.length()&& sb.charAt(i-r)==sb.charAt(i+r)){
r++;
}
maxlen = r > maxlen ? r :maxlen;
}
return maxlen-1;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String str = in.nextLine();
System.out.println(getLongestPalindromeLength(str));
}
}
}
```

17. Find the maximum continuous number of 1 in the binary number corresponding to an int type number. For example, the binary number of 3 is 00000011, and the maximum continuous two 1s

```import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int input = sc.nextInt();
String bi = Integer.toBinaryString(input);
String subs = "1";
int cnt = 1;
for(int i=1; i<=bi.length(); i++) {
if(bi.contains(subs)) {
cnt = subs.length();
subs += "1";
}
}
System.out.println(cnt);
}
}
}
```

18. Please calculate the number of n*m chessboard grids (n is the number of horizontal grids and M is the number of vertical grids) starting from the upper left corner of the chessboard and walking along the edge line from the upper left corner to the lower right corner. It is required that you can't go back, that is, you can only go right and down, not left and up.

```import java.util.*;
public class Main
{
public static void main(String []args)
{
Scanner sc = new Scanner(System.in);
while(sc.hasNext())
{
int n = sc.nextInt();
int m = sc.nextInt();

int top = factorial(m+n);
int behind = factorial(m)*factorial(n);
System.out.println(top/behind);
}
}
public static int factorial(int n)
{
int sum = 1;
for(int i = 1;i<=n;i++)
{
sum *= i;
}
return sum;
}
}
```

19. First enter the number of integers to be entered n, and then enter n integers. The output is the number of negative numbers in n integers and the average value of all positive integers. The result retains one decimal place. 0 is neither a positive integer nor a negative number and is not included in the calculation

```//A simple approach
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
while(s.hasNext()){
int n = s.nextInt();
int[] arr = new int[n];
int countf = 0;
int countz = 0;
double sum = 0.0;
double p = 0.0;
for(int i = 0;i < n;i++){
arr[i] = s.nextInt();
if(arr[i] < 0){
countf++;
}
if(arr[i] > 0){
countz++;
sum += arr[i];
p = (double)(sum / countz);
}
}
System.out.println(countf + " " + String.format("%.1f",p));
}
}
}
```

20. Arithmetic sequence 2, 5, 8, 11, 14.... (3 starting from 2 is the equal difference sequence of tolerance) output the sum of the first n items of the equal difference sequence

```//Summation formula of arithmetic sequence: Sn=a1*n+n*(n-1)*d/2
import java.util.*;

public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNext()){
int n = in.nextInt();
if(n<1){
System.out.println(-1);
}else{
System.out.println((2*n+3*n*(n-1)/2));
}
}
in.close();
}
}
```

21. Reverse the contents of a string str and output it.

```import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.nextLine();//next() is a space encountered; nextLine() is a carriage return encountered
StringBuilder sb = new StringBuilder(str);
System.out.println(sb.reverse().toString());
}
}
}
```

22. The least common multiple of positive integer A and positive integer B refers to the smallest positive integer value that can be divided by A and B. design an algorithm to find the least common multiple of input A and B.

```import java.util.Scanner;
public class Main {
/**
* Finding the least common multiple: formula method
* The least common multiple of two numbers a and B is a*b/gcd(a,b)
* Since the product of two numbers is equal to the product of the maximum common divisor and the minimum common multiple of the two numbers, that is (a, b) ×  [a，b] = a  ×  b
* Therefore, to find the least common multiple of two numbers, we can first find their maximum common divisor, and then use the above formula to find their least common multiple.
*/
public static int lcm(int m, int n) {
return (m * n) / gcd(m, n);
}
/**
* Finding the greatest common divisor: rolling Division
* 1\. a/b，Let R be the remainder (0 ≤ r < b). If r=0, the algorithm ends and a is the answer
* 2\. Interchange: set a ← b, b ← r, and return to step 1
*/
private static int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
System.out.println(lcm(a, b));
}
}

```

Posted by wesley1189 on Mon, 29 Nov 2021 13:41:36 -0800