(1)跳格子,现在有n个格子顺序排列,一只兔子从头开始跳,它一次可以跳1个格子,也可以跳2个格子……它也可以跳n个格子。求兔子跳进第n个格子总共有多少种跳法。
这是一个关于数列的题,可以这样去想一个存在递归的问题:
假如我要跳到第 n 个格子,那么就必须要在前一步跳到第 n−1 或者 n−2 个格子,如果说跳到第 n 个格子用的次数为 a_n,那么
a_n =a_(n−1)+ a_(n−2)。
注意,在这个地方有一个理解问题,为什么在等式的右侧没有系数:尽管 n−2 能够通过移动一个格子移动到 n−1 的位置,但是这样会导致计算的重复性,因此前面不加系数,直接可以默认为从 n−2个格子是跳一次移动两个格子到 n 的这个位置。
那么,根据这样的想法,去求解这个数学问题就很简答了,就直接是斐波那契数列了:对于前面的跳格子的跳法进行计算,后面的就可以根据前面两项进行求和计算。
格子数 1 2 3 4 5 6 7 8 9 10
跳法 1 2 3 5 8 13 21 34 55 89
当然,如果是步长设置最长为3,那么就相当于这个项的前面三项的值进行求和即可
格子数 1 2 3 4 5 6 7 8 9 10
跳法 1 2 4 7 13 24 44 81 149 274
以此类推,后面的格子可以根据前面的格子跳法来求和进行计算。
(2)4、编写一个线程程序、实现A线程先循环2次、接着B线程循环3次、再接着子线程循环2次、主线程循环3次、反复进行5次。
(3)将字符串”#name:jack;#age:24;#birthday:1995/10/24 00:00:00;”转换成Person对象。并使用jdbc将Person对象保存到mysql数据库,要求写出数据库建表脚本以及写出使用jdbc插入数据库的java代码。 说明:mysql数据库连接信息如下:
driverclass: com.mysql.jdbc.Driver
connection URL: jdbc:mysql://127.0.0.1:3306/test
username:root
password:000000
(4)有一串字符串String s = “xyxyxy”, 这个字符串可以看做由3个"xy"构成,即n=3,L = “xy”,s = n*L。现在要求编写一段程序,使用单例模式,输入任意字符串s,输出nL。如输入:xxxxx 输出 5x ,输入:xyxyx 输出:1xyxyx
(5)1、现有一个字符串String s = “DFSD713QWEQQQ4209PN5GHG”, 请编写一段程序将这个字符串中的数字截取出来并按照从小到大顺序输出。
(1)在这里插入代码片
(2)`package com.rimi.demo;
import java.util.logging.Level;
public class Share {
private int level = 1;
private int nextLevel = 1;
private int count = 2;
private int total = 0;
private int children = 3;
private int max = 4;
public Share(int total) {
this.total = total;
}
public synchronized void print(String name, int count, int level) {
try {
for (int i = 0;; i++) {
// 判断当前等级是否一致
while (this.level != level && !(this.level == this.children && level < this.level)) {
this.wait();
}
if (this.level == this.children) {
for (int j = 0; j < this.count; j++) {
while (this.nextLevel != level) {
this.wait();
}
// 如果等级等于3,则AB交叉进行
print(name, 1);
if (nextLevel == children - 1) {
nextLevel = 1;
} else {
nextLevel++;
}
this.notifyAll();
}
} else {
print(name, count);
}
if (this.level == level) {
if (this.level == this.max) {
this.level = 1;
this.total++;
} else {
this.level++;
}
} else {
if (this.children == this.level && level == this.children - 1) {
this.level++;
}
}
if (this.total == 5){
break;
}
this.notifyAll();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void print(String name, int count) {
for (int i = 0; i < count; i++) {
System.out.println(name + "执行了");
}
}
private int level = 1;
private int nextLevel = 1;
private int count = 2;
private int total = 0;
private int children = 3;
private int max = 4;
public Share(int total) {
this.total = total;
}
public synchronized void print(String name, int count, int level) {
try {
for (int i = 0;; i++) {
// 判断当前等级是否一致
while (this.level != level && !(this.level == this.children && level < this.level)) {
this.wait();
}
if (this.level == this.children) {
for (int j = 0; j < this.count; j++) {
while (this.nextLevel != level) {
this.wait();
}
// 如果等级等于3,则AB交叉进行
print(name, 1);
if (nextLevel == children - 1) {
nextLevel = 1;
} else {
nextLevel++;
}
this.notifyAll();
}
} else {
print(name, count);
}
if (this.level == level) {
if (this.level == this.max) {
this.level = 1;
this.total++;
} else {
this.level++;
}
} else {
if (this.children == this.level && level == this.children - 1) {
this.level++;
}
}
if (this.total == 5){
break;
}
this.notifyAll();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void print(String name, int count) {
for (int i = 0; i < count; i++) {
System.out.println(name + "执行了");
}
}
}(3)
package com.rimi.boot;
/**
- @author Zhou
- @date 2019/12/9 18:51
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class PUYuan {
//写一个pojo对象
class Person{
private String name;
private Integer age;
private String birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
//带参数构造
public Person(String name, Integer age, String birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
}
public void demo() throws ClassNotFoundException, SQLException {
try{
//1 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2 获取数据库连接
String url = "jdbc:mysql://127.0.0.1:3306/test";
String user = "root" ;
String password = "000000" ;
Connection conn = DriverManager.getConnection(url, user, password);
Person person = new Person("jack",24,"1995/10/24 00:00:00");
//3 写sql语句
String sql = "insert into person (name,age,birthday) values(?,?,?)" ;
PreparedStatement ps;
try {
ps = (PreparedStatement) conn.prepareStatement(sql);
ps.setString(1, person.getName());
ps.setInt(2, person.getAge());
ps.setString(3, person.getBirthday());
ps.executeUpdate();
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
conn.close();
}
}catch (ClassNotFoundException e) {
System.out.println("加载驱动失败!");
e.printStackTrace() ;
}
}
(4)
package com.rimi.boot;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.buf.StringUtils;
/**
-
@author Zhou
-
@date 2019/12/9 15:24
*/
public class App {
public static void main(String[] args) {
String s = “xxxxx”;
String result = “”;
Tool tool = Tool.getTool();
result = Tool.compress(s);
System.out.println(result);
}public static class Tool {
//创建一个对象
private static Tool tool;
//让构造函数为 private,这样该类就不会被实例化
private Tool(){}//获取唯一可用对象 public static Tool getTool() { if (tool == null) { tool = new Tool(); } return tool; } //编写用来解题的方法 public static String compress(String s){ String result = ""; if (StringUtils.isBlank(s)) { result = "你输入为空!"; } else if (s.contains(" ")){ result ="请不要输入空格!"; }else { int length = s.length(); System.out.println("length: "+length); int count = 0;
for (int i= 1;i<=length;i++){
String[] strings1 = s.split(s.substring(0,i));
if (strings1.length==0){
System.out.println(“切分到”+i);
count = i;
break;
}
}
result = length/count + s.substring(0,count);
}
return result;
}
}
}(5)
package com.rimi.boot;
/**
- @author Zhou
- @date 2019/12/9 19:06
*/
import java.util.Arrays;
public class Demo1 {
public static void main(String[] args) {
String str = “DFSD713QWEQQQ4209PN5GHG”;
//将字符串转成数组后进行处理
char[] arr = str.toCharArray();
//定义截取后的数组
char[] afterSrting = new char[arr.length];
int j = 0;
//将数字截取出来
for(int i = 0; i < arr.length; i++){
if(arr[i] >= ‘0’ && arr[i] <= ‘9’){
afterSrting[j] = arr[i];
j++;
}
}
//从小到大排列
Arrays.sort(afterSrting);
//afterSorting.length - j 除去字母所占的位置
for(int i = afterSrting.length - j; i < afterSrting.length; i++){
System.out.print(afterSrting[i]);
}
}
}
`
来源:CSDN
作者:@八宝周
链接:https://blog.csdn.net/m0_45109835/article/details/103465083