PAT甲级 1018 Public Bike Management (30分)

There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Public Bike Management Center (PBMC) keeps monitoring the real-time capacity of all the stations. A station is said to be in perfect condition if it is exactly half-full. If a station is full or empty, PBMC will collect or send bikes to adjust the condition of that station to perfect. And more, all the stations on the way will be adjusted as well. ...

April 12, 2020 · 5 min

Dubbo微服务影院系列(9):Dubbo服务开发(支付模块开发)

章节概要 完成支付模块业务开发 Dubbo 特性学习:隐式参数、参数验证等 Dubbo 本地存根 本地存根类似于 Dubbo 的静态代理 Dubbo 会在客户端生成一个代理,处理部分业务 Stub 必须有可传入 Proxy 的构造函数 官方文档:https://dubbo.apache.org/zh-cn/docs/user/demos/local-stub.html 远程服务后,客户端通常只剩下接口,而实现全在服务器端,但提供方有些时候想在客户端也执行部分逻辑,比如:做 ThreadLocal 缓存,提前验证参数,调用失败后伪造容错数据等等,此时就需要在 API 中带上 Stub,客户端生成 Proxy 实例,会把 Proxy 通过构造函数传给 Stub,然后把 Stub 暴露给用户,Stub 可以决定要不要去调 Proxy。 在 spring 配置文件中按以下方式配置: <dubbo:service interface="com.foo.BarService" stub="true" /> 或 <dubbo:service interface="com.foo.BarService" stub="com.foo.BarServiceStub" /> 提供 Stub 的实现: package com.foo; public class BarServiceStub implements BarService { private final BarService barService; // 构造函数传入真正的远程代理对象 public BarServiceStub(BarService barService){ this.barService = barService; } public String sayHello(String name) { // 此代码在客户端执行, 你可以在客户端做ThreadLocal本地缓存,或预先验证参数是否合法,等等 try { return barService.sayHello(name); } catch (Exception e) { // 你可以容错,可以做任何AOP拦截事项 return "容错数据"; } } } Stub 必须有可传入 Proxy 的构造函数。 在 interface 旁边放一个 Stub 实现,它实现 BarService 接口,并有一个传入远程 BarService 实例的构造函数 Dubbo 本地伪装 本地伪装是本地存根的一个子集 通常会使用本地伪装处理服务降级 建议本地伪装在客户端实现 官方文档:https://dubbo.apache.org/zh-cn/docs/user/demos/local-mock.html ...

April 11, 2020 · 2 min

PAT考场 Dev C++ 5.10 配置

根据往年 (2019) 年浙大软件学院的考研复试公告,如果今年 (2020) 是线下复试,且考场没有变化的话,机试应该还是在 浙大紫金港校区(如下图)。 浙大软院招生通知:http://www.cst.zju.edu.cn/2019/0307/c36207a1478348/page.htm 从 PAT 官网可以找到紫金港的机房配置: PAT 考点联盟:https://www.patest.cn/location 我本人选择使用 Dev C++ 5.10 版本,考试那天在考场需要先对其作以下配置(当然也许已经配置好了): 打开产生调试信息 在 “工具(Tools)” -> “编译选项(Compiler Options)" 中: 点击 “代码生成/优化(Settings)” 选项卡中的 "连接器(Linker)" 选项卡,将 "产生调试信息(Generate debugging information)" 选项改为 “Yes”。 这样,就可以愉快的 Debug 了~ 添加 C++ 11 支持 由于 PAT 的 OJ 是支持 C++ 11 的,故最好打开 C++ 11 支持。 方法一(推荐) 在 “工具(Tools)” -> “编译选项(Compiler Options)" 中,“代码生成/优化(Settings)” 选项卡下的 “代码生成(Code Generation)” 选项卡中,将 “语言标准(Language Standard)(-std)” 设置为 ISO C++11 即可。 ...

April 11, 2020 · 1 min

PAT甲级 1021 Deepest Root (25分)

A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root. Input Specification: Each input file contains one test case. For each case, the first line contains a positive integer N (≤10^4) which is the number of nodes, and hence the nodes are numbered from 1 to N. Then N−1 lines follow, each describes an edge by given the two adjacent nodes’ numbers. ...

April 10, 2020 · 2 min

PAT甲级 1013 Battle Over Cities (25分)

It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly. ...

April 9, 2020 · 2 min

PAT甲级 1032 Sharing (25分)

To store English words, one method is to use linked lists and store a word letter by letter. To save some space, we may let the words share the same sublist if they share the same suffix. For example, loading and being are stored as showed in Figure 1. You are supposed to find the starting position of the common suffix (e.g. the position of i in Figure 1). ...

April 7, 2020 · 2 min

PAT甲级 1056 Mice and Rice (25分)

