PAT (Advanced Level) Practise —— 1001. A+B Format (20)

本文介绍了如何使用C语言和Python实现将两个整数相加,并以标准格式输出结果,其中包括使用sprintf函数配合字符串操作的方法,以及利用printf函数直接控制输出格式的方式。

http://pat.zju.edu.cn/contests/pat-a-practise/1001


Calculate a + b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

Input

Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.

Output

For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

Sample Input
-1000000 9
Sample Output
-999,991

不想写判断语句,用sprintf函数和两个字符串完成:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

char outStr1[10];
char outStr2[10];

int main()
{
	int a, b, sum;
	scanf("%d%d", &a, &b);
	sum = a + b;
	if (sum < 0) printf("-");
	sprintf(outStr1, "%d", abs(sum));
	int len = strlen(outStr1);
	int i, j, k;
	for (i = len-1, j = 0, k = 1; i >= 0; i--, k++)
	{
		outStr2[j++] = outStr1[i];
		if (k%3 == 0 && i != 0)
		{
			outStr2[j++] = ',';
		}
	}
	int len2 = strlen(outStr2);
	for (i = len2-1; i >= 0; i--)
	{
		printf("%c", outStr2[i]);
	}
	printf("\n");

	return 0;
}

不用字符串,利用 printf函数控制输出格式:( http://alvenxuan.iteye.com/blog/1450533)

#include <stdio.h>

int main()
{
	int a, b, sum;
	while (scanf("%d%d", &a, &b) != EOF)
	{
		sum = a + b;
		if (sum < 0)
		{
			sum = -sum;
			printf("-");
		}
		if (sum >= 1000000)
		{
			printf("%d,%03d,%03d\n", sum/1000000, (sum/1000)%1000, sum%1000);
		}
		else if (sum >= 1000)
		{
			printf("%d,%03d\n", sum/1000, sum%1000);
		}
		else
		{
			printf("%d\n", sum);
		}
	}
	return 0;
}

使用Python:( http://www.cnblogs.com/szhang/archive/2013/01/16/2863511.html)

a,b = raw_input().split()
print format(int(a)+int(b),",")

Python在字符处理上真的很强大!(字符串的split函数默认以空格' '作为分割,format函数的第二个参数为","时自动将输出数字以","分割)





<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、付费专栏及课程。

余额充值