Java course class assignment code

preface

This article only records the classroom homework codes assigned by the classroom teachers. The topics are relatively simple, so there is no problem-solving idea, which I believe everyone can understand. Of course, some of the solutions and codes are not optimal. At that time, they were just to complete the topics, and later they were too lazy to change. If there are inappropriate or incorrect places, you are welcome to point out

Note: some forgot to record the title information. Please forgive me and make do with it

1,

view code
package java_works;

import java.util.Scanner;

public class java_10001 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		String b=sc.nextLine();
		int zm=0,kg=0,qt=0;
		for(int i=0;i<b.length();i++)
		{
			if(b.charAt(i)>=65&&b.charAt(i)<=90)
				zm++;
			else if(b.charAt(i)>=97&&b.charAt(i)<=122)
				zm++;
			else if(b.charAt(i)==32)
				kg++;
			else
				qt++;
		}
		System.out.println("letter:"+zm);
		System.out.println("Space:"+kg);
		System.out.println("other:"+qt);
	}

}

2,

view code
package java_works;

import java.text.DecimalFormat;

import java.util.Scanner;

public class java_10002 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		int k=sc.nextInt();
		double sum=0;
		for(double i=0;i<=k;i++)
		{
			sum=sum+((i+6)/(i+10));
		}
		System.out.print("sum:");
		System.out.printf("%.5f",sum);
	}

}

3. Enter a positive integer N and output a prime number between [1, N]

view code
package java_works;

import java.util.Scanner;

public class java_10003 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		int N=-1;
		N=sc.nextInt();
		for(int i=2;i<=N;i++)
		{
			int j;
			for(j=2;j<=Math.sqrt(i);j++)
			{
				if(i%j==0)break;
			}
			if(j>Math.sqrt(i))
				System.out.print(i+" ");
		}
	}

}

4. The user inputs the positive integer k as the number between 3-6, and then outputs the number of daffodils with K digits

view code
package java_works;

import java.util.Scanner;

public class java_10004 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		while(true) {
			Scanner sc=new Scanner(System.in);
			int K=-1;
			K=sc.nextInt();
			if(K == 0)
			{
				break;
			}
			int a[] = new int[K];
			int num = (int) Math.pow(10, K - 1) + 1;
			while (num <= Math.pow(10, K)) {
	            int sum = 0;
	            for (int j = 0; j < K; j++)
	                a[j] = (int) (num / Math.pow(10, j) % 10);
	            for (int j = 0; j < K; j++) 
	                sum = sum + (int) Math.pow(a[j],K); 
	            if (num == sum)
	                System.out.print(num + " ");
	            num++;
	        }
		}
		
	}

}

5. Enter the positive integer k, generate and store the k-order square matrix with a two-dimensional array. The first line: 1, 2, 3, K. The second behavior: K + 1, K + 2, K+K, ... And so on; Then output the transpose of the square matrix.

view code
package java_works;

import java.util.Scanner;

public class java_10005 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int K = sc.nextInt();
		int a[][] = new int[K][K];
		int b[][] = new int[K][K];
		int z=1;
		for(int i=0;i<K;i++)
		{
			for(int j=0;j<K;j++)
			{
				a[i][j]=z;
				z++;
			}
		}
		for(int i=0;i<K;i++)
		{
			for(int j=0;j<K;j++)
			{
				b[j][i]=a[i][j];
			}
		}
		for(int i=0;i<K;i++)
		{
			for(int j=0;j<K;j++)
			{
				System.out.print(b[i][j]+" ");
			}
			System.out.println();
		}
	}
}

6. Enter a positive integer N, then enter N floating-point numbers, and then calculate and output their average and variance.

view code
package java_works;

import java.util.Scanner;

public class java_10006 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = -1;
		N = sc.nextInt();
		double[] a = new double[N];
		for(int i=0;i<a.length;i++)
		{
			a[i]=sc.nextDouble();
		}
		Double sum=0.0;
		for(int i=0;i<a.length;i++)
		{
			sum=sum+a[i];                   //Sum
		}
		double mean = 0.0,diff=0.0;
		mean = sum / N;
		double c=0.0;
		for(int i=0;i<a.length;i++)
		{
			c=c+Math.pow((a[i]-mean),2);
		}
		diff=c/N;
		System.out.printf("mean:"+"%.5f",mean);
		System.out.println();
		System.out.printf("diff:"+"%.5f",diff);
	}

}

7. Enter a positive integer N, then enter N floating-point numbers, and then calculate and output their minimum and maximum values.

view code
package java_works;

import java.util.Scanner;

public class java_10007 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = -1;
		N = sc.nextInt();
		Double[] a = new Double[N];
		for(int i=0;i<N;i++)
		{
			a[i]=sc.nextDouble();
		}
		double minv = a[0],maxv=a[0];
		for(int i=0;i<a.length;i++)
		{
			if(a[i]<minv)
				minv=a[i];
			if(a[i]>maxv)
				maxv=a[i];
		}
		System.out.printf("minv:"+"%.5f",minv);
		System.out.println();
		System.out.printf("maxv:"+"%.5f",maxv);
		
	}

}

8. Enter a positive integer N, then enter N floating-point numbers, sort from small to large, and output the sorting results.

view code
package java_works;

import java.util.Scanner;

public class java_10008 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = -1;
		N = sc.nextInt();
		double[] a = new double[N];
		for(int i=0;i<a.length;i++)
		{
			a[i] = sc.nextDouble();
		}
		for(int i=0;i<a.length;i++)
			for(int j=0;j<a.length-1;j++)
			{
				double temp;
				if(a[j]>a[j+1])
				{
					temp=a[j+1];
					a[j+1]=a[j];
					a[j]=temp;
				}	
			}
		for(int i=0;i<a.length;i++)
		{
			System.out.printf("%.5f\n",a[i]);
		}
	}	
}

9. Enter a line of string, and then capitalize the first letter of each word

view code
package java_works;

import java.util.Scanner;

public class java_10009 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String str ;
		str = sc.nextLine();
		daxie(str);
	}
	public static void daxie(String s){
		String a[] = s.split(" ");
		for(int i=0;i<a.length;i++)
		{
			String s1=a[i].substring(0,1).toUpperCase()+a[i].substring(1);
			System.out.print(s1+" ");
		}
	}
}

10. Enter a line of string from the keyboard. Please adjust the reverse order between words. For example: "cat loves dog" is reversed to "dog loves cat".

view code
package java_works;

import java.util.Scanner;

