长春大学20级第十三周第二次上机(5月27号)

本文涵盖了四个编程挑战,涉及创建Weather类、二维数组排序、汉诺塔问题以及字符串子串排序。在Weather类中,实现了随机生成温度和湿度并判断是否舒适的功能。二维数组排序方法通过转换为一维数组并排序实现。汉诺塔问题求解了最少移动次数。字符串子串排序展示了如何按字典顺序排列字符串的所有子串。
摘要由CSDN通过智能技术生成

(一)题目描述

编写Weather类:

(1)包含两个属性:温度(temperature)、湿度(humidity)

(2)无参构造方法:使用Random类,随机生成一个035之间的整数赋值给温度,随机生成一个3080之间的整数赋值给湿度。

(3)成员方法: isComfortable:如果温度在1525度间,湿度在5070间则返回true,否则返回false。

print:输出温度、湿度。

import java.util.*;
public class Weather {
    int temperature;
    int humidity;
    Random r=new Random();
    public Weather() {
        temperature = r.nextInt(36);
        humidity = r.nextInt(51) + 30;
    }
    public boolean isComfortable() {
        if(temperature >= 15 && temperature <= 25 && humidity >= 50 && humidity <= 70) {
            return true;
        }
        return false;
    }
    public void print() {
        System.out.println("温度为" + this.temperature + "湿度为" + this.humidity);
    }
}

(二)题目描述

编写一个方法public static int[][] dyadicArraySort(int a[][]),能够实现对一个二维数组a进行升序排列,要求整个二维数组中最小的值为a[0][0],然后依次是a[0][1],a[0][2]….,a[1][0],a[1][1]…..,使用一个无序的二维数组对你编写的方法进行检验。(基本思想:在方法体中,先将二维数组转换为一个一维数组,然后对一维数组进行排序,排好序后再将一维数组中的各个元素依次搬到二维数组中

import java.util.Arrays;
public class Main {
    public static int[][] dyadicArraySort(int a[][]){
        int n = 0;
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                n++;
            }
        }
        int b[] = new int[n];
        int k = 0;
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                b[k] = a[i][j];
                k++;
            }
        }
        Arrays.sort(b);
        k = 0;
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                a[i][j] = b[k];
                k++;
            }
        }
        return a;
    }
    public static void main(String[] args) {
        int n[][] = {{8, 7, 10}, {9, 1}, {15, 0, -3, 16}};
        output(n);
        dyadicArraySort(n);
        output(n);
    }
    public static void output(int a[][]){
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                System.out.print(a[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println("-----------------");
    }
}

(三)题目描述

汉诺塔:有三根相邻的柱子,标号为A,B,C,A柱子上从下到上按金字塔状叠放着n个不同大小的圆盘,要把所有盘子一个一个移动到柱子B上,并且每次移动同一根柱子上都不能出现大盘子在小盘子上方,请问至少需要多少次移动,设移动次数为H(n)。

import java.util.Scanner;
public class Main {

    static long s = 0;

    public static void main(String args[]) {
        int n = 0;
        Scanner console = new Scanner(System.in);
        n = console.nextInt();
        System.out.println("汉诺塔层数为" + n);
        System.out.println("移动方案为:");
        hanoi(n, 'a', 'b', 'c');
        System.out.println("需要移动次数:" + s);
    }

    static void hanoi(int n, char a, char b, char c) {
        if (n == 1){
            System.out.println("n=" + n + " " + a + "-->" + c);
            s++;
        }
        else{
            hanoi(n-1,a,c,b);
            System.out.println("n=" + n + " " + a + "-->" + c);
            hanoi(n-1,b,a,c);
            s++;
        }
    }
}

(四)题目描述

对于一个字符串,将其后缀子串进行排序,例如grain 其子串有: grain rain ain in n 然后对各子串按字典顺序排序,即: ain,grain,in,n,rain

import java.util.Arrays;
import java.util.Scanner;
public class Main {
    public static String[] sT(String s) {
        String[] result = new String[s.length()];
        for (int i = 0; i < s.length(); i++) {
            result[i] = s.substring(i);
        }
        Arrays.sort(result);
        return result;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String s = sc.nextLine();
            String[] result = sT(s);
            for (String c : result) {
                System.out.println(c);
            }
        }
    }
}

(五)题目描述

给出一个01字符串(长度不超过100),求其每一个子串出现的次数。对每个字符串,输出它所有出现次数在1次以上的子串和这个子串出现的次数,输出按字典序排序。

例如:10101 ,输出结果为:

0 2

01 2

1 3

10 2

101 2

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            Map count = new HashMap<>();
            String s = sc.nextLine();
            for (int i = 0; i < s.length(); i++) {
                for (int j = i + 1; j <= s.length();j++) {
                    String temp = s.substring(i, j);
                    if (count.get(temp) == null) {
                        count.put(temp, 1);
                    } else {
                        int value = (int) count.get(temp);
                        count.put(temp, ++value);
                    }
                }
            }
            Collection keys = count.keySet();
            List list = new ArrayList<>(keys);
            Collections.sort(list);
            for (Object str : list) {
                if ((int)count.get(str) > 1) {
                    System.out.println(str + " " + count.get(str));
                }
            }
        }
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小成同学_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值