Simple algorithm exercise

Posted by rebelo on Sun, 23 Feb 2020 04:35:30 +0100

[1] Two-point distance

describe

Enter two-point coordinates (X1,Y1), (X2,Y2) (0<=x1, x2, y1, y2<=1000), calculate and output the distance between the two points.

input

The first line enters an integer n (0<n<=1000) to indicate that there are N sets of test data;
Each group then takes up a row and consists of four real numbers, representing x1,y1,x2,y2, separated by spaces.

output

For each set of input data, one row is output, leaving two decimal places in the result.

sample input

2

0 0 0 1

0 1 1 0

sample output

1.00

1.41

package cn.ls.lanqiao;

import java.text.DecimalFormat;
import java.util.Scanner;

public class Test1 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		float[] a = new float[n];
		for (int i = 0; i < n; i++) {
			float x1 = sc.nextFloat();
			float y1 = sc.nextFloat();
			float x2 = sc.nextFloat();
			float y2 = sc.nextFloat();
			float sum = (float) Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
			a[i] = sum;
		}
		for (int i = 0; i < a.length; i++) {
			System.out.println(new DecimalFormat("0.00").format(a[i]));
		}
	}
}

 

[2] Cut noodles

describe

One high-gluten noodle pull, cut in the middle to get 2 noodles.If you fold it once and cut it in the middle, you will get three noodles.If you fold twice in a row and cut in the middle, you will get five noodles.So how much noodle would you get if you folded 10 times in a row and cut in the middle?

package cn.ls.lanqiao;

public class Test2 {
	public static void main(String[] args) {
		int sum = 0;
		for (int i = 1; i <= 10; i++) {
			sum = 2 * i + 1;
		}
		System.out.println(sum);
	}
}

 

[3] interchange of case

describe

Now we give a string with only uppercase and lowercase letters, no spaces and no line breaks. We need to change the uppercase to lowercase, the lowercase to uppercase, and then output the interchanged string.

input

The first row has only one integer m (m<=10), which represents the number of test data groups.
The next m lines, each with a string (no longer than 100).

output

Output interchanged strings, one line for each set of outputs.

sample input

2

AcmdahSH

ACCEPTED

sample output

aCMDAHsh

accepted

package cn.ls.lanqiao;

import java.util.Scanner;

public class Test3 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int m = sc.nextInt();
		String[] o = new String[m];
		for (int i = 0; i < m; i++) {
			String ls = sc.next();
			o[i] = daXiao(ls);
		}
		for (int i = 0; i < o.length; i++) {
			System.out.println(o[i]);
		}
	}

	public static String daXiao(String ls) {
		StringBuffer a = new StringBuffer();
		for (int i = 0; i < ls.length(); i++) {
			char s = ls.charAt(i);
			if (s >= 'a' && s <= 'z') {
				char q = (char) (s - 32);
				a.append(q);
			}
			if (s >= 'A' && s <= 'Z') {
				char p = (char) (s + 32);
				a.append(p);
			}
		}
		return a.toString();
	}
}

 

[4] Sanyang Xianrui

Look at the following addition formula:

 

* Xiang a Rui b Sheng c Huid

+) Three eys of sheep f donated g Rei b

-----------------------

3 e Sheep f Sheng c Rui b Qi h

Among them, the same Chinese character represents the same number, and different Chinese characters represent different numbers.

Please use your computer to calculate the 4 digits represented by "Sanyang Xianrui" (the only answer),

package cn.ls.lanqiao;