public class Java_10010 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String str;
		str = sc.nextLine();
		nixu(str);
	}
	public static void nixu(String s) {
		String[] a = s.split(" ");
		String temp = "";
		for (int i = 0; i < a.length/2; i++) {
			temp = a[i];
			a[i] = a[a.length-1-i];
			a[a.length-1-i] = temp;
		}
		for(int i=0;i<a.length;i++){
			System.out.print(a[i]+" ");
		}
		
	}

}

11. Spacer normalization. Enter a line of string that uses spaces as spacers between words, symbols, and so on. However, due to the problem of user operation, multiple spaces may be entered when entering the spacer. Now it is required to process the input string, clear the beginning and end spaces of the string, and ensure that there can only be one space in the spacer in the string

view code
package java_works;

import java.util.Scanner;

public class java_10011 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		guiFanhua(str);
	}
	public static void guiFanhua(String s) {
		s=s.trim();                      //Remove the leading and trailing space characters of the string
		StringBuffer sb = new StringBuffer();
		int flag;
		for(int i=0;i<s.length();i++)
		{
			flag = 0;
			if(s.charAt(i)!=' ')
				sb.append(s.charAt(i));
			else {
				flag = 1;
			}try {
				if(s.charAt(i)==' '&&s.charAt(i+1)!=' ') {
					sb.append(' ');
				}
			}catch(Exception e) {
				continue;
			}
		}
		System.out.println(sb);
	}
}

12. Enter m and N, and then calculate the free fall of a ball from the height of M meters. After each landing, jump back to half of the original height, then fall and rebound. Calculate how many meters it passed on the nth landing? How high is the rebound on the nth landing

view code
package java_works;

import java.util.Scanner;

public class java_10012 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int M = sc.nextInt();
		int N = sc.nextInt();
		Solve(M,N);
	}
	public static void Solve(int a,int b) {
		double L = 0.0,H = 0.0;
		for(int i=1;i<b;i++)
		{
			L=L+a/(Math.pow(2, i-1));
		}
		H=a/(Math.pow(2,b));
		System.out.printf("%.5f\n",L+a);
		System.out.printf("%.5f",H);
	}

}

13. Enter a positive integer n, using 1,2,3, N numbers number n students, and n students form a circle. Then start counting from the first person (counting from 1 to 5). Those who report to 5 quit the circle and ask who left the original number?

view code
package java_works;

import java.util.Scanner;

public class java_10013 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = -1;
		N = sc.nextInt();
		Solves(N);
	}
	public static void Solves(int a) {
		int[] b = new int[a];
		for(int i=0;i<a;i++)
		{
			b[i]=i+1;
		}
		int currentNum = 1;
		int count = b.length;
		for(int i=0;;i++)
		{
			if(count==1) {
				break;
			}
			if(i>=b.length) {
				i=0;
			}
			if(b[i]==0) {
				continue;
			}
			if(currentNum % 5 == 0) {
				count--;
				b[i]=0;
			}
			if(currentNum == 5) {
				currentNum = 1;
			}
			else {
				currentNum++;
			}
		}
		for(int i=0;i<b.length;i++)
		{
			if(b[i]!=0) {
				System.out.println(b[i]);
			}
		}
	}

}

14. Enter a certain day of a certain month of a certain year, and the output of this day is the day of the year. Pay special attention to the judgment of leap years, handle the situation of leap years, input a line of string, count and output the number of occurrences of each character in the string

view code
package java_works;

import java.util.Scanner;

import java.util.GregorianCalendar;

public class java_10014 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int year = 0,month = 0, day = 0;
		year = sc.nextInt();
		month = sc.nextInt();
		day = sc.nextInt();
		Solves(year,month,day);
	}
	public static void Solves(int n,int y,int r) {
		GregorianCalendar gre = new GregorianCalendar();//Method of judging whether it is a leap year
		boolean isLeapYear = gre.isLeapYear(n);//If the return value is true, it indicates that it is a profit year. If the return value is false, it is not a leap year
		int ap = isLeapYear?29:28;//Judge the number of days in February
		int days=0;
		switch(y) {
		case 1:
			days=r;
			break;
		case 2:
			days=31+r;
			break;
		case 3:
			days=31+ap+r;
			break;
		case 4:
			days=31+ap+31+r;
			break;
		case 5:
			days=31+ap+31+30+r;
			break;
		case 6:
			days=31+ap+31+30+31+r;
			break;
		case 7:
			days=31+ap+31+30+31+30+r;
			break;
		case 8:
			days=31+ap+31+30+31+30+31+r;
			break;
		case 9:
			days=31+ap+31+30+31+30+31+31+r;
			break;
		case 10:
			days=31+ap+31+30+31+30+31+31+30+r;
			break;
		case 11:
			days=31+ap+31+30+31+30+31+31+30+31+r;
			default:
				days=31+ap+31+30+31+30+31+31+30+31+30+r;
		}
		System.out.println(days);
	}

}

15. Enter a line of string, count and output the number of occurrences of each character in the string

view code
package java_works;

import java.util.Iterator;

import java.util.Scanner;

import java.util.TreeMap;

import java.util.Set;

public class java_10015 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		char[] ch = str.toCharArray();
		method(ch);
		int blank = 0;
		for(int i=0;i<ch.length;i++)
		{
			if(ch[i]==' ')
				blank++;
		}
		System.out.println("Space:"+blank);
	}
	public static void method(char[] array) {
		//Initialize an integer array with a length of 26. The initial values are all 0. Record the number of occurrences of each character in the string
		int[] temp = new int[26];
		for(int i=0;i<temp.length;i++)
		{
			temp[i]=0;
		}
		//Traverse the character array and take the ASCII value corresponding to its value as the subscript of the integer array
		for(int i=0;i<array.length;i++)
		{
			if(array[i]>='a'&&array[i]<='z')
			{
				temp[array[i]-'a']++;
			}
		}
		//Print out each lowercase English letter and the number of occurrences
		for(int i=0;i<temp.length;i++)
		{
			System.out.println((char)(i+'a')+":"+temp[i]);
		}
	}
}	

16. Enter a line of string and output the first repeated character

view code
package java_works;

import java.util.Scanner;

public class java_10016 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		getFirstIndex(str);
	}
	public static void getFirstIndex(String s) {
		String[] a = s.split("");
		for(int i=0;i<a.length;i++) {
			int num = 0;
			int index = 0;
			while(index<=i) {
				if(a[index].equals(a[i])) {
					num++;
				}
				index++;
			}
			if(num>1) {
				System.out.println(a[i]);
				break;
			}
		}
		
	}
	
}

17. Input a positive integer N, decompose it by prime factor and output the result. For example, enter 90 and print out 90 = 2 * 3 * 3 * 5

view code
package java_works;

import java.util.Scanner;

