Redis 入门及应用 ( 三 ) 与springboot整合

news/2025/2/13 23:32:14/

4.redis 高级概念

4.1.事务

multi —标记一个事务块的开始
exec —执行事务块中的命令
discard — 取消事务,放弃执行事务块内的所有命令。
watch — 监视一个(或多个) key ,如果在事务执行之前这个(或这些) key 被其他命令所改动,那么事务将被打断。
unwatch — 取消 WATCH 命令对所有 key 的监视

127.0.0.1:6379> multi
OK
127.0.0.1:6379> set name li3
QUEUED
127.0.0.1:6379> get name
QUEUED
127.0.0.1:6379> ttl name
QUEUED
127.0.0.1:6379> expire name 20
QUEUED
127.0.0.1:6379> ttl name
QUEUED
127.0.0.1:6379> exec
1) OK
2) "li3"
3) (integer) -1
4) (integer) 1
5) (integer) 20

4.2.配置文件及登录权限

4.2.1.修改配置文件

redis服务器默认只能本机访问,不对外开放 ( bind 绑定 ip 默认是 bind 127.0.0.1, 增加 当前虚拟机的IP )
没有密码,需要设置密码才是安全的生产环境 ( requirepass 密码, 设置 登录密码 )
运行在前台 ( daemonize 改成 yes, 调整成守护进程 )
需要修改服务器配置,则修改redis.conf即可 ( 执行 vi redis.conf )

redis.conf 配置文件 根据安装方式不同 , 文件位置也不同

输入/requirepass 从上往下搜索 requirepass 单词

# 转到配置文件所在位置
[root@localhost ~]# cd /etc
# 打开 redis配置文件 redis.conf 的编辑界面
[root@localhost etc]# vi redis.conf# 在文件中 通过 /bind 找到 bind 配置, 增加虚拟机ip
# 或者 将 这行注释掉, 所有都可以访问
bind 192.168.0.93 127.0.0.1# 在文件中 通过 /requirepass 找到 登录密码设置
# requirepass foobared
# 解开注释, 调整成自己的密码
requirepass turing# 在文件中 通过 /daemonize 找到 设置守护进程
# 将 daemonize no  调整成 yes
daemonize yes# 退出 保存
:wq

修改 protected-mode yes 改为:protected-mode no

4.2.2.启动访问

redis-cli

​ -h ip 地址

​ -p 端口

-a 密码

# 在没有打开 redis服务时, 登录客户端 会报连接不上
[root@localhost ~]# redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> # 运行启动指令时, 要指明 配置文件及路径 
[root@localhost ~]# redis-server /etc/redis.conf # 启动客户端时, 可以通过  -h 指定 服务器 所在IP
[root@localhost ~]# redis-cli -h 192.168.0.93
# 但通过ping 不通, 没有权限
192.168.0.93:6379> ping
(error) NOAUTH Authentication required.# 通过 auth 指明 密码
192.168.0.93:6379> auth turing
OK
192.168.0.93:6379> ping
PONG

4.3.windows客户端

在 windows 环境 中 安装 客户端 RedisDesktopManager

关闭 虚拟机 防火墙 后 , 再启动redis服务器

