Java basic programming job 2

1 - simple encryption system based on ASCII code

[problem description] implement a simple encryption system based on ASCII code.

The encryption rules are as follows:

if (OriginalChar + Key > 126) then

       EncryptedChar = ((OriginalChar + Key)-127) + 32

else

       EncryptedChar = (OriginalChar + Key)

The qualified key is a number between 1 and 100. The original message consists entirely of ASCII code,
Write encryption and decryption function to realize the encryption system. Input the key and a line of plaintext, and output the ciphertext; Then decrypt the ciphertext and output plaintext.
Prompt: string The charat () method can be used to get a character in a string
String. The length () method returns the length of the string (number of characters)
Scanner. The nextline () method can enter a line of characters from the keyboard

import java.util.Scanner;

public class zuoye02_1 {
    public static void main(String[] args) {
        int size=100;
        char OriginalChar = 0;
        char EncryptedChar1[]=new char[15] ;
        char EncryptedChar2[] =new char[15];
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter a message for encrypt: ");
        String arr1;
        arr1=sc.nextLine();
        System.out.print("Enter a key between 1 to 100: ");
        int Key=sc.nextInt();
        for(int i=0;i<arr1.length();i++){
            OriginalChar=arr1.charAt(i);
            if (OriginalChar+Key>126)
                EncryptedChar1[i] = (char) (((OriginalChar+Key)-127) + 32);
            else
                EncryptedChar1[i] = (char) (OriginalChar + Key);

        }
        String arr2= String.valueOf(EncryptedChar1); //Convert char variable to string
        System.out.println("message: "+arr1);
        System.out.println("result: "+arr2);
        for(int i=0;i<arr2.length();i++){
            OriginalChar=arr2.charAt(i);
            if(OriginalChar-Key<32){
                EncryptedChar2[i]=(char) (((OriginalChar-Key)+127) - 32);
            }else{
                EncryptedChar2[i]=(char) (OriginalChar - Key);
            }
        }
        String arr3=String.valueOf(EncryptedChar2);
        System.out.println("result: "+arr2);
        System.out.println("message: "+arr3);
    }
}

2-palindrome number

[problem description] the so-called "palindrome number" refers to an integer with the following properties: when its digits are arranged in reverse order, the integer formed is the same as the original integer. Such a number is called palindrome number. For example, the prime number 11373 is still 11373 after the position of each digit is changed, so these two integers are palindromes. Write the function int loop(int x) to judge whether an integer is a palindrome number. If x is a palindrome number, it returns 1, otherwise it returns 0. Write the program loop c. Receive the two integers a and B entered by the console. Call the loop function to output the number of all palindromes between a and B (including a and b)
[input form] the console inputs two integers a and B (there must be a < b), separated by spaces.
[output form] the output has several lines, and each line has a palindrome number between a and b. The numbers on the output lines are not repeated, and are output in order from small to large.
[sample input] 3 120
[sample output]
3
4
5
6
7
8
9
11
22
33
44
55
66
77
88
99
101
111

import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
public class zuoye02_2 {
    public static void main(String[] args) {

        Scanner sc =new Scanner(System.in);

        int a = sc.nextInt();

        int b = sc.nextInt();

        List<Integer> array =new ArrayList<>();

        array.addAll(loop(a,b));

        print(array);

    }

    public static List<Integer> loop(int x, int y){

//Judge x and y who is big and who is small

        if (x > y){

            int temp = x;

            x = y;

            y = temp;

        }

        List array =new ArrayList<>();

        for (int i = x;i <= y;i++){

            String s = String.valueOf(i);

            //The reverse method of String reverses the character to determine whether it is equal before and after inversion

            String revers =new StringBuffer(s).reverse().toString();

            if (s.equals(revers)){

//Save to List

                array.add(i);

            }

        }

        return array;

    }

//Traverse List

    public static void print(List<Integer> a){

        for (Integer integer : a) {

            System.out.println(integer);

        }

    }
}

3 - tax rate

[problem description]
It is assumed that the pre tax salary and tax rate are as follows (s represents the pre tax salary and t represents the tax rate):
s<1000 t=0%
1000<=s<2000 t=10%
2000<=s<3000 t=15%
3000<=s<4000 t=20%
4000<=s t=25%
Write a program that requires the user to input the pre tax salary, and then calculate the after tax salary with multi branch if statements.
[input form]
Enter the pre tax salary s from the keyboard, which can be a floating point number.
[output form]
Output the after tax salary with two decimal places.
[input example]
3000
[output example]
2400.00

import java.util.Scanner;
public class zuoye02_3 {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        double s;
        double t = 0;
        s = in.nextFloat();

        if(s < 1000){
            System.out.printf("%.2f",s);
        }
        if(s >= 1000 && s < 2000){
            t+=0.10;
            s = s - (s * t);
            System.out.printf("%.2f",s);
        }
        if(s >= 2000 && s < 3000){
            t+=0.15;
            s = s - (s * t);
            System.out.printf("%.2f",s);
        }
        if(s >= 3000 && s < 4000){
            t+=0.20;
            s = s - (s * t);
            System.out.printf("%.2f",s);
        }
        if(s >= 4000){
            t+=0.25;
            s = s - (s * t);
            System.out.printf("%.2f",s);
        }

    }
}