public class java_10017 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = -1;
		N = sc.nextInt();
		Solve(N);
	}
	public static void Solve(int a) {
		System.out.print(a+"=");
		for(int i=2;i<a+1;i++) {
			while(a%i==0&&a!=i) {
				a/=i;
				System.out.print(i+"*");
			}
			if(a==i) {
				System.out.println(i);
				break;
			}
		}
	}
}

18. Enter a positive integer N and output the "completion" between [1, N]. "Perfect number" means that if the number is exactly equal to the sum of its factors, the number is called "perfect number", for example, 6 = 1 + 2 + 3

view code
package java_works;

import java.util.Scanner;

public class java_10018 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = -1;
		N = sc.nextInt();
		Solve(N);
	}
	public static void Solve(int a) {
		
		for(int i=1;i<a;i++)
		{
			int sum = 0;
			for(int j=1;j<=i/2;j++)
			{
				if(i % j == 0)
				{
					sum+=j;
				}
			}
			if(sum == i)
			{
				System.out.print(i+" ");
			}
			
		}
		
	}
}

19. Input a line of string, compress according to the specification, and output the compressed string. Compression specification: if the same character is continuous, it will be compressed into "character + number of digits", for example, "iamaaaaabccdd" will be compressed into "iama5AbcC2dD".

view code
package java_works;

import java.util.Scanner;

public class java_10019 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		compressStr(str);
	}
	public static void compressStr(String s) {
		StringBuilder sb = new StringBuilder();
		int count = 1;
		char c1=s.charAt(0);
		for(int i=1;i<s.length();i++)
		{
			char c2 = s.charAt(i);
			if(c1 == c2) {
				count++;
				continue;
			}
			if(count>1) {
				sb.append(c1).append(count);
			}else {
				sb.append(c1);
			}
			c1 = c2;
			count = 1;
		}
		if(count >1) {
			sb.append(c1).append(count);
		}else {
			sb.append(c1);
		}
		System.out.println(sb.toString());
	}
}

20. Split instruction. Enter a positive integer N, and then enter N lines of strings. Each line of string is an instruction.

view code
package java_works;

import java.util.Scanner;

public class java_10020 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = -1;
		N = Integer.parseInt(sc.nextLine());
		String[] a = new String[N];
		for(int i=0;i<N;i++)
		{
			a[i] = sc.nextLine().replace(" ","");
		}
		sc.close();
		String[] singleArr = {"printi","jg","jl","je","jne","jmp"};
		String[] doubleArr = {"mov","add","sub","mul","div","cmp"};
		for(int i=0;i<N;i++) {
			int flag = 0;
			for(String tmp:singleArr)
			{
				if(a[i].indexOf(":"+tmp)>0)
				{
					flag = 1;
					String[] tmpStrArr = a[i].split(":"+tmp);
					System.out.println("LineNo="+tmpStrArr[0]);
					System.out.println("OP="+tmp);
					System.out.println("N1="+tmpStrArr[1]);
				}
			}
			if(flag == 0){
				for(String tmp:doubleArr)
				{
					if(a[i].indexOf(":"+tmp)>0) {
						flag = 2;
						String[] tmpStrArr = a[i].split(":"+tmp);
						System.out.println("LineNo="+tmpStrArr[0]);
						System.out.println("OP="+tmp);
						System.out.println("N1="+tmpStrArr[1].split(",")[0]);
						System.out.println("N2="+tmpStrArr[1].split(",")[1]);
					}
				}
			}
			if(flag == 0)
				return;
		}
	}

}

21. Binary instruction sequence is decoded into string source code. Enter a line of string, which is the hexadecimal form string of binary instruction sequence

view code
import java.util.Scanner;

public class java_10021 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String inputNum = sc.nextLine();
		String[][] a = {
				{"mov","0"},
				{"mov","1"},
				{"add","0"},
				{"add","1"},
				{"sub","0"},
				{"sub","1"},
				{"mul","0"},
				{"mul","1"},
				{"div","0"},
				{"div","1"},
				{"cmp","0"},
				{"cmp","1"},
				{"je","0"},
				{"je","1"},
				{"jne","0"},
				{"jne","1"},
				{"jl","0"},
				{"jl","1"},
				{"jg","0"},
				{"jg","1"},
				{"jmp","0"},
				{"jmp","1"},
				{"printi","0"},
				{"printi","1"},
		};
		
	}

}

22. Sorry, I forgot to write it and forgot the title. It's just rotten...

23. Input the positive integer n, and then input N*N positive integers, which are arranged into a matrix of N rows and N columns in the order of row by row. It is required to flip the matrix in the vertical direction and output the results

view code
import java.util.Scanner;

public class java_10023 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[][] a = new int[N][N];
		for(int i = 0;i < N; i++)
			for(int j = 0;j < N;j++)
			{
				a[i][j] = sc.nextInt();
			}
 		//Realize vertical flip matrix
		for(int i = N - 1;i > -1;i--)
 		{
 			for(int j = 0;j < N;j++)
 			{
 				System.out.print(a[i][j]+" ");
 			}
 			System.out.println();
 		}
 			
	}

}

24. There are 1, 2,... On the old mobile phone health disk, 8, 9, 0 keys, where each key represents multiple letters and 0 represents a space. Input a line of string containing only letters and spaces, convert the string into the serial number of mobile phone keyboard keys, and output the result

view code
import java.util.Scanner;

public class java_10024 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String sentence = sc.nextLine();
		char[] a = sentence.toCharArray();
		for(int i = 0;i < a.length;i++)
		{
			if(a[i] >= 97 && a[i] <= 99)
				a[i] = '2';
			else if(a[i] >= 100 && a[i] <= 102)
				a[i] = '3';
			else if(a[i] >= 103 && a[i] <= 105)
				a[i] = '4';
			else if(a[i] >= 106 && a[i] <= 108)
				a[i] = '5';
			else if(a[i] >= 109 && a[i] <= 111)
				a[i] = '6';
			else if(a[i] >= 112 && a[i] <= 115)
				a[i] = '7';
			else if(a[i] >= 116 && a[i] <= 118)
				a[i] = '8';
			else if(a[i] >= 119 && a[i] <= 122)
				a[i] = '9';
			else if(a[i] == 32)
				a[i] = '0';
			System.out.print(a[i]);
		}
	}

}

25. Enter a positive integer N, and then enter a number K between [2, 16]. Please convert N into K-ary number and output the result

view code
import java.util.Scanner;

public class java_10025 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		if(K < 2 || K > 16)
			return;
		//Realize the mutual conversion between arbitrary hexadecimals
		String s = Integer.toString(N,K);
		System.out.println(s.toUpperCase());
	}

}

