1:请简述在java语言中for循环while循环和递归的使用场景以及各自的优缺点。2:请用java代码实现面向对象设计原则中的常用原则2~3中即可3:请用java代码实现面向对象设计模式中的常用的模式不包括单例模式工厂模式4:有1000个随机正整数例如:1234564567… 要求分为两组要求两组的总和尽量接近 1:请用java代码高效的实现。如果有100000个随机正整数呢? 假设要对1000
1:在Java语言中,for循环适用于已知循环次数的情况,可以通过控制循环变量来实现循环。while循环适用于未知循环次数的情况,可以通过条件判断来控制循环。递归适用于问题可以拆解为同样类型的子问题,并且每个子问题的解决方式与原问题相同的情况。
for循环的优点是结构清晰,循环变量控制明确,适用于已知循环次数的情况。缺点是需要手动控制循环变量,容易出错。
while循环的优点是适用于未知循环次数的情况,循环条件更为灵活。缺点是需要手动设置循环条件,容易出错。
递归的优点是可以将复杂问题拆解为简单问题,代码结构清晰。缺点是递归深度过大时会消耗大量的栈空间,可能导致栈溢出。
2:常用的面向对象设计原则有封装、继承、多态。
封装:将数据和方法封装在一个类中,通过访问修饰符控制对数据的访问,提高代码的安全性和可维护性。
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
继承:通过继承可以实现代码的复用,可以将公共的属性和方法提取到父类中。
public class Animal {
protected String name;
public Animal(String name) {
this.name = name;
}
public void eat() {
System.out.println(name + " is eating");
}
}
public class Cat extends Animal {
public Cat(String name) {
super(name);
}
public void meow() {
System.out.println(name + " is meowing");
}
}
多态:通过父类引用指向子类对象,实现对不同子类对象的统一操作。
public class Animal {
public void eat() {
System.out.println("Animal is eating");
}
}
public class Cat extends Animal {
public void eat() {
System.out.println("Cat is eating");
}
}
public class Dog extends Animal {
public void eat() {
System.out.println("Dog is eating");
}
}
public class Main {
public static void main(String[] args) {
Animal cat = new Cat();
Animal dog = new Dog();
cat.eat(); // 输出 "Cat is eating"
dog.eat(); // 输出 "Dog is eating"
}
}
3:常用的面向对象设计模式有策略模式、观察者模式、装饰者模式。
策略模式:定义一系列算法,将每个算法封装起来,并且使它们可以互换。
public interface Strategy {
void execute();
}
public class ConcreteStrategyA implements Strategy {
public void execute() {
System.out.println("Strategy A is executed");
}
}
public class ConcreteStrategyB implements Strategy {
public void execute() {
System.out.println("Strategy B is executed");
}
}
public class Context {
private Strategy strategy;
public void setStrategy(Strategy strategy) {
this.strategy = strategy;
}
public void executeStrategy() {
strategy.execute();
}
}
public class Main {
public static void main(String[] args) {
Context context = new Context();
Strategy strategyA = new ConcreteStrategyA();
context.setStrategy(strategyA);
context.executeStrategy(); // 输出 "Strategy A is executed"
Strategy strategyB = new ConcreteStrategyB();
context.setStrategy(strategyB);
context.executeStrategy(); // 输出 "Strategy B is executed"
}
}
观察者模式:定义了一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖它的对象都会得到通知并自动更新。
public interface Observer {
void update();
}
public class ConcreteObserver implements Observer {
public void update() {
System.out.println("Observer is updated");
}
}
public class Subject {
private List<Observer> observers = new ArrayList<>();
public void attach(Observer observer) {
observers.add(observer);
}
public void detach(Observer observer) {
observers.remove(observer);
}
public void notifyObservers() {
for (Observer observer : observers) {
observer.update();
}
}
}
public class Main {
public static void main(String[] args) {
Subject subject = new Subject();
Observer observer = new ConcreteObserver();
subject.attach(observer);
subject.notifyObservers(); // 输出 "Observer is updated"
subject.detach(observer);
subject.notifyObservers(); // 无输出
}
}
装饰者模式:动态地将责任附加到对象上,若要扩展功能,装饰者提供了比继承更有弹性的替代方案。
public interface Component {
void operation();
}
public class ConcreteComponent implements Component {
public void operation() {
System.out.println("Component operation");
}
}
public abstract class Decorator implements Component {
protected Component component;
public Decorator(Component component) {
this.component = component;
}
public void operation() {
component.operation();
}
}
public class ConcreteDecoratorA extends Decorator {
public ConcreteDecoratorA(Component component) {
super(component);
}
public void operation() {
super.operation();
System.out.println("Decorator A operation");
}
}
public class ConcreteDecoratorB extends Decorator {
public ConcreteDecoratorB(Component component) {
super(component);
}
public void operation() {
super.operation();
System.out.println("Decorator B operation");
}
}
public class Main {
public static void main(String[] args) {
Component component = new ConcreteComponent();
Component decoratorA = new ConcreteDecoratorA(component);
Component decoratorB = new ConcreteDecoratorB(decoratorA);
decoratorB.operation(); // 输出 "Component operation", "Decorator A operation", "Decorator B operation"
}
}
4:对于有1000个随机正整数的情况,可以使用贪心算法来实现分成两组,使得两组的总和尽量接近。
public class Main {
public static void main(String[] args) {
int[] nums = {1, 2, 3, 4, 5, 6, 45, 67, ...}; // 1000个随机正整数
Arrays.sort(nums); // 对数组进行排序
int sum1 = 0; // 第一组的总和
int sum2 = 0; // 第二组的总和
for (int i = nums.length - 1; i >= 0; i--) {
if (sum1 <= sum2) {
sum1 += nums[i];
} else {
sum2 += nums[i];
}
}
System.out.println("第一组的总和:" + sum1);
System.out.println("第二组的总和:" + sum2);
}
}
对于有100000个随机正整数的情况,可以使用动态规划算法来实现分成两组,使得两组的总和尽量接近。时间复杂度为O(n^2),空间复杂度为O(n)。
public class Main {
public static void main(String[] args) {
int[] nums = {1, 2, 3, 4, 5, 6, 45, 67, ...}; // 100000个随机正整数
int sum = 0; // 所有数的总和
for (int num : nums) {
sum += num;
}
int target = sum / 2; // 目标总和
boolean[][] dp = new boolean[nums.length + 1][target + 1];
dp[0][0] = true;
for (int i = 1; i <= nums.length; i++) {
for (int j = 0; j <= target; j++) {
dp[i][j] = dp[i - 1][j];
if (j >= nums[i - 1]) {
dp[i][j] = dp[i][j] || dp[i - 1][j - nums[i - 1]];
}
}
}
int sum1 = 0; // 第一组的总和
int sum2 = 0; // 第二组的总和
for (int i = nums.length; i >= 1; i--) {
if (dp[i][target] && (sum1 <= sum2 || sum1 - nums[i - 1] > sum2)) {
sum1 += nums[i - 1];
} else {
sum2 += nums[i - 1];
}
}
System.out.println("第一组的总和:" + sum1);
System.out.println("第二组的总和:" + sum2);
}
}
6:在服务器D上使用Java的多线程来实现以上逻辑,可以使用线程池来管理多线程的执行。
public class Main {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(3); // 创建一个固定大小为3的线程池
Future<Integer> task1 = executorService.submit(new Task1());
Future<Integer> task2 = executorService.submit(new Task2());
Future<Integer> task3 = executorService.submit(new Task3());
int result = 0;
try {
result = task1.get() + task2.get() + task3.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
System.out.println("任务4的结果:" + result);
executorService.shutdown(); // 关闭线程池
}
static class Task1 implements Callable<Integer> {
public Integer call() throws Exception {
// 任务1的逻辑
return 249;
}
}
static class Task2 implements Callable<Integer> {
public Integer call() throws Exception {
// 任务2的逻辑
return 354;
}
}
static class Task3 implements Callable<Integer> {
public Integer call() throws Exception {
// 任务3的逻辑
return 111;
}
}
}
7:该SQL能正确执行。
如果执行效率很差,可以通过以下优化来提高性能:
- 对表中的列建立索引,可以加快查询的速度。
- 优化SQL语句,避免使用子查询,使用连接查询或者其他更高效的方式实现相同的功能。
- 对表进行分区,可以分散数据并提高查询效率。
- 避免使用不必要的排序和聚合操作,只查询需要的列和行。
8:使用子查询方式实现该SQL可以改写为:
SELECT u.userid,
(SELECT SUM(m.value) FROM amount AS m WHERE m.orderid IN (SELECT t.orderid FROM trade AS t WHERE t.userid = u.userid AND DATE(t.trade_time) > DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND t.trade_type = 1)) AS sum_value,
(SELECT MAX(m.value) FROM amount AS m WHERE m.orderid IN (SELECT t.orderid FROM trade AS t WHERE t.userid = u.userid AND DATE(t.trade_time) > DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND t.trade_type = 1)) AS max_value,
(SELECT MIN(m.value) FROM amount AS m WHERE m.orderid IN (SELECT t.orderid FROM trade AS t WHERE t.userid = u.userid AND DATE(t.trade_time) > DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND t.trade_type = 1)) AS min_value,
(SELECT COUNT(t.id) FROM trade AS t WHERE t.userid = u.userid AND DATE(t.trade_time) > DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND t.trade_type = 1) / (SELECT COUNT(m.id) FROM amount AS m WHERE m.orderid IN (SELECT t.orderid FROM trade AS t WHERE t.userid = u.userid AND DATE(t.trade_time) > DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND t.trade_type = 1)) A
原文地址: http://www.cveoy.top/t/topic/hRlJ 著作权归作者所有。请勿转载和采集!