1079. Total Sales of Supply Chain (25)【树+搜索】——PAT (Advanced Level) Practise

本文介绍了一个关于供应链中从根供应商到零售商的总销售额计算的方法,包括建立树状结构进行搜索和使用递归函数DFS实现计算。

题目信息

1079. Total Sales of Supply Chain (25)

时间限制250 ms
内存限制65536 kB
代码长度限制16000 B
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone involved in moving a product from supplier to customer.

Starting from one root supplier, everyone on the chain buys products from one’s supplier in a price P and sell or distribute them in a price that is r% higher than P. Only the retailers will face the customers. It is assumed that each member in the supply chain has exactly one supplier except the root supplier, and there is no supply cycle.

Now given a supply chain, you are supposed to tell the total sales from all the retailers.

Input Specification:

Each input file contains one test case. For each case, the first line contains three positive numbers: N (<=10^5), the total number of the members in the supply chain (and hence their ID’s are numbered from 0 to N-1, and the root supplier’s ID is 0); P, the unit price given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then N lines follow, each describes a distributor or retailer in the following format:

Ki ID[1] ID[2] … ID[Ki]

where in the i-th line, Ki is the total number of distributors or retailers who receive products from supplier i, and is then followed by the ID’s of these distributors or retailers. Kj being 0 means that the j-th member is a retailer, then instead the total amount of the product will be given after Kj. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the total sales we can expect from all the retailers, accurate up to 1 decimal place. It is guaranteed that the number will not exceed 10^10.

Sample Input:
10 1.80 1.00
3 2 3 5
1 9
1 4
1 7
0 7
2 6 1
1 8
0 9
0 4
0 3
Sample Output:
42.4

解题思路

建树,搜索

AC代码

#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;
int a[100005];
vector<int> level[100005];
int n, tn, t;
double p, r;
double dfs(int root, int lv){
    double s = 0;
    if (level[root].size() == 0){
        s += a[root] * pow(1+r/100, lv) * p;
    }
    for (int i = 0; i < level[root].size(); ++i){
        s += dfs(level[root][i], lv + 1);
    }
    return s;
}
int main()
{
    scanf("%d%lf%lf", &n, &p, &r);
    for (int i = 0; i < n; ++i){
        scanf("%d", &tn);
        if (tn > 0){
            while (tn--){
                scanf("%d", &t);
                level[i].push_back(t);
            }
        }else{
            scanf("%d", &t);
            a[i] = t;
        }
    }
    printf("%.1f\n", dfs(0, 0));
    return 0;
}
<think>好的,用户遇到了Spring Boot项目中UserMapper Bean未找到的问题。我需要先分析可能的原因,然后给出解决方案。首先,UserMapper通常是一个MyBatis的Mapper接口,Bean未找到可能是因为没有被正确扫描到。 首先考虑组件扫描的问题。Spring Boot需要在主应用类上使用@MapperScan注解来指定Mapper接口的包路径,或者每个Mapper接口添加@Mapper注解。如果用户没有正确配置这些,会导致Bean无法被创建。 接下来,检查MyBatis的依赖是否正确。项目应该包含mybatis-spring-boot-starter,否则MyBatis相关的自动配置不会生效,导致Mapper无法被识别。 然后,确认UserMapper是否在Spring的组件扫描路径下。如果应用主类所在的包与Mapper接口不在同一包或子包下,又没有使用@MapperScan指定路径,Spring可能无法发现这些接口。 还要查看配置文件,比如application.properties,是否配置了mybatis.mapper-locations,指向XML文件的位置。如果XML文件存放位置不符合默认配置,这里需要显式指定。 另外,查看是否有多个数据源配置导致冲突。如果项目中配置了多个DataSource,可能需要使用@Primary注解指定主数据源,或者在配置类中明确指定SqlSessionFactoryBean和MapperScannerConfigurer。 最后,检查IDE的编译输出目录,确保Mapper接口的.class文件已经正确生成。有时候构建工具的问题可能导致类文件未生成,需要清理并重新编译项目。 综合这些可能的原因,应该逐步引导用户检查配置、依赖和路径设置,确保所有步骤都正确无误。同时,引用相关的文档和之前的文章,帮助用户更深入地理解每个配置项的作用。</think>在Spring Boot项目中遇到`UserMapper` Bean未找到的问题,通常由组件扫描、依赖配置或注解缺失引起。以下是逐步排查和解决方案: --- ### **1. 检查Mapper接口的注解与扫描配置** - **确保Mapper接口添加了`@Mapper`注解** 每个MyBatis Mapper接口需标注`@Mapper`,否则Spring无法识别: ```java import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapper { // SQL方法定义 } ``` - **或全局配置扫描路径** 在主应用类添加`@MapperScan`指定Mapper包路径[^3]: ```java @SpringBootApplication @MapperScan("com.example.mapper") // 替换为实际包名 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` --- ### **2. 验证MyBatis依赖是否正确** - **在`pom.xml`中添加MyBatis Starter依赖** 确保包含以下依赖以启用自动配置: ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.3.1</version> </dependency> ``` 如果未正确引入,Spring Boot不会自动配置Mapper扫描[^1]。 --- ### **3. 检查XML映射文件路径配置** - **在`application.properties`中指定XML位置** 如果使用XML定义SQL映射,需显式配置路径: ```properties mybatis.mapper-locations=classpath:mapper/*.xml ``` 确保XML文件位于`src/main/resources/mapper/`目录下。 --- ### **4. 排除多数据源配置冲突** - **若配置了多个数据源** 需在`@Configuration`类中手动指定`SqlSessionFactory`和`MapperScannerConfigurer`,避免自动配置失效: ```java @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); return factoryBean.getObject(); } ``` --- ### **5. 验证组件扫描范围** - **确保Mapper接口在Spring Boot主类同级或子包下** 默认扫描范围是主类所在包及其子包。若Mapper接口在其他路径,需通过`@ComponentScan`扩展扫描范围[^2]。 --- ### **6. 清理并重新构建项目** - **执行Maven/Gradle清理命令** 有时IDE缓存会导致编译不更新: ```bash mvn clean install ``` 或使用IDE的`Rebuild Project`功能。 --- ### **示例项目结构** ``` src/ ├── main/ │ ├── java/ │ │ └── com.example/ │ │ ├── Application.java # 主类 │ │ └── mapper/ │ │ └── UserMapper.java # Mapper接口 │ └── resources/ │ └── mapper/ │ └── UserMapper.xml # XML映射文件 ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值