# 关闭防火墙
[root@localhost ~]# systemctl stop firewalld.service 
# 禁止开机启动防火墙
[root@localhost ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
# 再启动 redis 服务器
[root@localhost ~]# redis-server /etc/redis.conf 

在 增加连接

RedisDesktopManager

在这里插入图片描述

Another Redis Desktop Manager
在这里插入图片描述

在 redis 保存 name -> hello

127.0.0.1:6379> set name hello
OK
# 在客户端 就可以 查看到,也可以修改
127.0.0.1:6379> get name
"hello"# 在这里也可以查看 客户端的修改内容
127.0.0.1:6379> get name
"hello world"

4.4.持久化数据

​ Redis 提供两种持久化机制 RDB(默认) 和 AOF 机制:

4.4.1.RDB:

是Redis DataBase缩写快照

RDB是Redis默认的持久化方式。按照一定的时间将内存的数据以快照的形式保存到硬盘中,对应产生的数据文件为dump.rdb。通过配置文件中的save参数来定义快照的周期。

在这里插入图片描述

优点:

  • 1、只有一个文件 dump.rdb,方便持久化。
  • 2、容灾性好,一个文件可以保存到安全的磁盘。
  • 3、性能最大化,fork 子进程来完成写操作,让主进程继续处理命令,所以是 IO 最大化。使用单独子进程来进行持久化,主进程不会进行任何 IO 操作,保证了 redis 的高性能
  • 4.相对于数据集大时,比 AOF 的启动效率更高。

缺点:

  • 1、数据安全性低。RDB 是间隔一段时间进行持久化,如果持久化之间 redis 发生故障,会发生数据丢失。所以这种方式更适合数据要求不严谨的时候)
  • 2、由于RDB是通过fork子进程来协助完成数据持久化工作的,因此,如果当数据集较大时,可能会导致整个服务器停止服务几百毫秒,甚至是1秒钟。

4.4.2.AOF:持久化

AOF持久化(即Append Only File持久化),则是将Redis执行的每次写命令记录到单独的日志文件中,当重启Redis会重新将持久化的日志中文件恢复数据。

当两种方式同时开启时,数据恢复Redis会优先选择AOF恢复。
在这里插入图片描述

优点:

  • 1、数据安全,aof 持久化可以配置 appendfsync 属性,有 always,每进行一次 命令操作就记录到 aof 文件中一次。
  • 2、通过 append 模式写文件,即使中途服务器宕机,可以通过 redis-check-aof 工具解决数据一致性问题。
  • 3、AOF 机制的 rewrite 模式。AOF 文件没被 rewrite 之前(文件过大时会对命令 进行合并重写),可以删除其中的某些命令(比如误操作的 flushall))

缺点:

  • 1、AOF 文件比 RDB 文件大,且恢复速度慢。
  • 2、数据集大的时候,比 rdb 启动效率低。

两种方式比较:

  • AOF文件比RDB更新频率高,优先使用AOF还原数据。
  • AOF比RDB更安全也更大
  • RDB性能比AOF好
  • 如果两个都配了优先加载AOF

4.4.3.RDB 指令

不通过 配置文件启动 服务, dump.rdb 在 启动的文件夹

[root@localhost ~]# ls
dump.rdb

使用 配置文件 启动时 , dump.rdb 默认在 /var/lib/redis 文件夹下, 这是配置在 配置文件里

备份时会在 dir 属性指定的路径下存储一个 dump.rdb 文件,

还原: 在 dump.rdb 文件所在目录启动服务即可.

# 获取存储文件的位置127.0.0.1:6379> config get dir
1) "dir"
2) "/var/lib/redis"# 修改存储文件的位置
127.0.0.1:6379> config set dir /root/temp#  同步备份 save 异步备份  bgsave 
127.0.0.1:6379> save
OK

redis-check-rdb dump.rdb 检查备份文件

[root@localhost ~]# redis-check-rdb dump.rdb 
[offset 0] Checking RDB file dump.rdb
[offset 27] AUX FIELD redis-ver = '3.2.12'
[offset 41] AUX FIELD redis-bits = '64'
[offset 53] AUX FIELD ctime = '1624846372'
[offset 68] AUX FIELD used-mem = '757288'
[offset 70] Selecting DB ID 0
[offset 530] Checksum OK
[offset 530] \o/ RDB looks OK! \o/
[info] 13 keys read
[info] 0 expires
[info] 0 already expired

4.5. 其它常用命令

(1)连接
auth 验证密码
select index 切换数据库 redis 有 16个数据库, 默认用 db0

​ quit 断开连接
​ ping 判断客户端是否与服务端连通