Mice and Rice is the name of a programming contest in which each programmer must write a piece of code to control the movements of a mouse in a given map. The goal of each mouse is to eat as much rice as possible in order to become a FatMouse. First the playing order is randomly decided for N P programmers. Then every N G programmers are grouped in a match. The fattest mouse in a group wins and enters the next turn. All the losers in this turn are ranked the same. Every N G winners are then grouped in the next match until a final winner is determined. ...

April 6, 2020 · 3 min

PAT甲级 1100 Mars Numbers (20分)

People on Mars count their numbers with base 13: Zero on Earth is called “tret” on Mars. The numbers 1 to 12 on Earth is called “jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec” on Mars, respectively. For the next higher digit, Mars people name the 12 numbers as “tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou”, respectively. For examples, the number 29 on Earth is called “hel mar” on Mars; and “elo nov” on Mars corresponds to 115 on Earth. In order to help communication between people from these two planets, you are supposed to write a program for mutual translation between Earth and Mars number systems. ...

April 6, 2020 · 2 min

Dubbo微服务影院系列(8):Dubbo服务开发(订单模块开发)

章节概要 完成订单模块业务开发 完成限流和熔断、降级相关内容 Dubbo 特性之分组、聚合和版本控制 订单模块问题 订单模块的横向和纵向分表如何解决? 服务限流如何处理? 服务熔断和降级? 如何保证多版本的蓝绿上线? 横向拆分和纵向拆分 Dubbo 服务分组 官方文档:https://dubbo.apache.org/zh-cn/docs/user/demos/service-group.html 当一个接口有多种实现时,可以用 group 区分。 服务 <dubbo:service group="feedback" interface="com.xxx.IndexService" /> <dubbo:service group="member" interface="com.xxx.IndexService" /> 引用 <dubbo:reference id="feedbackIndexService" group="feedback" interface="com.xxx.IndexService" /> <dubbo:reference id="memberIndexService" group="member" interface="com.xxx.IndexService" /> 任意组: <dubbo:reference id="barService" interface="com.foo.BarService" group="*" /> Dubbo 分组聚合 https://dubbo.apache.org/zh-cn/docs/user/demos/group-merger.html 按组合并返回结果,比如菜单服务,接口一样,但有多种实现,用 group 区分,现在消费方需从每种 group 中调用一次返回结果,合并结果返回,这样就可以实现聚合菜单项。 配置 搜索所有分组 <dubbo:reference interface="com.xxx.MenuService" group="*" merger="true" /> 合并指定分组 <dubbo:reference interface="com.xxx.MenuService" group="aaa,bbb" merger="true" /> 指定方法合并结果,其它未指定的方法,将只调用一个 Group <dubbo:reference interface="com.xxx.MenuService" group="*"> <dubbo:method name="getMenuItems" merger="true" /> </dubbo:reference> 某个方法不合并结果,其它都合并结果 <dubbo:reference interface="com.xxx.MenuService" group="*" merger="true"> <dubbo:method name="getMenuItems" merger="false" /> </dubbo:reference> 指定合并策略,缺省根据返回值类型自动匹配,如果同一类型有两个合并器时,需指定合并器的名称 <dubbo:reference interface="com.xxx.MenuService" group="*"> <dubbo:method name="getMenuItems" merger="mymerge" /> </dubbo:reference> 指定合并方法,将调用返回结果的指定方法进行合并,合并方法的参数类型必须是返回结果类型本身 <dubbo:reference interface="com.xxx.MenuService" group="*"> <dubbo:method name="getMenuItems" merger=".addAll" /> </dubbo:reference> Dubbo 多版本 当一个接口实现,出现不兼容升级时,可以用版本号过渡,版本号不同的服务相互间不引用。 可以按照以下的步骤进行版本迁移: 在低压力时间段,先升级一半提供者为新版本 再将所有消费者升级为新版本 然后将剩下的一半提供者升级为新版本 配置 老版本服务提供者配置: <dubbo:service interface="com.foo.BarService" version="1.0.0" /> 新版本服务提供者配置: <dubbo:service interface="com.foo.BarService" version="2.0.0" /> 老版本服务消费者配置: <dubbo:reference id="barService" interface="com.foo.BarService" version="1.0.0" /> 新版本服务消费者配置: ...

April 5, 2020 · 2 min

PAT甲级 1060 Are They Equal (25分)

If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123×105 with simple chopping. Now given the number of significant digits on a machine and two float numbers, you are supposed to tell if they are treated equal in that machine. Input Specification: Each input file contains one test case which gives three numbers N, A and B, where N (<100) is the number of significant digits, and A and B are the two float numbers to be compared. Each float number is non-negative, no greater than 10100, and that its total digit number is less than 100. ...

April 4, 2020 · 2 min