华中农业大学第四届程序设计大赛网络同步赛 Arithmetic Sequence

题目链接:http://acm.hzau.edu.cn/problem.php?cid=1009&pid=9

Problem J: Arithmetic Sequence

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 1823   Solved: 317
[ Submit][ Status][ Web Board]

Description

    Giving a number sequence A with length n, you should choosing m numbers from A(ignore the order) which can form an arithmetic sequence and make m as large as possible.

Input

   There are multiple test cases. In each test case, the first line contains a positive integer n. The second line contains n integers separated by spaces, indicating the number sequence A. All the integers are positive and not more than 2000. The input will end by EOF.

Output

   For each test case, output the maximum  as the answer in one line.

Sample Input

5
1 3 5 7 10
8
4 2 7 11 3 1 9 5

Sample Output

4
6

HINT

   In the first test case, you should choose 1,3,5,7 to form the arithmetic sequence and its length is 4.


   In the second test case, you should choose 1,3,5,7,9,11 and the length is 6.



题意很简单,给我们一个序列,问我们从这个序列中选出一些数能构成的最长的等差序列的长度为多少。

这个问题我们用DP来解决,首先因为我们取数是不用关注它的顺序的,所以我们就可以先排个序,然后我们用dp[i][j]表示取到第i个数的时候间距为j的长度的等差数列最长的长度,所以我们有状态转移方程dp[i][dif] = dp[j][dif]+1

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 2000+5;
int a[maxn];
int dp[maxn][maxn];
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=0; i<n; i++) scanf("%d",&a[i]);
		if(n <= 1)
		{
			printf("%d\n",n);
			continue;
		}
		sort(a,a+n);
		int ans = 1;
		for(int i=0; i<maxn; i++)
			for(int j=0; j<maxn; j++)
				dp[i][j] = 1;
		for(int i=1; i<n; i++)
		{
			for(int j=0; j<i; j++)
			{
				int dif = a[i]-a[j];
				dp[i][dif] = dp[j][dif]+1;
				ans = max(ans, dp[i][dif]);
			}
		}
		//for(int i=0; i<n; i++) printf("%d ",dp[i][0]);
		printf("%d\n",ans);
	}
}


### 关于华中农业大学 Java 期末考试的相关资料 #### 题目结构分析 通常情况下,Java 课程的期末考试会覆盖基础语法、面向对象编程、集合框架以及异常处理等内容。根据已知的信息[^1],可以推测华中农业大学的 Java 考试可能涉及 `try-catch` 块的应用场景、自定义异常的设计与实现等方面。 以下是基于已有引用内容整理的一些典型题目方向: --- #### 可能的试题类型及其解析 ##### 一、填空题 此类题目主要考察学生对基本概念的理解程度。例如: - **问题**: 抽象类中的方法默认具有何种访问权限? - **答案**: 默认为 protected 类型,除非显式声明其他访问修饰符[^2]。 ##### 二、判断题 用于测试考生对于特定规则的记忆准确性。 - **示例**: abstract 和 final 是否能够同时修饰一个类? - **解答**: 不可以,因为两者的语义存在冲突——abstract 表明该类需被继承扩展,而 final 则禁止任何派生操作[^2]。 ##### 三、程序改错题 提供一段含有错误代码片段让学生找出其中的问题所在。 ```java // 错误示范 class Test { public static void main(String[] args){ try{ int result = divide(10, 0); System.out.println(result); } catch (ArithmeticException e){ System.err.println(e.getMessage()); } } private static int divide(int a,int b)throws ArithmeticException{ // 这里抛出了异常却未实际触发 return a /b; } } ``` 上述例子中虽然函数签名表明可能会引发算术溢出情况下的异常但是内部并未主动创建相应事件因此外部捕获不到预期的结果应该调整如下所示版本以便正确反映潜在风险状况从而达到教学目的即训练学员识别常见陷阱的能力同时也加深他们关于如何妥善管理运行时期间可能出现的各种意外情形的知识水平[^1]: 改进后的版本如下: ```java private static int divide(int a, int b) throws ArithmeticException { if(b == 0){ throw new ArithmeticException("除数不能为零"); } return a/b ; } ``` ##### 四、综合设计题 这类考题往往要求结合多个知识点完成较为复杂的任务设定比如构建一个多线程环境下来模拟银行账户存取款过程确保数据一致性的同时还要考虑到并发控制机制等等细节部分这就需要用到锁同步原语或者高级别的队列容器来保障整个系统的健壮性和可靠性当然前提是先掌握好基础知识再逐步深入学习更深层次的技术要点[^3]. --- ### 总结说明 综上所述通过以上几种形式可以帮助我们更好地准备针对像华农这样重点院校所设置的标准考核体系内的相关内容不仅限于此还可以进一步探索更多实践性强的学习资源以提高个人解决问题的实际能力.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值