(2)服务
dbsize 当前数据库 key 的数量
flushdb 删除当前数据库的所有 key
flushall 删除所有数据库的所有 key
info 服务运行信息
time 查看时间

​ shutdown 关闭服务

5.redis与springboot整合

5.1.基本说明

5.1.1.两种客户端

Springboot2 中使用 redis,有两个客户端可供选择,他们是 Jedis 和 lettuce, 且Springboot2 默认采用 lettuce 作为客户端.
Jedis 和 lettuce 的区别?
jedis 在多线程环境下是非线程安全的,使用了 jedis pool 连接池,为每个 Jedis 实例增加物理连接。
Lettuce 的连接是基于 Netty 的,连接实例(StatefulRedisConnection)可以在多个线程间并发访问。

Jedis:是Redis的Java实现客户端,提供了比较全面的Redis命令的支持,使用阻塞的I/O,程序流需要等到sockets处理完I/O才能执行,不支持异步。Jedis客户端实例不是线程安全的,所以需要通过连接池来使用Jedis。

Redisson:Redisson实现了分布式和可扩展的Java数据结构,和Jedis相比,功能较为简单,不支持字符串操作,不支持排序、事务、管道、分区等Redis特性。Redisson的宗旨是促进使用者对Redis的关注分离,从而让使用者能够将精力更集中地放在处理业务逻辑上。基于Netty框架的事件驱动的通信层,其方法调用是异步的。Redisson的API是线程安全的。

Lettuce:高级Redis客户端,用于线程安全同步,异步和响应使用,支持集群,Sentinel,管道和编码器,主要在一些分布式缓存框架上使用比较多。基于Netty框架的事件驱动的通信层,其方法调用是异步的。Lettuce的API是线程安全的。

5.2.配置redis

5.2.1.依赖包

使用 Jedis 客户端

        <!--springboot 1.X 是使用 jedis 依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><exclusions><exclusion><groupId>io.lettuce</groupId><artifactId>lettuce-core</artifactId></exclusion></exclusions></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId></dependency>

使用 lettuce 客户端

<!-- springboot 2.X redis 依赖-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency><!--连接池--><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency>

5.2.2. 配置

使用 jedis 客户端

spring.redis.host=192.168.0.93
spring.redis.port=6379
spring.redis.password=turing
spring.redis.timeout=3000
spring.redis.database=0
#最大连接数,默认 8
spring.redis.jedis.pool.max-active=30
#最小空闲数,默认 0
spring.redis.jedis.pool.min-idle=0
#最大空闲数,默认 8
spring.redis.jedis.pool.max-idle=8
#等待时间,负值表示没有限制
spring.redis.jedis.pool.max-wait=-1

使用 lettuce 客户端

spring.redis.host=192.168.0.93
spring.redis.port=6379
spring.redis.password=turing
spring.redis.timeout=3000
spring.redis.database=0
#关闭超时时间
spring.redis.lettuce.shutdown-timeout=100
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=30
#连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=0
#连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=8
#连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.lettuce.pool.max-wait=-1

5.3. redis 操作类

@Autowired
private RedisTemplate redisTemplate;

5.3.1. 得到不同数据类型的操作类

  • ValueOperations:简单K-V操作
  • SetOperations:set类型数据操作
  • ZSetOperations:zset类型数据操作
  • HashOperations:针对map类型的数据操作
  • ListOperations:针对list类型的数据操作

5.3.2.RedisTemplate 基本操作

@Autowired
private RedisTemplate redisTemplate;// 删除key
public void delete(String key){redisTemplate.delete(key);
}
// 删除多个key
public void deleteKey (String ...keys){redisTemplate.delete(keys);
}
// 指定key的失效时间
public void expire(String key,long time){redisTemplate.expire(key,time,TimeUnit.MINUTES);
}
// 根据key获取过期时间
public long getExpire(String key){Long expire = redisTemplate.getExpire(key);return expire;
}
// 判断key是否存在
public boolean hasKey(String key){return redisTemplate.hasKey(key);
}