26. Establish the mapping between city name and main key and telephone number area code with hash table,

view code
import java.util.HashMap;

import java.util.Scanner;

public class java_10026 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		HashMap<String,String> code = new HashMap<String,String>();
		Scanner sc = new Scanner(System.in);
		int N = Integer.parseInt(sc.nextLine());
		//Map the city name primary key and telephone number area code and store them in the hash table
		for(int i = 0;i < N;i++)
		{
			String tempStr = sc.nextLine();
			String tempStrArr[] = tempStr.split(" ");
			code.put(tempStrArr[0], tempStrArr[1]);
		}
		int IN = Integer.parseInt(sc.nextLine());
		String[] cityKey = new String[IN];
		for(int i = 0;i < IN;i++)
		{
			String tempStr = sc.nextLine();
			cityKey[i] = tempStr;
		}
		sc.close();
		for(int i = 0;i < IN;i++)
		{
			System.out.println(cityKey[i] + " " + code.get(cityKey[i]));
		}
	}

}

27. Please define a class Student to represent students and realize the corresponding functions

view code
import java.util.*;

import java.io.*;

public class java_10027 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		class Student{
			String Name;//full name
			Double NGrade;//Usual performance
			Double EGrade;//Examination results
			public Student(String Name,Double NGrade,Double EGrade){
				super();
				this.Name = Name;
				this.NGrade = NGrade;
				this.EGrade = EGrade;
			}
			
			public Student() {
				// TODO Auto-generated constructor stub
			}

			public String getName() {
				return this.Name;
			}
			public void setName(String Name) {
				this.Name = Name;
			}
			public Double getNGrade() {
				return NGrade;
			}
			public void setNGrade(Double NGrade) {
				this.NGrade = NGrade;
			}
			public double sumGrade(Student s) {
				return this.NGrade * 0.3 + this.EGrade * 0.7;
			}
			public boolean CompareTS(Student s) {
				 Double in = this.sumGrade();
			        Double out = s.sumGrade();
			        if (in > out)
			            return true;
			        else if (in == out)
			            return true;
			        else
			            return false;
			}
			private Double sumGrade() {
				// TODO Auto-generated method stub
				return null;
			}

			public void ToString(Student s) {
				System.out.printf("full name:"+s.Name+"\n Usual performance:"+s.NGrade+"\n Examination results:"+s.EGrade+"\n Total score:"+sumGrade(s)+"\n");
			}
		}
		
	}

}

28. Alas, the title is not written again...

view code
import java.io.*;

import java.util.*;

class Complex{
	double real;
	double image;
	public Complex(double real,double image) {
		this.real = real;
		this.image = image;
	}
	public Complex(Complex c) {
		this.real = c.real;
		this.image = c.image;
	}
	public double getReal() {
		return real;
	}
	public double getImage() {
		return image;
	}
	public void setReal(double real) {
		this.real = real;
	}
	public void setImage(double image) {
		this.image = image;
	}
	public String toString() {
		return "(" + String.format("%.5f", real) + ")" + "+" + "(" + String.format("%.5f", image) + ")" + "i";
	}
	public Complex add(Complex c) {
		return new Complex( this.real + c.real, this.image + c.image );
	}
	public Complex sub(Complex c) {
		return new Complex( this.real - c.real, this.image - c.image );
	}
	public Complex multiply( Complex C ) {
		double newa = this.real * C.real - this.image * C.image; // Create a new real part
		double newb = this.real * C.image + this.image * C.real; // Create a new imaginary part
		return new Complex( newa, newb );
	}
	public Complex exp( ) {
		return new Complex( Math.exp(this.real) * Math.cos(this.image),
		Math.exp(this.real) * Math.sin(this.image) );
	}
	public double abs( ) {
		return Math.sqrt( this.real * this.real + this.image * this.image );
	} 
}

29. Rectangle entity class

view code
import java.io.*;

import java.util.*;

class Rect {
	Double x;//X coordinate
	Double y;//Y coordinate
	Double L;//Rectangle length
	Double W;//Rectangle width
	public Rect(Double x,Double y,Double l,Double w) {
		this.x = x;
		this.y = y;
		this.L = l;
		this.W = w;
	}
	public Rect(Rect r) {
		// TODO Auto-generated constructor stub
		this.x = r.x;
		this.y = r.y;
		this.L = r.L;
		this.W = r.W;
	}
	public Double getX() {
		return this.x;
	}
	public void setX(Double x) {
		this.x = x;
	}
	public Double getY() {
		return this.y;
	}
	public void setY(Double y) {
		this.y = y;
	}
	public Double getL() {
		return this.L;
	}
	public void setL(Double l) {
		this.L = l;
	}
	public Double getW() {
		return this.W;
	}
	public void setW(Double w) {
		this.W = w;
	}
	public Double Area() {
		return L * W;
	}
	public int CompareArea(Rect r) {
		Double a = this.Area();
		Double b = r.Area();
		if(a > b)
			return 1;
		else if(a == b)
			return 0;
		else
			return -1;
	}
	public String  toString() {
		return (String.format("x:%.5f",x) +"\n" + String.format("y:%.5f", y) + "\n" + String.format("L:%.5f", L)+ "\n" + String.format("W:%.5f", W) +"\n" + String.format("the measure of area:%.5f", this.Area()));
	}
}

30. Please define a class Circle to represent a Circle. It is required to realize corresponding functions

view code
public class java_10030 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/* The constructor parameters are in order: x, y, r */
		Circle rc1 = new Circle(10.01, 20.0, 30.0);
		Circle rc2 = new Circle(rc1);

		System.out.println(String.valueOf(rc1.getX()));
		System.out.println(String.valueOf(rc1.getY()));
		System.out.println(String.valueOf(rc1.getR()));

		rc2.setX(40.66);
		rc2.setY(58.18);
		rc2.setR(28.98);

		/* Compare the area. If the rc2 area is larger, output 1, equal output 0, otherwise output - 1; */
		System.out.println(String.valueOf(rc2.CompareCS(rc1)));
		
		/* Compare the perimeter. If rc2 perimeter is longer, output 1, equal output 0, otherwise output - 1; */
		System.out.println(String.valueOf(rc2.CompareCL(rc1)));
		System.out.println(String.valueOf(rc2.toString()));
	}

}

31,. . .

import java.io.*;

import java.util.*;

class CUser {
	String userName;
	String passWord;
	public CUser(String u,String p) {
		this.userName = u;
		this.passWord = p;
	}
	public String getUserName() {
		return this.userName;
	}
	public void setUserName(String u) {
		this.userName = u;
	}
	public String getPassWord() {
		return this.passWord;
	}
	public void setPassWord(String p) {
		this.passWord = p;
	}
	public String Login(String InUserName,String InPassWord) {
		if(userName.equals(InUserName) && passWord.equals(InPassWord))
			return "Login successful";
		else
			return "Login failed";
	}
}

