第四周作业

后端-第四周作业考核

第四周后端组作业

  1. 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,不要使用额外的数组空间,你必须在 原地 修改输入数组

说明:原地删除重复出现的元素即为不能申请额外的数组空间,只能在原有数组上进行修改

示例1:

输入: 1,1,2
输出:2, nums = 1,2
解释:应该输出新的长度 2 ,并且原数组 nums 的前两个元素被修改为 1, 2 。不需要考虑数组中超出新长度后面的元素。

提示:

0 <= nums.length <= 3 * 104
-104 <= nums[i] <= 104
nums 已按升序排列
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        String[] nums = s.split(",");
        int a = 0, b = 1 ;
        int len = 0;
        //覆盖重复的;
        while(b < nums.length){
            if (!nums[a].equals(nums[b])){
                nums[a+1] = nums[b];
                a = a + 1;
            }
            b++;
        }
        //输出;
        len = a + 1;
        System.out.print(len + ",");
        System.out.print("nums = ");
        for (int i = 0 ; i < len ; i++) {
            String tmp = nums[i];
            if (i != 0){
                System.out.print(",");
            }
            System.out.print(tmp);
        }

    }
}
  1. 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。

示例一:

输入:

1,2,3,4,5,6,7

k=3

输出:

[5,6,7,1,2,3,4]
解释:
向右旋转 1 步: [7,1,2,3,4,5,6]
向右旋转 2 步: [6,7,1,2,3,4,5]
向右旋转 3 步: [5,6,7,1,2,3,4]

要求:大二同学解此题不允许使用临时数组存取数据,只能在原数组上进行修改

package manuscript;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //分割出数组
        String s1 = scan.next();
        String[] nums = s1.split(",");
        //分割出数字字符
        String s2 = scan.next();
        String s3 = s2.replaceAll("\\D+", "");
        int k = Integer.parseInt(s3);
        rotate(nums, k);
        System.out.print("[" + nums[0] + ",");
        for (int i = 1; i < nums.length-1; i++) {
            System.out.print(nums[i] + ",");
        }
        System.out.print(nums[nums.length-1] + "]");

    }

    private static void reverse(String[] nums, int start, int end) {//翻转指定范围内的数组
        for (int i = start, j = end; i < j; i++, j--) {
            String temp = nums[j];
            nums[j] = nums[i];
            nums[i] = temp;
        }
    }

    public static void rotate(String[] nums, int k) {
        //将数组向右旋转k个单位
        int n = nums.length;
        k %= n;
        reverse(nums, 0, n - 1);
        reverse(nums, 0, k - 1);
        reverse(nums, k, n - 1);
    }
}

  1. 请写出下面代码的输出结果(自己先看着代码思考,写出答案,不要用编辑器运行
//爷爷类
class Ye {
    public String show(Sun obj) {
        return ("Ye and Sun");
    }

    public String show(Ye obj) {
        return ("Ye and Ye");
    }

}
//爸爸类
class Fu extends Ye {
    public String show(Fu obj) {
        return ("Fu and Fu");
    }

    public String show(Ye obj) {
        return ("Fu and Ye");
    }
}
//儿子类
class Zi extends Fu {

}
//孙子类
class Sun extends Fu {

}

public class PolymorphicTest {
    public static void main(String[] args) {
         Ye y = new Ye();
        Ye y2 = new Fu(); //向上
        Fu f = new Fu();
        Zi z = new Zi();
        Sun s = new Sun();


        System.out.println("第一题 " + y.show(f));
        System.out.println("第二题 " + y.show(z));
        System.out.println("第三题 " + y.show(s));
        System.out.println("第四题 " + y2.show(f)); 
        System.out.println("第五题 " + y2.show(z));
        System.out.println("第六题 " + y2.show(s));
        System.out.println("第七题 " + f.show(f));
        System.out.println("第八题 " + f.show(z));
        System.out.println("第九题 " + f.show(s));
     
    }
}
你的答案://雀氏好玩
第一题 :Ye and Ye
第二题 :Ye and Ye
第三题 :Ye and Sun
第四题 :Fu and Ye//多态
第五题 :Fu and Ye
第六题 :Ye and Sun
第七题 :Fu and Fu
第八题 :Fu and Fu
第九题 :Ye and Sun

如果你对答案很意外,或者不解,那么恭喜你,你又能学到新知识了,再去深入了解一下多态吧。

  1. 请将这张图片从D:\images\EDG.png 用java代码将其拷贝到D:\img\EDG.png

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dykT3Rng-1636684968459)(D:\img\EDG.png)]

提示:在自己D盘下新建文件夹images,将这张图片粘贴进去并改名

import java.io.*;

public class Main {
    public static void main(String[] args){
        //你的代码
        mkdirs(new File("D:\\img"));

        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("D:\\images\\EDG.png");
            fos = new FileOutputStream("D:\\img\\EDG.png");
            int len;
            while ((len = fis.read()) != -1) {//这里的len就是read返回数据,且len不等于-1
                fos.write(len);
            }
        } catch(IOException e) {
            e.printStackTrace();
        }


    }
    public static void mkdirs(File file) {
        if (file.exists() && file.isDirectory()) {
            return;
        }
        if (file.exists()) {
            file.delete();
            file.mkdirs();
        } else {
            file.mkdirs();
        }
    }
}
  1. 简单的在线信息接收

要求:用户能在客户端向服务端发送信息,服务端能够接收用户所发信息,用户向服务端发送“ bye"结束发送

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) {
        Socket s = null;
        DataOutputStream dos = null;
        Scanner scan = null;
        try {
            s = new Socket(InetAddress.getLocalHost(), 9999);
            dos = new DataOutputStream(s.getOutputStream());
            scan = new Scanner(System.in);
            System.out.println("==客户端的启动==");
            while (true) {
                System.out.print("请说:");
                String str = scan.nextLine();
                if ("bye".equals(str)) {
                    dos.writeUTF(str);
                    return;
                }
                dos.writeUTF(str);

            }
        } catch (IOException e) {
            
            try {
                dos.close();
                s.close();
                scan.close();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }

    }
}
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) {
        ServerSocket ss = null;
        DataInputStream dis = null;
        Socket s = null;
        try {
            ss = new ServerSocket(9999);
            s = ss.accept();
            dis = new DataInputStream(s.getInputStream());
            System.out.println("==服务器的启动==");
            while (true) {
                String str = dis.readUTF();
                System.out.println("服务端收到:" + str);
            }
        } catch (IOException e) {
            try {
                System.out.println("客户端已退出");
                dis.close();
                ss.close();
                s.close();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }

        }


    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-C8U9fDQL-1636684968462)(C:\Users\冯小强\AppData\Roaming\Typora\typora-user-images\image-20211108160542742.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JtBcUv2y-1636684968464)(C:\Users\冯小强\AppData\Roaming\Typora\typora-user-images\image-20211108160554077.png)]

ry {
System.out.println(“客户端已退出”);
dis.close();
ss.close();
s.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}

    }


}

}






[外链图片转存中...(img-C8U9fDQL-1636684968462)]

[外链图片转存中...(img-JtBcUv2y-1636684968464)]



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

兴趣使然的小小

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

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

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

打赏作者

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

抵扣说明:

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

余额充值