zoukankan      html  css  js  c++  java
  • JAVA语言程序设计课后习题----第五单元解析(仅供参考)

    1    本题是水题,题目要求你求最大值、最小值,建议你用Arrays.sort函数进行排序,最大值、最小值就可以确定了

     1 import java.util.Arrays;
     2 import java.util.Scanner;
     3 
     4 public class paone {
     5     public static void main(String[] args) {
     6        int [] aa = new int[5];
     7        double sum = 0.0;
     8        Scanner input =new Scanner(System.in);
     9         for (int i = 0; i <5 ; i++) {
    10             aa[i] = input.nextInt();
    11             sum += aa[i];
    12         }
    13         Arrays.sort(aa);
    14         for (int a : aa)
    15             System.out.print(a+" ");
    16         System.out.println();
    17         System.out.println("最小值:"+aa[0]);
    18         System.out.println("最大值:"+aa[4]);
    19         System.out.println("平均值:"+sum/5);
    20     }
    21 }

    2  本题建议用swtich函数进行统计1-6的整数,每次产生一个随机数,就进行+1,switch中进行统计数目的大小建议用素组来定义。

     1 import java.util.Random;
     2 
     3 public class potwo {
     4     public static void main(String[] args) {
     5         Random r = new Random();
     6         int [] a =new int[1000];
     7         int [] b =new int[6];
     8         int count = 0;
     9         for (int i = 0; i < 1000; i++) {
    10             a[i] = r.nextInt(6)+1;
    11             switch (a[i]){
    12                 case 1:
    13                     b[0]++;
    14                     break;
    15                 case 2:
    16                     b[1]++;
    17                     break;
    18                 case 3:
    19                     b[2]++;
    20                     break;
    21                 case 4:
    22                     b[3]++;
    23                     break;
    24                 case 5:
    25                     b[4]++;
    26                     break;
    27                 case 6:
    28                     b[5]++;
    29                     break;
    30             }
    31         }
    32         for (int A : a){
    33             count++;
    34             System.out.print(A+" ");
    35             if (count % 10 == 0)
    36                 System.out.println();
    37         }
    38         System.out.println("1的个数为"+b[0]);
    39         System.out.println("2的个数为"+b[1]);
    40         System.out.println("3的个数为"+b[2]);
    41         System.out.println("4的个数为"+b[3]);
    42         System.out.println("5的个数为"+b[4]);
    43         System.out.println("6的个数为"+b[5]);
    44     }
    45 }

    3  本题水题,还是用sort函数进行排序,直接找到最小元素

     1 import java.util.Arrays;
     2 import java.util.Scanner;
     3 
     4 public class pothree {
     5     public static void main(String[] args) {
     6         double [] arrays = new double[5];
     7         Scanner input = new Scanner(System.in);
     8         for (int i = 0; i <5 ; i++) {
     9             arrays[i]=input.nextDouble();
    10         }
    11         System.out.println("这个元素最小值是:"+min(arrays));
    12     }
    13     public static double min(double[] array){
    14         Arrays.sort(array);
    15         return array[0];
    16     }
    17 }

    4  本题水题根据题目意思写即可,注意交换位置如何交换 a ^= b; b ^= a; a ^= b;

     1 import java.util.Scanner;
     2 
     3 public class pofour {
     4     public static void main(String[] args) {
     5         int [] aa = new int[10];
     6         Scanner input =new Scanner(System.in);
     7         for (int i = 0; i < 10 ; i++) {
     8             aa[i] = input.nextInt();
     9         }
    10         System.out.println("排序前的元素:");
    11         for (int A : aa)
    12             System.out.print(A+" ");
    13         System.out.println();
    14         for (int i = 0; i < 4 ; i++) {
    15             aa[i] ^= aa[9-i];
    16             aa[9-i] ^= aa[i];
    17             aa[i] ^= aa[9-i];
    18         }
    19         System.out.println("交换后的元素:");
    20         for (int A : aa)
    21             System.out.print(A+" ");
    22     }
    23 }

    5  理解题目意思即可

     1 import java.util.Scanner;
     2 
     3 public class pofive {
     4     public static void main(String[] args) {
     5         int [] aa = new int[8];
     6         Scanner input =new Scanner(System.in);
     7         for (int i = 0; i < 8; i++) {
     8             aa[i]=input.nextInt();
     9         }
    10         System.out.println("选择排序前:");
    11         for (int A : aa)
    12             System.out.print(A+" ");
    13         System.out.println();
    14         for (int i = 0; i < 7; i++) {
    15             int k = i;
    16             for (int j = k+1; j < 8 ; j++) {
    17                 if (aa[j] < aa[k])
    18                     k = j;
    19             }
    20 
    21             if (i!=k) {
    22                 aa[i] ^= aa[k];
    23                 aa[k] ^= aa[i];
    24                 aa[i] ^= aa[k];
    25             }
    26            /* for (int A : aa)
    27                 System.out.print(A+" ");
    28             System.out.println();*/
    29         }
    30         System.out.println("选择排序后:");
    31         for (int A : aa)
    32             System.out.print(A+" ");
    33     }
    34 }

    6  根据题目意思,把Fibonacci数列意思理解即可

     1 public class posix {
     2    public int [] aa = new int[20];
     3     public static void main(String[] args) {
     4         posix posix =new posix();
     5         int i,count=0;
     6         for (i = 0; i < 20; i++) {
     7             System.out.print(posix.fic(i)+" ");
     8             count++;
     9             if (count % 10 ==0)
    10                 System.out.println();
    11         }
    12 
    13 
    14     }
    15     public int fic(int n){
    16         if (n==0)
    17             return aa[n]=1;
    18         if (n==1)
    19             return aa[n]=1;
    20         else
    21             return fic(n-2)+fic(n-1);
    22     }
    23 }

    7  本题有点坑,你写的可能只是一种结果,因为你定义a、b两个数组,两个数组的长度有三种可能 a>b;a=b;a<b;三种可能,注意即可

     1 import java.util.Scanner;
     2 
     3 public class Poseven {
     4     public static void main(String[] args) {
     5         //int [] aa ={1,2,3,4,15};
     6         //int [] bb ={4,5,6,7};
     7 
     8         int a,b;
     9         Scanner input = new Scanner(System.in);
    10 
    11 
    12 
    13         System.out.print("请输入第一个数组的元素个数:");
    14         a=input.nextInt();
    15         System.out.print("请输入第二个数组的元素个数:");
    16         b=input.nextInt();
    17         int []aa=new int[a];
    18         int []bb=new int[b];
    19         System.out.println("请输入第一个数组的元素:");
    20         for (int i = 0; i < a; i++) {
    21           aa[i] = input.nextInt();
    22         }
    23         System.out.println("请输入第二个数组的元素:");
    24         for (int i = 0; i < b; i++) {
    25             bb[i] = input.nextInt();
    26         }
    27         System.out.println("加之前两个数组分别为:");
    28         for (int AA : aa)
    29             System.out.print(AA+" ");
    30         System.out.println();
    31         for (int BB : bb)
    32             System.out.print(BB+" ");
    33         System.out.println();
    34         System.out.println("相加后数组为:");
    35         Poseven poseven =new Poseven();
    36        int []abs= poseven.sumAraay(aa,bb);
    37        for (int ABS : abs)
    38            System.out.print(ABS+" ");
    39 
    40 
    41     }
    42     public static int[] sumAraay(int [] a,int [] b){
    43         int max,min;
    44         max = a.length >= b.length ? a.length:b.length;
    45         min = a.length <  b.length ? a.length:b.length;
    46         int leng1=a.length,leng2=b.length;
    47         int sum []=new int[max];
    48         for (int i = 0; i < min; i++) {
    49             sum[i] = a[i]+b[i];
    50             leng1--;
    51             leng2--;
    52         }
    53         for (int j = min; j < max; j++) {
    54             if (leng1==0)
    55                 sum[j] = b[j];
    56             else
    57                 sum[j] = a[j];
    58         }
    59        /* for (int SUM : sum)
    60             System.out.print(SUM+" ");
    61         System.out.println();*/
    62         return sum;
    63 
    64     }
    65 
    66 }

    8  定义一个可以放下这两个素组大小的素组,记录一个数组存完后,这个素组的下标即可

     1 import java.util.Arrays;
     2 
     3 public class Poeight {
     4     public static void main(String[] args) {
     5         Poeight poeight =new Poeight();
     6         int [] aa = {1,10,3,4};
     7         int [] bb = {4,5,6,7,8};
     8         System.out.println("和成前的数组分别为:");
     9         for (int AA : aa)
    10             System.out.print(AA+" ");
    11         System.out.println();
    12         for (int BB : bb)
    13             System.out.print(BB+" ");
    14         System.out.println();
    15         System.out.println("合成后的数组为:");
    16         int [] merge=poeight.arrayMerge(aa,bb);
    17         for (int MERGE : merge)
    18             System.out.print(MERGE+" ");
    19         System.out.println();
    20 
    21     }
    22     public static int [] arrayMerge(int [] a,int [] b){
    23         int sum=a.length+b.length;
    24         int [] suml = new int[sum];
    25         int min = a.length < b.length ? a.length:b.length;
    26         int max = a.length >= b.length ? a.length:b.length;
    27         for (int i = 0; i < min ; i++) {
    28             if (a.length < b.length)
    29             suml[i] = a[i];
    30             else
    31                 suml[i] = b[i];
    32         }
    33         for (int i = min; i < sum; i++) {
    34             if (a.length>b.length)
    35             suml[i] = a[i-b.length];
    36             else
    37                 suml[i] = b[i-a.length];
    38         }
    39         Arrays.sort(suml);
    40         return suml;
    41     }
    42 }

    9  根据题目意思,直接求解即可

     1 import java.util.Scanner;
     2 
     3 public class Ponine {
     4     public static void main(String[] args) {
     5         double [] aa = new double[3];
     6         double [] bb = new double[3];
     7         Scanner input = new Scanner(System.in);
     8         System.out.print("请依次输入a,b,c的值:");
     9         aa[0] = input.nextDouble();
    10         aa[1] = input.nextDouble();
    11         aa[2] = input.nextDouble();
    12         double x1 =( -aa[1]+Math.sqrt(Math.pow(aa[1],2)-4*aa[0]*aa[2]))/2*aa[0];
    13         double x2 =( -aa[1]-Math.sqrt(Math.pow(aa[1],2)-4*aa[0]*aa[2]))/2*aa[0];
    14         bb[0]=x1;
    15         bb[1]=x2;
    16 
    17         System.out.println("根的个数为"+ solveQuadratic(aa,bb));
    18 
    19     }
    20     public static int solveQuadratic(double [] eqn, double [] roots){
    21         int solve=-1;
    22         if(Math.pow(eqn[1],2)-4*eqn[0]*eqn[2]==0)
    23             solve=1;
    24         if(Math.pow(eqn[1],2)-4*eqn[0]*eqn[2] > 0)
    25             solve=2;
    26         return solve;
    27     }
    28 }

    10  本题有个小技巧,我用的是“置0法”,把不符合要求的全部置0,2是素数,所以我们从下标为1的开始筛选,这个方法有点坑,因为你这样筛选可能会把本身给筛选掉,就需要错位筛选,比如筛选2的倍数,我们从3开始筛选,依次类推,因为求的是2~100的素数,所以因子只可能到50,定义循环的次数即可

     1 public class Poten {
     2     public static void main(String[] args) {
     3         int a[] = new int[100];
     4         //int b[] =new int[99];
     5         int i,k,j,cout=0;
     6         for (i = 0; i < 99 ; i++) {
     7             a[i] = i+2;
     8         }
     9         for (int A : a)
    10             System.out.print(A+" ");
    11         System.out.println();
    12        /* System.out.println();
    13         k=0;
    14         System.out.println(a.length);
    15         for (i=0;i<a.length;i++) {
    16             if (a[i] % 2 != 0) {
    17                 b[k] = a[i];
    18                 k++;
    19             }
    20         }
    21         for (i=0;i<b.length;i++)
    22             System.out.print(b[i]+" ");*/
    23 //          把每次结果都输出来显示。
    24         for (j = 2; j <=50 ; j++) {
    25             for (k = j-1 ; k<a.length ; k++) {
    26                 if (a[k] % j == 0)
    27                     a[k]=0;
    28             }
    29             for (int A : a)
    30                 System.out.print(A+" ");
    31             System.out.println();
    32         }
    33 
    34         for (i=0;i<a.length;i++){
    35             if (a[i]!=0)
    36                 System.out.print(a[i]+" ");
    37         }
    38        // a[i] != ''
    39         //System.out.println(cout);
    40     }
    41 }

    11  根据题目意思理解即可

     1 import java.util.Arrays;
     2 
     3 public class Poeleven {
     4     public static void main(String[] args) {
     5         int [] a={1,2,3,4};
     6         int [] b={1,2,3,4};
     7 
     8        System.out.println( equals(a,b));
     9        // System.out.println(a.equals(b));
    10     }
    11     public static boolean equals (int [] list1,int []  list2){
    12 //        定义count的原因是把满足题目条件的结果进行加一
    13         int count=0;
    14         if(Arrays.equals(list1,list2)){
    15             for (int i = 0; i <list1.length ; i++) {
    16                if(list1[i]==list2[i])
    17                    count++;
    18             }
    19         }
    20 //        满足条件的结果与数组长度相等则满足
    21         if (count==list1.length)
    22             return true;
    23         else
    24             return false;
    25     }
    26 
    27 }

    12  本题用的 较简单的方法与第十题基本想法一致,每次循环一遍就把那个数“置0”

     1 public class Potwleven {
     2 
     3     public static void main(String[] args) {
     4         int j=0,i=0;
     5         int a[]={1,2,3,4,5,6,7,8,9,10,11,12};
     6        //int t= s(a);
     7 //        while循环的条件就是数组里面不为18的数大于1
     8         while (s(a)>1){
     9 //            因为是12个数,则用%12来进行循环
    10             if (a[i%12]!=18)
    11                 j++;
    12             if (j % 5 == 0)
    13                 a[i%12]=18;
    14             i++;
    15         }
    16        // System.out.println(j);
    17         for (int k = 0; k <a.length ; k++) {
    18             if (a[k]!=18)
    19                 System.out.println(a[k]);
    20         }
    21     }
    22     public static int s(int a[]){
    23         int count=0;
    24         for (int i = 0; i < a.length; i++) {
    25             if (a[i]!=18)
    26                 count++;
    27         }
    28         return count;
    29     }
    30 }

    13  本题的选牌次数纯属运气,因为有随机,根据题目意思定义一个二维数组来存放牌

     1 import java.util.Random;
     2 
     3 public class threeteen {
     4     public static void main(String[] args) {
     5         int [][]desk = new int[4][13];
     6         int count=1;
     7 //        用来存放选牌的次数
     8         int cu=0;
     9         boolean flag =true;
    10         for (int i = 0; i <4 ; i++) {
    11             for (int j = 0; j < 13; j++) {
    12                 desk[i][j]=count;
    13                 count++;
    14             }
    15             count=1;
    16         }
    17 
    18         Random random =new Random();
    19         while (flag) {
    20             cu++;
    21             int r1 = random.nextInt(4);
    22             int r2 = random.nextInt(13);
    23             int a = desk[r1][r2];
    24             int b = desk[r1][r2];
    25             int c = desk[r1][r2];
    26             int d = desk[r1][r2];
    27             if (a+b+c+d==24)
    28                 flag=false;
    29 
    30         }
    31         System.out.println(cu);
    32     }
    33 
    34    /* public static void main(String[] args) {
    35         int[] sum = new int[52];
    36         int[] a = new int[4];
    37         boolean flag = true;
    38         for (int i = 0; i < 52; i++) {
    39             sum[i] = i % 13 + 1;
    40         }
    41         for (int i = 0; i < sum.length; i++) {
    42             System.out.print(sum[i] + " ");
    43         }
    44         System.out.println();
    45 
    46         Random random = new Random();
    47         int r = random.nextInt(52);
    48         int n = sum.length;
    49         int cu = 0;
    50         while (flag) {
    51             for (int i = 0; i < 4; i++) {
    52                 a[i] = sum[r];
    53                 for (int j = r; j < n - 1; j++) {
    54                     sum[j] = sum[j + 1];
    55                 }
    56                 n--;
    57                 for (int j = 0; j < n; j++) {
    58                     System.out.print(sum[j] + " ");
    59                 }
    60                 System.out.println();
    61             }
    62 
    63             System.out.println();
    64             if (a[0]+a[1]+a[2]+a[3]==24)
    65                 flag=false;
    66           }*/
    67 
    68 
    69 }

    14  本题就是求因子,每次求得的因子就让他进栈,知道最后一次进栈,最后再出栈,即可

     1 import java.util.Scanner;
     2 
     3 public class Pofourteen {
     4     private int[]elements;
     5     private int size=0;
     6     public static final int DEFAULT_CAPACITY = 10;
     7 
     8     public Pofourteen(){
     9         this(DEFAULT_CAPACITY);
    10     }
    11     public Pofourteen(int capacity){
    12         elements = new int[capacity];
    13     }
    14     //进栈
    15     public void push(int value){
    16         if(size >= elements.length){
    17             int []temp = new int[elements.length*2];
    18             System.arraycopy(elements,0,temp,0,elements.length);
    19             elements = temp;
    20         }
    21         elements[size++]=value;
    22     }
    23     //出栈
    24     public int pop() {
    25         return elements[--size];
    26     }
    27 
    28     public int peek() {
    29         return elements[size--];
    30     }
    31 //  栈是否为空
    32     public boolean empty() {
    33         return size == 0;
    34     }
    35 
    36     public int getSize() {
    37         return size;
    38     }
    39 
    40 
    41     public static void main(String[] args) {
    42         Scanner input = new Scanner(System.in);
    43         int number = input.nextInt();
    44         Pofourteen stack = new Pofourteen();
    45         Pofourteen stack1 =new Pofourteen();
    46         int ans=0;
    47         int[] a = new int[20];
    48         while (number != 1) {
    49             for (int i = 2; i <= number; i++) {
    50                 if (number % i == 0) {
    51                    // a[ans] = i;
    52                     stack.push(i);
    53                     ans++;
    54                     number /= i;
    55                     break;
    56                 }
    57             }
    58         }
    59         while (!stack.empty()) {
    60             stack1.push(stack.pop());
    61          // System.out.print(stack1.pop() + " ");
    62         }
    63         //System.out.println();
    64         while (!stack1.empty()) {
    65            // stack1.push(stack.pop());
    66             System.out.print(stack1.pop() + " ");
    67         }
    68     }
    69 }

    15  注意转置是什么意思就是行列里面的元素互换

     1 public class Pofifteen {
     2     public static void main(String[] args) {
     3         int [][]A= {{1,3,5},{-3,6,0},{13,-5,7},{-2,19,25}};
     4         int [][]B={{0,-1,2},{7,-1,6},{-6,13,2},{12,-8,-13}};
     5         System.out.println("A+B矩阵");
     6         jia(A,B);
     7         System.out.println("A-B矩阵");
     8         jian(A,B);
     9        // System.out.println(A[0].length);
    10       //  System.out.println(A.length);
    11         System.out.println("*********************");
    12         System.out.println("A的转置");
    13         zhuan(A);
    14     }
    15     public static void jia(int [][]a,int [][]b){
    16         int [][] add= new int[4][3];
    17         for (int i = 0; i < 4; i++) {
    18             for (int j = 0; j < 3; j++) {
    19                 add [i][j] = a[i][j]+b[i][j];
    20                 System.out.print(add [i][j]+" ");
    21             }
    22             System.out.println();
    23         }
    24     }
    25     public static void jian(int [][]a,int [][]b){
    26         int [][] add= new int[4][3];
    27         for (int i = 0; i < 4; i++) {
    28             for (int j = 0; j < 3; j++) {
    29                 add [i][j] = a[i][j]-b[i][j];
    30                 System.out.print(add [i][j]+" ");
    31             }
    32             System.out.println();
    33         }
    34     }
    35     public static void zhuan(int [][]a){
    36         int [][]AA=new int[3][4];
    37         for (int i = 0; i < a[0].length; i++) {
    38             for (int j = 0; j < a.length; j++) {
    39                 AA[i][j]=a[j][i];
    40             }
    41         }
    42         for (int i = 0; i < a[0].length; i++) {
    43             for (int j = 0; j < a.length; j++) {
    44                 System.out.print(AA[i][j]+" ");
    45             }
    46             System.out.println();
    47         }
    48     }
    49 }

    16  把二维素组的下标存入数组即可

     1 import java.util.Scanner;
     2 
     3 public class Posixteen {
     4    public static int a1,a2;
     5     public static void main(String[] args) {
     6         System.out.print("请输入数组的行数和列数:");
     7         Scanner input = new Scanner(System.in);
     8 
     9         a1 = input.nextInt();
    10        a2 =input.nextInt();
    11        double [][]A=new double[a1][a2];
    12         for (int i = 0; i <a1 ; i++) {
    13             for (int j = 0; j <a2 ; j++) {
    14                 A[i][j] = input.nextDouble();
    15             }
    16         }
    17         int C[]=locateLargest(A);
    18         System.out.println("最大数的位置是:"+"("+C[0]+","+C[1]+")");
    19     }
    20     public static int[] locateLargest(double [][] a){
    21        int n=0;
    22        int m=0;
    23        int c[]=new int[2];
    24         for (int i = 0; i <a1 ; i++) {
    25             for (int j = 0; j <a2 ; j++) {
    26              //   a[i][j] = input.nextDouble();
    27                 if (a[i][j]>a[n][m]) {
    28                     c[0] = i;
    29                     c[1] = j;
    30                     n=i;
    31                     m=j;
    32                 }
    33             }
    34         }
    35         return c;
    36     }
    37 }

    17  本题水题,但是有点意思,根据题目的意思写即可

     1 import java.util.Scanner;
     2 
     3 public class Potwelve {
     4    /* public static void main(String[] args) {
     5         Scanner input = new Scanner(System.in);
     6         int num = input.nextInt();
     7         int count = 1;
     8         int n=0,m=num/2;
     9         int temp1=0,temp2=0;
    10         int [][]A = new int[num][num];
    11         for (int i = 0; i <num*num; i++) {
    12 
    13             //    System.out.println(n+" "+m);
    14 
    15              //   if (A[n][m]==0){
    16                   //  A[n][m]=count;
    17             if (A[n][m]!=0) {
    18                 n=++temp1;
    19                 m=temp2;
    20             }
    21                 A[n][m]=count;
    22 
    23                  //   System.out.println(n+" "+m+" "+A[n][m]);
    24                     count++;
    25                     temp1 = n;
    26                     temp2 =m;
    27                     n--;
    28                     m++;
    29                     if (n==-1)
    30                         n=num-1;
    31                     if (m==num)
    32                         m=0;
    33                // }
    34         }
    35         for (int i = 0; i <num; i++) {
    36             for (int j = 0; j <num; j++) {
    37                 System.out.print(A[i][j]+" ");
    38             }
    39             System.out.println();
    40         }
    41     }*/
    42    public static void main(String[] args) {
    43        Scanner input =new Scanner(System.in);
    44        int num = input.nextInt();
    45        int [][]A=new int[num][num];
    46 
    47        int x=0,y=num/2;
    48        for (int i = 1; i <=num*num ; i++) {
    49 
    50            if (A[x][y]!=0) {
    51                //A[x][y] = i;
    52                x=(x+2)%num;
    53              //  y=(y+1)%num;
    54 //               保存原来位置的y
    55                y=(y+num-1)%num;
    56            }
    57           A[x][y]=i;
    58            x=(x+num-1)%num;
    59            y=(y+1)%num;
    60        }
    61        for (int i = 0; i < num; i++) {
    62            for (int j = 0; j < num; j++) {
    63                System.out.print(A[i][j]+" ");
    64            }
    65            System.out.println();
    66        }
    67    }
    68 }
  • 相关阅读:
    Shell脚本创建Nginx的upstream及location配置文件
    系统初始化
    算法训练 P0505
    算法训练 素因子去重
    基础训练 时间转换
    基础训练 字符串对比
    基础训练 分解质因数
    基础训练 矩形面积交
    快速幂矩阵
    基础训练 矩阵乘法
  • 原文地址:https://www.cnblogs.com/PerZhu/p/10886761.html
Copyright © 2011-2022 走看看