32,. . .

view code
import java.io.*;

import java.util.*;

interface IReadBook {
	void ReadBook();
}
class CBauStu implements IReadBook{
	public void ReadBook() {
		System.out.println("Undergraduate reading materials");
	}
}
class CGduStu implements IReadBook{
	public void ReadBook() {
		System.out.println("Master students read Chinese Academic Journals");
	}
}
class CDocStu implements IReadBook{
	public void ReadBook() {
		System.out.println("Doctoral students read foreign academic journals");
	}
}

33. Look at the code directly

view code
import java.io.*;

import java.util.*;

interface ICry{
	void Cry();
}
class CFrog implements ICry{
	public void Cry() {
		System.out.println("Frogs whoosh");
	}
}
class CDog implements ICry{
	public void Cry() {
		System.out.println("The dog barked");
	}
}
class CCat implements ICry{
	public void Cry() {
		System.out.println("kittens mew ");
	}
}

34,

view code
public class java_10034 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Integer[] li = new Integer[] {100, 20, 32, 196, 85};
		 Integer iv = CommonFun.getMax(li);   //The return iv value is 196  
		 System.out.println(iv);
		  
		 Double[] ld = new Double[] {51.0, 10.6, 165.2, 12.0, 82.0};
		 Double dv = CommonFun.getMax(ld);   //The return dv value is 165.2 
		 System.out.println(dv);
	}

}

35,

import java.util.Scanner;

public class java_10035 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		String[][] Student = new String[N][6];
		for(int i = 0;i < N;i++)
		{
			int sum =0;
			for(int j = 0;j < 4;j++)
			{
				Student[i][j] = sc.next();
			}
			sum = Integer.parseInt(Student[i][1]) + Integer.parseInt(Student[i][2]) + Integer.parseInt(Student[i][3]);
			Student[i][4] = "" + sum;
			Student[i][5] = "" + i;
		}
		//sort
		for(int i = 0;i < N -1;i++)
		{
			for(int j = N -1;j > i;j--)
			{
				if(Integer.parseInt(Student[i][4]) < Integer.parseInt(Student[j][4]))
				{
					String[] tempArr = Student[i];
					Student[i] = Student[j];
					Student[j] = tempArr;
				}else if(Integer.parseInt(Student[i][4]) == Integer.parseInt(Student[j][4]))
				{
					if(Integer.parseInt(Student[i][5]) > Integer.parseInt(Student[j][5]))
					{
						String[] tempArr = Student[i];
						Student[i] = Student[j];
						Student[j] = tempArr;
					}
				}
			}
		}
		for(int i = 0;i < N;i++)
		{
			for(int j = 0;j < 5;j++)
			{
				System.out.print(Student[i][j] + " ");
			}
			System.out.println();
		}
	}
}

36,

import java.util.Scanner;

public class java_10036 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		String[][] Student = new String[N][6];
		for(int i = 0;i < N;i++)
		{
			int sum =0;
			for(int j = 0;j < 4;j++)
			{
				Student[i][j] = sc.next();
			}
			sum = Integer.parseInt(Student[i][1]) + Integer.parseInt(Student[i][2]) + Integer.parseInt(Student[i][3]);
			Student[i][4] = "" + sum;
			Student[i][5] = "" + i;
		}
		//sort
		for(int i = 0;i < N -1;i++)
		{
			for(int j = N -1;j > i;j--)
			{
				if(Integer.parseInt(Student[i][4]) < Integer.parseInt(Student[j][4]))
				{
					String[] tempArr = Student[i];
					Student[i] = Student[j];
					Student[j] = tempArr;
				}else if(Integer.parseInt(Student[i][4]) == Integer.parseInt(Student[j][4]))
				{
					if(Integer.parseInt(Student[i][1]) < Integer.parseInt(Student[j][1]))
					{
						String[] tempArr = Student[i];
						Student[i] = Student[j];
						Student[j] = tempArr;
					}else if(Integer.parseInt(Student[i][1]) == Integer.parseInt(Student[j][1])) {
						if(Integer.parseInt(Student[i][5]) > Integer.parseInt(Student[j][5]))
						{
							String[] tempArr = Student[i];
							Student[i] = Student[j];
							Student[j] = tempArr;
						}
					}
				}
			}
		}
		for(int i = 0;i < N;i++)
		{
			for(int j = 0;j < 5;j++)
			{
				System.out.print(Student[i][j] + " ");
			}
			System.out.println();
		}
	}

}

37,

import java.util.Scanner;

public class java_10037 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		String[][] Student = new String[N][6];
		for(int i = 0;i < N;i++)
		{
			int sum =0;
			for(int j = 0;j < 4;j++)
			{
				Student[i][j] = sc.next();
			}
			sum = Integer.parseInt(Student[i][1]) + Integer.parseInt(Student[i][2]) + Integer.parseInt(Student[i][3]);
			Student[i][4] = "" + sum;
			Student[i][5] = "" + i;
		}
		//sort
		for(int i = 0;i < N -1;i++)
		{
			for(int j = N -1;j > i;j--)
			{
				if(Integer.parseInt(Student[i][4]) < Integer.parseInt(Student[j][4]))
				{
					String[] tempArr = Student[i];
					Student[i] = Student[j];
					Student[j] = tempArr;
				}else if(Integer.parseInt(Student[i][4]) == Integer.parseInt(Student[j][4]))
				{
					if(Integer.parseInt(Student[i][1]) < Integer.parseInt(Student[j][1]))
					{
						String[] tempArr = Student[i];
						Student[i] = Student[j];
						Student[j] = tempArr;
					}else if(Integer.parseInt(Student[i][1]) == Integer.parseInt(Student[j][1])) {
						if(Integer.parseInt(Student[i][2]) < Integer.parseInt(Student[j][2]))
						{
							String[] tempArr = Student[i];
							Student[i] = Student[j];
							Student[j] = tempArr;
						}else if(Integer.parseInt(Student[i][2]) == Integer.parseInt(Student[j][2]))
							if(Integer.parseInt(Student[i][5]) > Integer.parseInt(Student[j][5]))
							{
								String[] tempArr = Student[i];
								Student[i] = Student[j];
								Student[j] = tempArr;
							}
					}
				}
			}
		}
		for(int i = 0;i < N;i++)
		{
			for(int j = 0;j < 5;j++)
			{
				System.out.print(Student[i][j] + " ");
			}
			System.out.println();
		}
	}

}

