PTA 循环和数组(Java)

一、 组合找出最大值和最小值 (15 分)

从键盘输入0~9之内的4个整数,计算由这4个整数组合成的整数的最大值和最小值。若输入的不是整数则输出“number input error”若输入的整数不是0~9范围内则输出“Numerical range error”

输入格式:
从键盘输入0~9之内的4个整数,以空格分割

输出格式:
对每一组输入,在一行中输出两个整数,最大值和最小值。

输入样例:
1 5 9 3
输出样例:
9531 1359

输入样例:
1 5 a 9
输出样例:
number input error

输入样例:
1 5 10 9
输出样例:
Numerical range error


import java.util.Arrays;
import java.util.Scanner;

public class Main {
    
    
    public static void main(String args[]) {
    
    

        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String[] strings = s.split(" ");

        int[] a = new int[4];
        for (int i = 0; i < strings.length; i++) {
    
    
            if (strings[i].charAt(0) < '0' || strings[i].charAt(0) > '9') {
    
    
                System.out.println("number input error");
                return;
            } else if (strings[i].length() != 1) {
    
    
                System.out.println("Numerical range error");
                return;
            } else {
    
    
                a[i] = Integer.parseInt(strings[i]);
            }
        }

        Arrays.sort(a);
        String s1 = "";
        for (int i = 3; i >= 0; i--) {
    
    
            s1 += a[i];
        }
        String s2 = "";
        for (int i = 0; i < 4; i++) {
    
    
            s2 += a[i];
        }
        System.out.println(Integer.parseInt(s1) + " " + Integer.parseInt(s2));
        /*或
          int max=a[3]*1000+a[2]*100+a[1]*10+a[0];
          int min=a[0]*1000+a[1]*100+a[2]*10+a[3];
          System.out.println( max+ " " + min);
         */
    }
}

二、JAVA-求整数序列中出现次数最多的数 (15 分)

要求统计一个整型序列中出现次数最多的整数及其出现次数。

输入格式:
在一行中给出序列中整数个数N(0<N≤1000),依次给出N个整数,每个整数占一行。

输出格式:
在一行中输出出现次数最多的整数及其出现次数,数字间以空格分隔。题目保证这样的数字是唯一的。

输入样例:
在这里给出一组输入。例如:

10
3
2
-1
5
3
4
3
0
3
2

输出样例:
在这里给出相应的输出。例如:

3 4
方法一:两个数组

import java.util.Scanner;


public class Main {
    
    
    public static void main(String args[]) {
    
    
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int a[] = new int[n];
        int b[] = new int[n];
        for (int i = 0; i < n; i++) {
    
    
            a[i] = sc.nextInt();
        }
        for (int i = 0; i < n; i++) {
    
    
            for (int j = 0; j < n; j++) {
    
    
                if (a[i] == a[j]) {
    
    
                    b[i]++;
                }
            }
        }
        int m = 0;
        int max = b[0];
        for (int i = 1; i < n; i++) {
    
    
            if (max < b[i]) {
    
    
                max = b[i];
                m = i;
            }
        }
        System.out.println(a[m] + " " + max);
    }
}


方法二:一个数组,简单

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int[] a=new int[n];
        int t=0;
        int b=0;
        for(int i=0;i<n;i++)
            a[i]=sc.nextInt();
        for(int i=0;i<n;i++)
        {
    
    
            int c=0;
            for(int j=0;j<n;j++)
            {
    
    
                if(a[i]==a[j])
                {
    
    
                    c++;
                }
            }
            if(c>b)
            {
    
    
                t=a[i];
                b=c;
            }
        }
        System.out.println(t+" "+b);

    }
}

三、 jmu-java-m02-使用二维数组存储多元线性方程组 (15 分)

题面
可以使用二维数组存储来存储线性方程组的系数与常数。比如,对于如下3元线性方程组
3x+y+z=1
6x+2y+z=-1
-2x+2y+z=7
可以使用二位数组存储
2 1 1 1
6 2 1 -1
-2 2 1 7
编写一个程序可以存储n元线性方程组

输入格式:
整数n,代表n元
n行、每行n+1列线性方程组的系数与常数。系数与常数为double型。

输出格式:
格式化输出二维数组。注意:使用Arrays.deepToString进行格式化输出。
依次输出n行线性方程组的系数与常数。系数以 , 分隔,系数与常数之间以 = 分隔,= 之间有两个空格。

输入样例:
3
2 1 1 1
6 2 1 -1
-2 2 1 7

输出样例:
[[2.0, 1.0, 1.0, 1.0], [6.0, 2.0, 1.0, -1.0], [-2.0, 2.0, 1.0, 7.0]]
2.0, 1.0, 1.0 = 1.0
6.0, 2.0, 1.0 = -1.0
-2.0, 2.0, 1.0 = 7.0

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        double a[][] = new double[n][n + 1];
        for (int i = 0; i < a.length; i++) {
    
    
            for (int j = 0; j < a[i].length; j++) {
    
    
                a[i][j] = sc.nextDouble();
            }
        }
        System.out.println(Arrays.deepToString(a));
        for (int i = 0; i < a.length; i++) {
    
    
            for (int j = 0; j < a[i].length; j++) {
    
    
                if (j < a[i].length - 2) {
    
    
                    System.out.print(a[i][j] + ", ");
                }
                else if (j == a[i].length - 2) {
    
    
                    System.out.print(a[i][j] + " = ");
                }
                else if (j == a[i].length - 1) {
    
    
                    System.out.println(a[i][j]);
                }
            }
        }
    }
}