4 - minimum combination of RMB

[problem description]

Enter an integer value of RMB (within 100, in Yuan), and program to find the minimum combination of the total quantity represented by 10 yuan, 5 yuan, 2 yuan and 1 yuan.

[[input form]

Enter an integer value from the console to represent the value of RMB in yuan.

[Output form]

Output four integers (separated by spaces) to the console to represent the quantity of RMB 10, 5, 2 and 1 converted respectively. If there is no currency value, 0 will be output accordingly.

[Example 1 [input]

98

[Example 1 output]

9 1 1 1
import java.util.Scanner;
public class zuoye02_4 {
    public static void main(String[] args) {
        Scanner c=new Scanner(System.in);
        int RMB=c.nextInt();
        int i10=0;int i5=0; int i2=0;int i1=0;
        i10=RMB/10;
        RMB=RMB-10*i10;
        i5=RMB/5;
        RMB=RMB-5*i5;
        i2=RMB/2;
        RMB=RMB-2*i2;
        i1=RMB;
        System.out.print(i10+" ");
        System.out.print(i5+" ");
        System.out.print(i2+" ");
        System.out.print(i1+" ");
    }
}

5 - find the intersection of two sets of integers

[problem description]

Input two groups of integers from the standard input (no more than 20 integers in each group, the elements in each group of integers are not repeated, and the integers are greater than or equal to 0), program to find the intersection of the two groups of integers, that is, the integers that appear in both groups of integers, and sort and output them from small to large. If the intersection is empty, nothing is output.

[input form]

Enter two sets of integers on each line, separate each integer with a space, and end with - 1.

[output form]

Sort and output the intersection of two groups of integers from small to large (each integer is separated by a space, and the space after the last integer is optional).

[sample input]

5 105 0 4 32 8 7 9 60 -1
5 2 87 10 105 0 32 -1

[sample output]

0 5 32 105

import java.util.Scanner;
import java.util.Arrays;
public class zuoye02_5 {
    static int input(Scanner in){
        int tempInt=in.nextInt();
        while(tempInt<-1){
            tempInt=in.nextInt();
        }
        return tempInt;
    }
    public static void main(String[] args) {
        int i=0,n=0,x=0,i1,i2,min,t,j;
        int arr1[]=new int[21];
        int arr2[]=new int[21];
        int array[]=new int[21];
        Arrays.fill(array,-1);

        Scanner in=new Scanner(System.in);
        arr1[i]=input(in);
        while(arr1[i]!=-1){
            i++;
            arr1[i]=input(in);
        }
        i1=i;
        arr2[n]=input(in);
        while(arr2[n]!=-1){
            n++;
            arr2[n]=input(in);
        }
        i2=n;
        for(i=0;i<i1;i++){
            for(n=0;n<i2;n++){
                if(arr1[i]==arr2[n]){
                    array[x]=arr1[i];
                    x++;
                }
            }
        }
        Arrays.sort(array);
        for(int a : array)
            if(a!=-1)
                System.out.print(a + " ");
    }
}

6-

[problem description]

Input a group of unordered integers, and program to output the integers with the most occurrences and their occurrences.

[[input form]

First read the number of integers (greater than or equal to 1, less than or equal to 100) from the standard input, and then enter these integers on the next line, separated by a space.

[Output form]

On the standard output, the integer with the most occurrences and its occurrences are output, which are separated by a space; If there are multiple integers with the most occurrences, they will be output in ascending order.

[[sample input]

10

0 -50 0 632 5813 -50 9 -50 0 632

[[sample output]

-50 3

0 3
import java.util.Scanner;
import java.util.Arrays;
public class zuoye02_6 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int i,j,n;
        int max;
        int index=0;
        n=sc.nextInt();
        int a[] = new int[n+1];
        int b[] = new int[n+1];
        int c[] = new int[n+1];
        Arrays.fill(c,-1);

        for(i= 0;i < n;i++)
            a[i]=sc.nextInt();
        for(i= 0;i< n;i++){
            for(j= 0;j< n;j++){
                if(a[i] == a[j])
                    b[i]++;
            }
        }
        max = b[0];
        for(i= 1;i < n;i++){
            if (max < b[i]){
                max = b[i];
            }
        }
        for(int m= 0;m < n;m++){
            if(max == b[m]){
                c[index]=a[m];
                index++;
            }
        }
        Arrays.sort(c);
        if(c[0]!=-1)
            System.out.println(c[0]+" "+ max);
        for(int m= 0;m < n;m++){
            if(c[m+1]!=-1&&c[m+1]!=c[m])
                System.out.println(c[m+1]+" "+ max);
        }
    }
}

Keywords: Java

Added by The Wise One on Sat, 19 Feb 2022 13:05:32 +0200