38,

import java.util.Scanner;

public class java_10038 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		String[][] Student = new String[N][6];
		for(int i = 0;i < N;i++)
		{
			int sum =0;
			for(int j = 0;j < 4;j++)
			{
				Student[i][j] = sc.next();
			}
			sum = Integer.parseInt(Student[i][1]) + Integer.parseInt(Student[i][2]) + Integer.parseInt(Student[i][3]);
			Student[i][4] = "" + sum;
			Student[i][5] = "" + i;
		}
		//sort
		for(int i = 0;i < N -1;i++)
		{
			for(int j = N -1;j > i;j--)
			{
				if(Integer.parseInt(Student[i][4]) < Integer.parseInt(Student[j][4]))
				{
					String[] tempArr = Student[i];
					Student[i] = Student[j];
					Student[j] = tempArr;
				}else if(Integer.parseInt(Student[i][4]) == Integer.parseInt(Student[j][4]))
				{
					if(Integer.parseInt(Student[i][1]) < Integer.parseInt(Student[j][1]))
					{
						String[] tempArr = Student[i];
						Student[i] = Student[j];
						Student[j] = tempArr;
					}else if(Integer.parseInt(Student[i][1]) == Integer.parseInt(Student[j][1])) {
						if(Integer.parseInt(Student[i][2]) < Integer.parseInt(Student[j][2]))
						{
							String[] tempArr = Student[i];
							Student[i] = Student[j];
							Student[j] = tempArr;
						}else if(Integer.parseInt(Student[i][2]) == Integer.parseInt(Student[j][2]))
							if(Student[i][0].compareTo(Student[j][0]) > 0)
							{
								String[] tempArr = Student[i];
								Student[i] = Student[j];
								Student[j] = tempArr;
							}
					}
				}
			}
		}
		for(int i = 0;i < N;i++)
		{
			for(int j = 0;j < 5;j++)
			{
				System.out.print(Student[i][j] + " ");
			}
			System.out.println();
		}
	}

}

39,

import java.io.*;

import java.util.Scanner;

public class java_10039 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		int flag = -1 , i = 0;
		char[] ch = str.toCharArray();
		while(i < str.length()/2)
		{
			if(ch[i] != ch[str.length() -1 -i])
			{
				flag = 0;
				break;
			}
			i++;
		}
		if(flag ==-1)
		{
			flag = 1;
		}
		System.out.println(flag);
	}

}

40,

import java.util.Scanner;

public class java_10040 {	
	public String longStr(String s)
	{
		if(s.length() < 2)
			return s;
		int max_len = 1;
		int start = 0;
		//dp[i][j] indicates whether s[i][j] is a palindrome string
		boolean[][] dp = new boolean[s.length()][s.length()];
		//Initialization, all strings with length of 1 are palindromes
		for(int i = 0;i < s.length();i++)
		{
			dp[i][i] = true;
		}
		char[] charArr = s.toCharArray();
		//Recurrence start
		//Enumerate the string length first
		for(int l = 2;l <= s.length();l++)
		{
			//Enumeration left boundary
			for(int i = 0;i < s.length();i++)
			{
				int j = l + i - 1;
				//If the right side is out of bounds, exit the loop
				if(j >= s.length())
					break;
				if(charArr[i] != charArr[j]) {
					dp[i][j] = false;
				}
				else {
					if(j - i < 3) {
						dp[i][j] = true;
					}
					else {
						dp[i][j] = dp[i + 1][j - 1];
					}
				}
				//As long as dp[j][k] == true, it means that the substring s[i][j] is a palindrome
				if(dp[i][j] && j - i + 1 > max_len) {
					max_len = j - i + 1;
					start = i;
				}
			}
		}
		return s.substring(start,start + max_len);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		java_10040 solution = new java_10040();
		System.out.println(solution.longStr(str));
	}
}

41. Input positive integers N and K, and then input N floating-point numbers. It is required to output the number with the largest K among them

view code
import java.util.Scanner;

public class java_10041 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		Double Temp;
		Double[] Arr = new Double[N];
		for(int i = 0; i < Arr.length;i++)
		{
			Arr[i] = sc.nextDouble();
		}
		//The bubble sorting method is used to sort the array
		for(int i = 0;i < Arr.length - 1 ;i++)
		{
			for(int j = 0;j <Arr.length - i - 1;j++)
			{
				if(Arr[j] < Arr[j + 1])
				{
					Temp = Arr[j];
					Arr[j] = Arr[j + 1];
					Arr[j + 1] = Temp;
				}
			}
		}
		System.out.printf("%.5f",Arr[K-1]);
	}

}

42. Input positive integer n, then input vector A composed of N floating-point numbers, and then input vector b composed of N floating-point numbers. It is required to calculate and output A+B.

view code
import java.util.Scanner;

public class java_10042 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		Double[] A = new Double[N];
		Double[] B = new Double[N];
		for(int i = 0; i < N;i++)
		{
			A[i] = sc.nextDouble();
		}
		for(int i = 0; i < N;i++)
		{
			B[i] = A[i] + sc.nextDouble();
		}
		for(int i = 0;i < N;i++)
		{
			System.out.print(String.format("%.5f" + " ", B[i]));
		}
	}

}

43. Input positive integer n, then input vector A composed of N floating-point numbers, and then input vector B composed of N floating-point numbers. It is required to calculate and output A-B.

view code
import java.util.Scanner;

public class java_10043 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		Double[] A = new Double[N];
		Double[] B = new Double[N];
		for(int i = 0; i < N;i++)
		{
			A[i] = sc.nextDouble();
		}
		for(int i = 0; i < N;i++)
		{
			B[i] = A[i] - sc.nextDouble();
		}
		for(int i = 0;i < N;i++)
		{
			System.out.print(String.format("%.5f" + " ", B[i]));
		}
	}

}

44. Input positive integer n, then input vector A composed of N floating-point numbers, and then input vector b composed of N floating-point numbers. It is required to calculate and output the Euclidean distance between vectors A and B

view code
import java.util.Scanner;

public class java_10044 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		Double[] A = new Double[N];
		Double[] B = new Double[N];
		Double sum = 0.0;
		for(int i = 0;i < N;i++)
		{
			A[i] = sc.nextDouble();
		}
		for(int i = 0;i < N;i++)
		{
			 B[i]= sc.nextDouble();
			sum += Math.pow(A[i] - B[i] , 2);
		}
		sum = Math.sqrt(sum);
		System.out.println(String.format("%.5f",sum));
	}

}

45. Input positive integers n and K, then input A vector A composed of N floating-point numbers, and then input K vectors with the same dimension. It is required to find the vector with the smallest Euclidean distance from vector A among the K vectors, and output the Euclidean distance between the found vector and vector A