5.3.3.String类型相关操作

5.3.3.1.添加缓存

//1、通过redisTemplate设置值
redisTemplate.boundValueOps("StringKey").set("StringValue");
redisTemplate.boundValueOps("StringKey").set("StringValue",1, TimeUnit.MINUTES);//2、通过BoundValueOperations设置值
BoundValueOperations stringKey = redisTemplate.boundValueOps("StringKey");
stringKey.set("StringVaule");
stringKey.set("StringValue",1, TimeUnit.MINUTES);//3、通过ValueOperations设置值
ValueOperations ops = redisTemplate.opsForValue();
ops.set("StringKey", "StringVaule");
ops.set("StringValue","StringVaule",1, TimeUnit.MINUTES);

5.3.3.2.设置过期时间(单独设置)

redisTemplate.boundValueOps("StringKey").expire(1,TimeUnit.MINUTES);
redisTemplate.expire("StringKey",1,TimeUnit.MINUTES);

5.3.3.3.获取缓存值

//1、通过redisTemplate设置值
String str1 = (String) redisTemplate.boundValueOps("StringKey").get();//2、通过BoundValueOperations获取值
BoundValueOperations stringKey = redisTemplate.boundValueOps("StringKey");
String str2 = (String) stringKey.get();//3、通过ValueOperations获取值
ValueOperations ops = redisTemplate.opsForValue();
String str3 = (String) ops.get("StringKey");

5.3.3.4.删除key

Boolean result = redisTemplate.delete("StringKey");

5.3.3.5.顺序递增

redisTemplate.boundValueOps("StringKey").increment(3L);

5.3.3.6.顺序递减

redisTemplate.boundValueOps("StringKey").increment(-3L);

5.3.3.7.setNX/setEX

Boolean lock = redisTemplate.opsForValue().setIfAbsent("lock", "1111", 300, TimeUnit.SECONDS);

5.3.4.Hash类型相关操作

5.3.4.1.添加缓存

//1、通过redisTemplate设置值
redisTemplate.boundHashOps("HashKey").put("SmallKey", "HashVaue");//2、通过BoundValueOperations设置值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
hashKey.put("SmallKey", "HashVaue");//3、通过ValueOperations设置值
HashOperations hashOps = redisTemplate.opsForHash();
hashOps.put("HashKey", "SmallKey", "HashVaue");

5.3.4.2.设置过期时间(单独设置)

redisTemplate.boundValueOps("HashKey").expire(1,TimeUnit.MINUTES);
redisTemplate.expire("HashKey",1,TimeUnit.MINUTES);

5.3.4.3.添加一个Map集合

HashMap<String, String> hashMap = new HashMap<>();
redisTemplate.boundHashOps("HashKey").putAll(hashMap );

5.3.4.5.提取所有的小key

//1、通过redisTemplate获取值
Set keys1 = redisTemplate.boundHashOps("HashKey").keys();//2、通过BoundValueOperations获取值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
Set keys2 = hashKey.keys();//3、通过ValueOperations获取值
HashOperations hashOps = redisTemplate.opsForHash();
Set keys3 = hashOps.keys("HashKey");

5.3.4.6.提取所有的value值

//1、通过redisTemplate获取值
List values1 = redisTemplate.boundHashOps("HashKey").values();//2、通过BoundValueOperations获取值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
List values2 = hashKey.values();//3、通过ValueOperations获取值
HashOperations hashOps = redisTemplate.opsForHash();
List values3 = hashOps.values("HashKey");

5.3.4.7.根据key提取value值

//1、通过redisTemplate获取
String value1 = (String) redisTemplate.boundHashOps("HashKey").get("SmallKey");//2、通过BoundValueOperations获取值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
String value2 = (String) hashKey.get("SmallKey");//3、通过ValueOperations获取值
HashOperations hashOps = redisTemplate.opsForHash();
String value3 = (String) hashOps.get("HashKey", "SmallKey");