四、找到二维数组最大值和最小值并实现交换 (15 分)

有一个5行4列的整形二维数组,请找出该二维组数中的最大值和最小值,并互换这两个数的位置。

输入格式:

请输入20个整数,数据之间只能用1个空格间隔。

输出格式:

在一行中按照“max=最大值,min=最小值”的格式输出结果,最大值和最小值均原样输出,没有列宽控制。 在下一行中输出交换完最大值和最小值的二维数组,每个数据输出占5列,右对齐。

输入样例:

55 14 13 10

20 34 21 11

89 13 45 14

9 5 18 77

90 22 38 82

输出样例:

max=90,min=1

55 14 13 10

20 34 21 11

89 13 45 14

9 90 18 77

5 22 38 82

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        int a[][] = new int[5][4];
        for (int i = 0; i < a.length; i++) {
    
    
            for (int j = 0; j < a[i].length; j++) {
    
    
                a[i][j] = sc.nextInt();
            }
        }
        int max = a[0][0], m = 0, n = 0;
        for (int i = 0; i < a.length; i++) {
    
    
            for (int j = 0; j < a[i].length; j++) {
    
    
                if (max < a[i][j]) {
    
    
                    max = a[i][j];
                    m = i;
                    n = j;
                }
            }
        }
        int min = a[0][0], x = 0, y = 0;
        for (int i = 0; i < a.length; i++) {
    
    
            for (int j = 0; j < a[i].length; j++) {
    
    
                if (min > a[i][j]) {
    
    
                    min = a[i][j];
                    x = i;
                    y = j;
                }
            }
        }
        int t;
        t = a[m][n];
        a[m][n] = a[x][y];
        a[x][y] = t;
        System.out.println("max="+max+",min="+min);
        for (int i = 0; i < a.length; i++) {
    
    
            for (int j = 0; j < a[i].length; j++) {
    
    
                System.out.printf("%5d",a[i][j]);
            }
            System.out.println();
        }
    }
}


五、数组的合并和升序排列。 (20 分)

有两个数组:数组a为1,7,9,11,13,15,17,19,数组b为2,4,6,8,10.将两个数组合并为数组c,按升序排列。

输出格式:
输出数组合并后的升序排列。

输出样例:
1 2 4 6 7 8 9 10 11 13 17 19

import java.util.Arrays;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        int [] a={
    
    1,7,9,11,13,15,17,19};
        int [] b={
    
    2,4,6,8,10};
        int [] c = new int[a.length+b.length];
        System.arraycopy(a,0,c,0,a.length);
        System.arraycopy(b,0,c,a.length,b.length);
        Arrays.sort(c);
        for(int i=0;i<c.length-1;i++){
    
    
            System.out.print(c[i]+"  ");
        }
        System.out.print(c[c.length-1]);
    }
}

六、 数组元素移动 (20 分)

完成数组元素的移动功能:假设数组有n个元素,输入一个数x,把数组的第x个位置的元素先保存起来,然后把x+1到n的元素,依次往前移一位,最后将原来的第x个位置的元素放在数组的最后。 重复若干次这样的移动,得到最后的结果。

输入格式:
第一行包括一个整数n(1<=n<=100),表示数组元素的个数。 第二行输入n个数组元素,均为整数,用空格隔开。 第三行输入一个数k(1<=k<=100),表示要进行k次移动。 接下来k行,每行一个数x,表示要移动第x个元素。

输出格式:
输出经过k次移动后的数组,每两个元素之间用空格隔开。

输入样例:
10
1 2 3 4 5 6 7 8 9 10
4
4
3
2
5
输出样例:
1 5 6 7 9 10 4 3 2 8

函数方法:

import java.util.Scanner;

public class Main {
    
    
    public static void fun(int a[], int n) {
    
    
        int t = a[n - 1];
        for (int i = n - 1; i < a.length - 1; i++) {
    
    
            a[i] = a[i + 1];
        }
        a[a.length - 1] = t;
    }

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

        for (int i = 0; i < n; i++) {
    
    
            a[i] = sc.nextInt();
        }

        int m = sc.nextInt();
        for (int i = 0; i < m; i++) {
    
    
            fun(a, sc.nextInt());
        }
        for (int i = 0; i < n - 1; i++) {
    
    
            System.out.println(a[i] + " ");
        }
        System.out.print(a[a.length - 1]);
    }
}

正常做法:

import java.util.Scanner;

public class Main {
    
    

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

        for (int i = 0; i < n; i++) {
    
    
            a[i] = sc.nextInt();
        }

        int m = sc.nextInt();
        for (int i = 0; i < m; i++) {
    
    
            int k=sc.nextInt();
            int t = a[k - 1];
            for (int j = k - 1; j < a.length - 1; j++) {
    
    
                a[j] = a[j + 1];
            }
            a[a.length - 1] = t;
        }
        for (int i = 0; i < n - 1; i++) {
    
    
            System.out.print(a[i] + " ");
        }
        System.out.print(a[a.length - 1]);
    }
}


猜你喜欢

转载自blog.csdn.net/Anemia_/article/details/116897313