view code
import java.util.Scanner;

public class java_10045 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		Double[] A = new Double[N];
		
		for(int i = 0;i < A.length;i++)
		{
			A[i] = sc.nextDouble();
		}
		Double[][] B = new Double[K][N];
		for(int i = 0;i < B.length;i++)
		{
			for(int j = 0;j < B[i].length;j++)
			{
				B[i][j] = sc.nextDouble();
			}
		}
		//Find Euclidean distance
		
		Double[] V = new Double[K];
		for(int i = 0;i < K;i++)
		{
			double value = 0;
			for(int j = 0;j < N;j++)
			{
				value += Math.pow(A[j] - B[i][j],2);
			}
			V[i] = Math.sqrt(value);
		}
		//Find the vector with the smallest Euclidean distance from vector A
		double minValue = V[0];
		for(int i = 1 ;i < V.length;i++)
		{
			if(V[i] < minValue)
				minValue = V[i];
		}
		System.out.printf("%.5f",minValue);
	}
}

46. Enter positive integers n and K, and then enter K vectors, where each vector is composed of N floating-point numbers. Now it is required to find the two vectors with the smallest Euclidean distance among the K vectors, and output the Euclidean distance between the two vectors

view code
import java.util.Scanner;

public class java_10046 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		double sum = 0.0;
		double minSum = Double.MAX_VALUE;
		int minI = -1;
		int minJ = -1;
		Double[][] data= new Double[K][N];
		for(int i = 0;i < K;i++)
		{
			for(int j = 0 ;j < N;j++)
			{
				data[i][j] = sc.nextDouble();
			}
		}
		for(int i = 0 ;i < K;i++)
		{
			for(int j = i + 1;j < K;j++)
			{
				sum = 0.0;
				for(int m = 0;m < N;m++)
				{
					sum = sum + Math.pow(data[i][m] - data[j][m],2);
				}
				sum = Math.sqrt(sum);
				if(sum < minSum)
				{
					minSum = sum;
					minI =i;
					minJ = j;
				}
			}
		}
		System.out.format("%.5f",minSum);
	}
}

47,. . .

48,. . .

import java.util.Scanner;

public class java_10048 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		Double[] M = new Double[N];
		Double[] A = new Double[N];
		Double[] B = new Double[N];
		Double[] R = new Double[N];
		for(int i = 0;i < N;i++)
		{
			M[i] = sc.nextDouble();
		}
		for(int i = 0;i < N;i++)
		{
			A[i] = sc.nextDouble();
		}
		for(int i = 0;i < N;i++)
		{
			B[i] = sc.nextDouble();
		}
		for(int i = 0;i < N;i++)
		{
			if(M[i] < 0.5)
			{
				R[i] = A[i];
			}
			else
			{
				R[i] = B[i];
			}
		}
		for(int i = 0;i < N;i++)
		{
			System.out.format("%.5f" + " ", R[i]);
		}
	}

}

49,. . .

import java.util.Scanner;
	
	public class java_10049 {
	
		public static void main(String[] args) {
			// TODO Auto-generated method stub
			Scanner sc = new Scanner(System.in);
			int N1 = sc.nextInt();
			int C1 = sc.nextInt();
			int N2 = sc.nextInt();
			int C2 = sc.nextInt();
			Double[][] A = new Double[N1][C1];
			Double[][] B = new Double[N2][C2];
			for(int i = 0;i < N1;i++)
			{
				for(int j = 0;j < C1;j++)
				{
					A[i][j] = sc.nextDouble();
				}
			}
			for(int i = 0;i < N2;i++)
			{
				for(int j = 0;j < C2;j++)
				{
					B[i][j] = sc.nextDouble();
				}
			}
			for(int i = 0;i < N1;i++)
			{
				for(int j = 0;j < C2;j++)
				{
					Double sum = 0.0;
					for(int k = 0;k < C1;k++)
					{
						sum = sum + A[i][k]*B[k][j];
					}
					System.out.format("%.5f" + " ", sum);
				}
				System.out.println();
			}
		}
	
	}

50, 51, not written ha ha ha

52,. . .

import java.util.Scanner;

public class java_10052 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int C = sc.nextInt();
		Double[][] M = new Double[N][C];
		Double[][] A = new Double[N][C];
		Double[][] B = new Double[N][C];
		Double[][] R = new Double[N][C];
		for(int i = 0;i < N;i++)
		{
			for(int j = 0;j < C;j++)
			{
				M[i][j] = sc.nextDouble();
			}
		}
		for(int i = 0;i < N;i++)
		{
			for(int j = 0;j < C;j++)
			{
				A[i][j] = sc.nextDouble();
			}
		}
		for(int i = 0;i < N;i++)
		{
			for(int j = 0;j < C;j++)
			{
				B[i][j] = sc.nextDouble();
			}
		}
		
		for(int i = 0;i < N;i++)
		{
			for(int j = 0;j < C;j++)
			{
				if(M[i][j] < 0.5)
				{
					R[i][j] = A[i][j];
				}
				else
				{
					R[i][j] = B[i][j];
				}
			}
		}
		for(int i = 0;i < N;i++)
		{
			for(int j = 0;j < C;j++)
			{
				System.out.format("%.5f" + " ", R[i][j]);
			}
			System.out.println();
		}
	}

}

53,. .

import java.util.Scanner;

public class java_10053 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[][] data = new int[N][N];
		for(int i = 0;i < N;i++)
		{
			for(int j = 0;j < N;j++)
			{
				data[i][j] = sc.nextInt();
			}
		}
		for(int i = 0;i < N;i++)
		{
			for(int j = N - 1;j > -1;j--)
			{
				System.out.print(data[i][j] + " ");
			}
			System.out.println();
		}
	}

}

54,. . .

import java.util.Scanner;

public class java_10054 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[][] data = new int[N][N];
		for(int i = 0;i < N;i++)
		{
			for(int j = 0;j < N;j++)
			{
				data[i][j] = sc.nextInt();
			}
		}
		for(int i = N - 1;i > -1;i--)
		{
			for(int j = N - 1;j > -1;j--)
			{
				System.out.print(data[j][i] + " ");
			}
			System.out.println();
		}
	}

}

55,. . .

import java.util.Scanner;

public class java_10055 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[][] data = new int[N][N];
		for(int i = 0;i < N;i++)
		{
			for(int j = 0;j < N;j++)
			{
				data[i][j] = sc.nextInt();
			}
		}
		for(int i = 0;i < N;i++)
		{
			for(int j = N - 1;j > -1;j--)
			{
				System.out.print(data[j][i] + " ");
			}
			System.out.println();
		}
	}

}