5.3.4.8.获取所有的键值对集合

//1、通过redisTemplate获取
Map entries = redisTemplate.boundHashOps("HashKey").entries();//2、通过BoundValueOperations获取值
BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
Map entries1 = hashKey.entries();//3、通过ValueOperations获取值
HashOperations hashOps = redisTemplate.opsForHash();
Map entries2 = hashOps.entries("HashKey");

5.3.4.9.删除

//删除小key
redisTemplate.boundHashOps("HashKey").delete("SmallKey");
//删除大key
redisTemplate.delete("HashKey");

5.3.4.10.判断Hash中是否含有该值

Boolean isEmpty = redisTemplate.boundHashOps("HashKey").hasKey("SmallKey");

5.3.5.Set类型相关操作

5.3.5.1.添加Set缓存(值可以是一个,也可是多个)

//1、通过redisTemplate设置值
redisTemplate.boundSetOps("setKey").add("setValue1", "setValue2", "setValue3");//2、通过BoundValueOperations设置值
BoundSetOperations setKey = redisTemplate.boundSetOps("setKey");
setKey.add("setValue1", "setValue2", "setValue3");//3、通过ValueOperations设置值
SetOperations setOps = redisTemplate.opsForSet();
setOps.add("setKey", "SetValue1", "setValue2", "setValue3");

5.3.5.2.设置过期时间(单独设置)

redisTemplate.boundValueOps("setKey").expire(1,TimeUnit.MINUTES);
redisTemplate.expire("setKey",1,TimeUnit.MINUTES);

5.3.5.3.根据key获取Set中的所有值

//1、通过redisTemplate获取值
Set set1 = redisTemplate.boundSetOps("setKey").members();//2、通过BoundValueOperations获取值
BoundSetOperations setKey = redisTemplate.boundSetOps("setKey");
Set set2 = setKey.members();//3、通过ValueOperations获取值
SetOperations setOps = redisTemplate.opsForSet();
Set set3 = setOps.members("setKey");

5.3.5.4.根据value从一个set中查询,是否存在

Boolean isEmpty = redisTemplate.boundSetOps("setKey").isMember("setValue2");

5.3.5.5.获取Set缓存的长度

Long size = redisTemplate.boundSetOps("setKey").size();

5.3.5.6.移除指定的元素

Long result1 = redisTemplate.boundSetOps("setKey").remove("setValue1");

5.3.6. LIST类型相关操作

5.3.6.1.添加缓存

//1、通过redisTemplate设置值
redisTemplate.boundListOps("listKey").leftPush("listLeftValue1");
redisTemplate.boundListOps("listKey").rightPush("listRightValue2");//2、通过BoundValueOperations设置值
BoundListOperations listKey = redisTemplate.boundListOps("listKey");
listKey.leftPush("listLeftValue3");
listKey.rightPush("listRightValue4");//3、通过ValueOperations设置值
ListOperations opsList = redisTemplate.opsForList();
opsList.leftPush("listKey", "listLeftValue5");
opsList.rightPush("listKey", "listRightValue6");

5.3.6.2.将List放入缓存

ArrayList<String> list = new ArrayList<>();
redisTemplate.boundListOps("listKey").rightPushAll(list);
redisTemplate.boundListOps("listKey").leftPushAll(list);

5.3.6.3.设置过期时间(单独设置)

redisTemplate.boundValueOps("listKey").expire(1,TimeUnit.MINUTES);
redisTemplate.expire("listKey",1,TimeUnit.MINUTES);

5.3.6.4.获取List缓存全部内容(起始索引,结束索引)

List listKey1 = redisTemplate.boundListOps("listKey").range(0, 10); // 0, -1 是全部

5.3.6.5.从左或从右弹出一个元素