public class Test4 {
	public static void main(String[] args) {
		for (int a = 0; a <= 9; a++) {
			for (int b = 0; b <= 9; b++) {
				for (int c = 0; c <= 9; c++) {
					for (int d = 0; d <= 9; d++) {
						for (int f = 0; f <= 9; f++) {
							for (int g = 0; g <= 9; g++) {
								for (int h = 0; h <= 9; h++) {
									if (a != b && a != c && a != d && a != f && a != g && a != h && b != c && b != d
											&& b != f && b != g && b != h && c != d && c != f && c != h && c != g
											&& d != f && d != g && d != h && f != g && f != h && g != h && a != 1
											&& b != 1 && c != 1 && d != 1 && f != 1 && g != 1 && h != 1) {
										int sum = a * 1000 + b * 100 + c * 10 + d;
										int sum1 = 1 * 1000 + f * 100 + g * 10 + b;
										int sum2 = 1 * 10000 + f * 1000 + c * 100 + b * 10 + h;
										if ((sum + sum1) == sum2) {
											System.out.println(1 + "" + f + g + b);
											System.out.println(2 + "--" + a + b + c + d + f + g + h);
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}

 

Sum of Matrix Diagonals

describe

Sum the diagonal elements of a 3*3 matrix

input

Enter a 2-D array of 3*3

output

Sum of two diagonals of a two-dimensional array

sample input

1 2 3

4 5 6

7 8 9

sample output

15

15

package cn.ls.lanqiao;

import java.util.Scanner;

public class Test5 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int[][] a = new int[3][3];
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[0].length; j++) {
				a[i][j] = sc.nextInt();
			}
		}
		int sum1 = a[0][0]+a[1][1]+a[2][2];
		int sum2 = a[0][2]+a[1][1]+a[2][0];
		System.out.println(sum1);
		System.out.println(sum2);
	}
}

 

Hollow diamond

Please design a program that runs as follows:

 

package cn.ls.lanqiao;

import java.util.Scanner;

public class Test6 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		for (int i = 0; i < n; i++) {
			lingXing(sc.nextInt());
		}

	}

	public static void lingXing(int n) {// Output diamonds of different sizes according to the length of input
		for (int i = 1; i <= n; i++) {
			if (i == 1) {
				for (int j = 1; j <= n - i; j++) {
					System.out.print(" ");
				}
				System.out.print("*");
			} else {
				for (int j = 1; j <= n - i; j++) {
					System.out.print(" ");
				}
				System.out.print("*");
				for (int j = 1; j <= 2 * (i - 1) - 1; j++) {
					System.out.print(" ");
				}
				System.out.print("*");
			}
			System.out.println();
		}
		for (int i = n - 1; i >= 1; i--) {
			if (i == 1) {
				for (int j = 1; j <= n - i; j++) {
					System.out.print(" ");
				}
				System.out.print("*");
			} else {
				for (int j = 1; j <= n - i; j++) {
					System.out.print(" ");
				}
				System.out.print("*");
				for (int j = 1; j <= 2 * (i - 1) - 1; j++) {
					System.out.print(" ");
				}
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

 

[7] Date calculation

Time limit: 3000 ms | Memory limit: 65535 KB

Difficulty: 1

describe

For example, enter a date, such as: 2010 10 24, to determine the day is the day of the year.

input

The first line enters a number N (0<N<=100) to indicate that there are N sets of test data.The following N lines enter multiple sets of input data, each of which is a date entered in the format required by the title.

output

The output of each set of input data takes up one row, and the number of days determined by the output is n

sample input

3

2000 4 5

2001 5 4

2010 10 24

sample output

96

124

297

package cn.ls.lanqiao;

import java.util.Scanner;

public class Test7 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] ls = new int[n];

		for (int i = 0; i < n; i++) {
			int year = sc.nextInt();
			int month = sc.nextInt();
			int days = sc.nextInt();
			ls[i] = riQi(year, month, days);
		}
		for (int i = 0; i < n; i++) {
			System.out.println(ls[i]);
		}
	}

	public static int riQi(int year, int month, int days) {
		int[] list = { 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
		int count = 0;
		if (month == 1) {
			count = days;
		}
		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
			if (month > 1 && month < 13) {
				count = list[month - 2] + days;
			}
		} else {
			if (month > 1 && month < 13) {
				count = list[month - 2] + days - 1;
			}
		}
		return count;

	}

}

 

 

 

 

166 original articles were published. 178 were praised. 10,000 visits+
Private letter follow

Topics: Java