56,

import java.util.Scanner;

public class java_10055 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[][] data = new int[N][N];
		for(int i = 0;i < N;i++)
		{
			for(int j = 0;j < N;j++)
			{
				data[i][j] = sc.nextInt();
			}
		}
		for(int i = 0;i < N;i++)
		{
			for(int j = N - 1;j > -1;j--)
			{
				System.out.print(data[j][i] + " ");
			}
			System.out.println();
		}
	}

}

57. Forgot to write OvO

58,

import java.util.Scanner;

public class java_10058 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int count = 0;
		String str = sc.nextLine();
		for(int i = 0;i < str.length();i++)
		{
			if(count < 0)
				break;
			String temp = str.substring(i,i + 1);
			if(temp.equals("("))
				count ++;
			if(temp.equals(")"))
				count --;
		}
		if(count == 0)
			System.out.println(1);
		else
			System.out.println(0);
	}

}

59,

import java.util.Scanner;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class java_10059 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		@SuppressWarnings("resource")
		Scanner sc=new Scanner(System.in);
		String str=sc.nextLine();
		BigDecimal result=cal(str);
		System.out.format("%.5f\n",result);
	}
	public static BigDecimal cal(String str)
	{
		List<String> list=new ArrayList<>();
		char[] arr=str.toCharArray();
		StringBuffer sb=new StringBuffer();
		for(char c:arr)
		{
			if(c>='0'&&c<='9') {
				sb.append(c);
			}else if(c=='.')
			{
				if(sb.indexOf(".")>0)
				{
					throw new RuntimeException("Illegal character");
				}
				sb.append(c);
			}
			else if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')') {
				if(sb.length()>0)
				{
					list.add(sb.toString());
					sb.setLength(0);
				}
				list.add(c+"");
			}
			else if(c==' ')
			{
				continue;
			}else {
				throw new RuntimeException("Illegal character");
			}
		}
		if(sb.length()>0)
		{
			list.add(sb.toString());
		}
		List<String> strList =new ArrayList<>();
		Stack<String> stack=new Stack<>();
		String temp;
		for(String s:list)
		{
			if(s.equals("("))
			{
				stack.push(s);
			}
			else if(s.equals(")")) {
				while(!(temp=stack.pop()).equals("("))
				{
					strList.add(temp);
				}
			}
			else if (s.equals("*")||s.equals("/")) {
				while(!stack.isEmpty()) {
					temp=stack.peek();
					if(temp.equals("*")||temp.equals("/"))
					{
						stack.pop();
						strList.add(temp);
					}else {
						break;
					}
				}
				stack.push(s);
			}else if(s.equals("+")||s.equals("-"))
			{
				while(!stack.isEmpty()) {
					temp=stack.peek();
					if(!temp.equals("("))
					{
						stack.pop();
						strList.add(temp);
					}else {
						break;
					}
				}
				stack.push(s);
			}
			else {
				strList.add(s);
			}
		}
		while(!stack.isEmpty()) {
			strList.add(stack.pop());
		}
		Stack<BigDecimal>newStack=new Stack<>();
		for(String s:strList)
		{
			if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/")) {
				BigDecimal b1=newStack.pop();
				BigDecimal b2=newStack.pop();
				switch(s){
					case "+":
					    newStack.push(b2.add(b1));
					    break;
					case "-":
						newStack.push(b2.subtract(b1));
						break;
					case "*":
						newStack.push(b2.multiply(b1));
						break;
					case "/":
						newStack.push(b2.divide(b1,9,BigDecimal.ROUND_HALF_UP));
						break;
				}
			}
			else {
				newStack.push(new BigDecimal(s));
			}
		}
		return newStack.peek();
	}

}

60,

import java.util.Scanner;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class java_10060{
    public static void main(String[] args) throws Exception{
    	@SuppressWarnings("resource")
		Scanner sc=new Scanner(System.in);
		String str=sc.nextLine().replace(" ", "");
		BigDecimal result=cal(str);
		System.out.format("%.5f\n",result);
	}
	public static BigDecimal cal(String str)
	{
		List<String> list=new ArrayList<>();
		char[] arr=str.toCharArray();
		StringBuffer sb=new StringBuffer();
		for(char c:arr)
		{
			if(c>='0'&&c<='9') {
				sb.append(c);
			}else if(c=='.')
			{
				if(sb.indexOf(".")>0)
				{
					throw new RuntimeException("Illegal character");
				}
				sb.append(c);
			}
			else if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')') {
				if(sb.length()>0)
				{
					list.add(sb.toString());
					sb.setLength(0);
				}
				list.add(c+"");
			}
			else if(c==' ')
			{
				continue;
			}else {
				throw new RuntimeException("Illegal character");
			}
		}
		if(sb.length()>0)
		{
			list.add(sb.toString());
		}
		List<String> strList =new ArrayList<>();
		Stack<String> stack=new Stack<>();
		String temp;
		for(String s:list)
		{
			if(s.equals("("))
			{
				stack.push(s);
			}
			else if(s.equals(")")) {
				while(!(temp=stack.pop()).equals("("))
				{
					strList.add(temp);
				}
			}
			else if (s.equals("*")||s.equals("/")) {
				while(!stack.isEmpty()) {
					temp=stack.peek();
					if(temp.equals("*")||temp.equals("/"))
					{
						stack.pop();
						strList.add(temp);
					}else {
						break;
					}
				}
				stack.push(s);
			}else if(s.equals("+")||s.equals("-"))
			{
				while(!stack.isEmpty()) {
					temp=stack.peek();
					if(!temp.equals("("))
					{
						stack.pop();
						strList.add(temp);
					}else {
						break;
					}
				}
				stack.push(s);
			}
			else {
				strList.add(s);
			}
		}
		while(!stack.isEmpty()) {
			strList.add(stack.pop());
		}
		Stack<BigDecimal>newStack=new Stack<>();
		for(String s:strList)
		{
			if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/")) {
				BigDecimal b1=newStack.pop();
				BigDecimal b2=newStack.pop();
				switch(s){
					case "+":
					    newStack.push(b2.add(b1));
					    break;
					case "-":
						newStack.push(b2.subtract(b1));
						break;
					case "*":
						newStack.push(b2.multiply(b1));
						break;
					case "/":
						newStack.push(b2.divide(b1,9,BigDecimal.ROUND_HALF_UP));
						break;
				}
			}
			else {
				newStack.push(new BigDecimal(s));
			}
		}
		return newStack.peek();
    }
}

 

Tags: Java

Posted by lyasian on Wed, 11 May 2022 04:32:47 +0300