7-2 enter a character string in reverse order, and output the character string in reverse order. Knowledge Point: stringbuffer // modify content stringbuffer sb = new stringbuffer (STR) // Initialization
Code snippet:
import java.util.Scanner;public class Main{ public static void main (String [] args){ Scanner n=new Scanner (System.in); String str=n.nextLine(); StringBuffer sb =new StringBuffer(str); System.out.println(sb.reverse().toString()); }}
7-5 Simplified insertion sorting this question requires programming to insert a given integer into the originally ordered integer sequence so that the result sequence is still ordered. Knowledge Point: arrays. Sort (a) // sort
Code snippet:
import java.util.*;public class Main{public static void main(String[] args){Scanner read=new Scanner(System.in);int n=read.nextInt();int a[]=new int[n+1];int i;for(i=0;i<n;i++){a[i]=read.nextInt();}a[n]=read.nextInt();Arrays.sort(a);for(i=0;i<=n;i++){System.out.print(a[i]+" ");}}}
7-6 swap minimum and maximum values
This question requires programming. First, the minimum value of the input series of integers is exchanged with the first number, then the maximum value is exchanged with the last number, and finally the sequence after the exchange is output. Note: ensure that the maximum and minimum values are unique. Knowledge Point: Exchange max = (a [I]> A [Max])? I: Max; // when the condition is set, max = I; if it is not set, it is equal to the following.
Min = (a [I] <A [Min])? I: min; // when the condition is set, min = I; if it is not set, it is equal to the following.
Code snippet:
import java.util.*;public class Main{public static void main(String[] args){Scanner read=new Scanner(System.in);int N=read.nextInt();int a[]=new int[N];int i;for(i=0;i<N;i++){a[i]=read.nextInt();}int max=0;int min=0;for(i=1;i<N;i++){ max=(a[i]>a[max])?i:max;min=(a[i]<a[min])?i:min;} int c;c=a[0];a[0]=a[min];a[min]=c;if(max==0){max=min;}c=a[N-1];a[N-1]=a[max];a[max]=c;for(i=0;i<N;i++){System.out.print(a[i]+" ");}}}
A 7-8 IP address is converted into a four-byte binary code. Convert the 32-bit binary IP address to the IP address output in decimal format. Knowledge Point: integer. parseint // L type conversion
Code snippet:
import java.util.Scanner;public class Main{public static void main(String[] args){Scanner read=new Scanner(System.in);String n=read.nextLine();String a=n.substring(0,8);String b=n.substring(8,16);String c=n.substring(16,24);String d=n.substring(24,32);int a1=Integer.parseInt(a,2);int b1=Integer.parseInt(b,2);int c1=Integer.parseInt(c,2);int d1=Integer.parseInt(d,2);System.out.print(a1+"."+b1+"."+c1+"."+d1);}}
Code address: https://gitee.com/wxl19981225/16012101_1/tree/master
Third Procedural assessment