String listKey2 = (String) redisTemplate.boundListOps("listKey").leftPop();  //从左侧弹出一个元素
String listKey3 = (String) redisTemplate.boundListOps("listKey").rightPop(); //从右侧弹出一个元素

5.3.6.6.根据索引查询元素

String listKey4 = (String) redisTemplate.boundListOps("listKey").index(1);

5.3.6.7.获取List缓存的长度

Long size = redisTemplate.boundListOps("listKey").size();

5.3.6.8.根据索引修改List中的某条数据(key,索引,值)

redisTemplate.boundListOps("listKey").set(3L,"listLeftValue3");

5.3.6.9.移除N个值为value(key,移除个数,值)

redisTemplate.boundListOps("listKey").remove(3L,"value");

5.3.7.Zset类型的相关操作

5.3.7.1.向集合中插入元素,并设置分数

//1、通过redisTemplate设置值
redisTemplate.boundZSetOps("zSetKey").add("zSetVaule", 100D);//2、通过BoundValueOperations设置值
BoundZSetOperations zSetKey = redisTemplate.boundZSetOps("zSetKey");
zSetKey.add("zSetVaule", 100D);//3、通过ValueOperations设置值
ZSetOperations zSetOps = redisTemplate.opsForZSet();
zSetOps.add("zSetKey", "zSetVaule", 100D);

5.3.7.2.向集合中插入多个元素,并设置分数

DefaultTypedTuple<String> p1 = new DefaultTypedTuple<>("zSetVaule1", 2.1D);
DefaultTypedTuple<String> p2 = new DefaultTypedTuple<>("zSetVaule2", 3.3D);
redisTemplate.boundZSetOps("zSetKey").add(new HashSet<>(Arrays.asList(p1,p2)));

5.3.7.3.按照排名先后(从小到大)打印指定区间内的元素, -1为打印全部

Set<String> range = redisTemplate.boundZSetOps("zSetKey").range(0, -1);

5.3.7.4.获得指定元素的分数

Double score = redisTemplate.boundZSetOps("zSetKey").score("zSetVaule");

5.3.7.5.返回集合内的成员个数

Long size = redisTemplate.boundZSetOps("zSetKey").size();

5.3.7.6.返回集合内指定分数范围的成员个数(Double类型)

Long count = redisTemplate.boundZSetOps("zSetKey").count(0D, 2.2D);

5.3.7.7.返回集合内元素在指定分数范围内的排名(从小到大)

Set byScore = redisTemplate.boundZSetOps("zSetKey").rangeByScore(0D, 2.2D);

5.3.7.8.带偏移量和个数,(key,起始分数,最大分数,偏移量,个数)

Set<String> ranking2 = redisTemplate.opsForZSet().rangeByScore("zSetKey", 0D, 2.2D 1, 3);

5.3.7.9.返回集合内元素的排名,以及分数(从小到大)

Set<TypedTuple<String>> tuples = redisTemplate.boundZSetOps("zSetKey").rangeWithScores(0L, 3L);for (TypedTuple<String> tuple : tuples) {System.out.println(tuple.getValue() + " : " + tuple.getScore());}ss

5.3.7.10.返回指定成员的排名

//从小到大
Long startRank = redisTemplate.boundZSetOps("zSetKey").rank("zSetVaule");
//从大到小
Long endRank = redisTemplate.boundZSetOps("zSetKey").reverseRank("zSetVaule");

5.3.7.11.从集合中删除指定元素

redisTemplate.boundZSetOps("zSetKey").remove("zSetVaule");

5.3.7.12.删除指定索引范围的元素(Long类型)

redisTemplate.boundZSetOps("zSetKey").removeRange(0L,3L);

5.3.7.13.删除指定分数范围内的元素(Double类型)

redisTemplate.boundZSetOps("zSetKey").removeRangeByScorssse(0D,2.2D);

5.3.7.14)、为指定元素加分(Double类型)

Double score = redisTemplate.boundZSetOps("zSetKey").incrementScore("zSetVaule",1.1D);

5.4.调用脚本

if redis.call("get",KEYS[1]) == ARGV[1]
thenreturn redis.call("del",KEYS[1])
elsereturn 0
end
String script = "if redis.call(\"get\",KEYS[1]) == ARGV[1]\n" +"then\n" +"    return redis.call(\"del\",KEYS[1])\n" +"else\n" +"    return 0\n" +"end";
redisTemplate.execute(new DefaultRedisScript<Integer>(script, Integer.class), Arrays.asList("lock"), uuid);

http://www.ppmy.cn/news/573931.html

相关文章

程序解:现有3L容器和5L容器各一个,问如何量出4L水(水无限)

穷举所有的可能性&#xff0c;从杯子里面水变化角度来讲&#xff0c;每个状态到下一个状态只有6种行为&#xff1a;3L杯子装满&#xff0c;5L杯子装满&#xff0c;3L倒空&#xff0c;5L倒空&#xff0c;3L倒到5L&#xff0c;5L倒到3L。最终结果如果5L的里面有4L水则得到一个解。…

python实现无刻度3升水和5升水准确得到4升水的代码步骤

瓶子灌水问题: 两个没有刻度的杯子&#xff0c;一个容积是3L,一个容积是5L,怎么计量出4L水&#xff1f; 进阶1: 两个没有刻度的杯子&#xff0c;一个容积是A,一个容积是B,最终可计量水的体积。 c 跟A, B 什么关系&#xff1f; A 3; B 5 C {1, 2, 3, 4, 5} A 2; B 4 C {2,…

逻辑思维面试题-河里的水是无限的,现在有两个水桶分别是5L,6L,问如何从河里取3L水?

河里的水是无限的,现在有两个水桶分别是5L,6L,问如何从河里取3L水? 解1 设&#xff1a; A为5L 。 B为6L。 解&#xff1a; &#xff08;1&#xff09;5L的装满,全倒向6L中&#xff1b;此时B中有5L水&#xff08;空1L&#xff09;. &#xff08;2&#xff09;5L的再装满,再倒向…

C语言再学习 -- 文件

文件是什么 一个文件&#xff08;file&#xff09;通常就是磁盘上的一段命名的存储区。C 将文件看成是连续的字节序列&#xff0c;其中每一个字节都可以单独地读取。 二进制和文本模式 1、在windows系统中&#xff0c;文本模式下&#xff0c;文件以"\r\n"代表换行。若…

L3-Day13

⏰打卡时间&#xff1a;9月12日&#xff08;周四&#xff09; 6:00-17:00训练技巧顺序&#xff1a;【完全听写法】️【车轮法】️【影子跟读法】⏱【练习时间】30 mins句1: But at one time, some businesses refused to employ people who were on a blacklist for belonging …

YoloV4学习笔记5_yolov4-tiny(win10+VC2015+opencv4.4+yolov4)

YoloV4学习笔记5_yolov4-tiny&#xff08;win10VC2015opencv4.4yolov4&#xff09; 对于cpu识别或者资源不太充足的设备&#xff0c;yolov4的完整版太慢了。 yolov4-tiny相当于yolov4的缩小版本&#xff0c;速度比完整版快10倍&#xff0c;当然也牺牲了一些准确度。 》YOLOv4-…

shell3

http://note.youdao.com/noteshare?idae8785c0aff0587e91a01fd504f10107&subCB3C2FAC835845F6BAA8CFBF4BBED2FF

3ll4c语言,3l英语

概括&#xff1a;这道题是王嗡行同学的课后数学练习题&#xff0c;主要是关于3l英语&#xff0c;指导老师为訾老师。此书可以说是《新概念英语的青少年版》。全套教材按听、说、读、写顺序安排&#xff0c;实际运用英语的技能训练重于英语知识的学习。这一点既是这套教材的特点…