codeforces 382C

the reason of failure:1、当输入为2个值a,b时,a,b相加可能为单数,那么其2个数之间就无法找到一个数使得这3个数变成等差数列.

2、当输入大于3个数字的时候,比如14 10 6 4,原来我写的判断是

for(i=2;i<n;i++){
		b=qq[i]-qq[i-1];
		if(b!=maxn){
			if(cc==2){
				cout << "0" << endl;
				return 0;
			}
			cc++;
			if(&&maxn>b&&maxn==2*b){
				maxn=b; 
			}else if(b>maxn&&b==maxn*2){
				pos1=i;
			}else{
				cout << "0" << endl;
				return 0;
			}
		}
	}

刚开始第一个差是maxn,然后当只遇到一个差不同与maxn时,且((maxn为其两倍)),那么maxn变为一半,然后判断剩下的是否都为现在的maxn,如果是把maxn变为一半的那个值和前一个值的一般输出.如16 12 10 8 。则输出14. 这里有个bug14 10 6 4,也是符合我的代码的,但却无法运行得到正确的结果,因为只有第一个差为后面差的两倍菜能够成等差数列.

3、!!!!认真看题,考虑各个情况再开始敲代码实现,每个特殊情况在思考时都该自己写一个例子,然后运行程序的时候应该运行看看是否能过.!!!!

thinking:先把特殊值

题意:给定N个数,判断有几个数可以和这N个数在一起构成等差数列,无限则输出-1,没有则输出0,有2个则输出2个并在下一行输出这两个的值.

Description

Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers a1, a2, ..., an of length n, that the following condition fulfills:

a2 - a1 = a3 - a2 = a4 - a3 = ... = ai + 1 - ai = ... = an - an - 1.

For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not.

Alexander has n cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting n + 1 cards to make an arithmetic progression (Alexander has to use all of his cards).

Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 108.

Output

If Arthur can write infinitely many distinct integers on the card, print on a single line -1.

Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 108 or even be negative (see test samples).

Sample Input

Input
3
4 1 7
Output
2
-2 10
Input
1
10
Output
-1
Input
4
1 3 5 9
Output
1
7
Input
4
4 3 4 5
Output
0
Input
2
2 4
Output
3
0 3 6
代码:

#include <bits/stdc++.h>
using namespace std;
int main(){
	long long n;
	cin >> n;
	long long qq[n+5],qq1[n+5];
	long long pos1=0,pos2;
	long long i,j,k,l;
	 for(i=0;i<n;i++)
	 cin >> qq[i];
	 if(n==1){
	 	cout << -1 << endl;
	 	return 0;
	 }
	 sort(qq,qq+n);
	long long b=qq[1]-qq[0];
	long long maxn=b;
	if(n==2){
		if(qq[1]==qq[0]){
			cout << "1"<< endl;
			cout << qq[0] << endl;
		}else{
		if(qq[1]-qq[0]==1){
			cout << "2" << endl;
			cout << qq[0]-1 << " " << qq[1]+1 << endl;
		}else{
			if((qq[1]+qq[0])%2==0){
				cout << "3" << endl;
				cout << qq[0]-(qq[1]-qq[0]) << " " << (qq[0]+qq[1])/2 << " " << qq[1]+(qq[1]-qq[0]) << endl;
			}else{
				cout << "2" << endl;
				cout << qq[0]-(qq[1]-qq[0]) << " " << qq[1]+(qq[1]-qq[0]) << endl;
			}
		}
	}
		return 0;
	}
	int cc=1;
	long long tt1=0,tt2=0;
	for(i=2;i<n;i++){
		b=qq[i]-qq[i-1];
		if(b!=maxn){
			if(cc==2){
				cout << "0" << endl;
				return 0;
			}
			cc++;
			if(i==2&&maxn>b&&maxn==2*b){
				maxn=b; 
			}else if(b>maxn&&b==maxn*2){
				pos1=i;
			}else{
				cout << "0" << endl;
				return 0;
			}
		}
	}
		if(cc==1){
			if(qq[0]==qq[1]){
				cout << "1" << endl;
				cout << qq[0]<< endl;
			}else{
			cout << "2" << endl;
			cout << qq[0]-maxn << " " << qq[n-1]+maxn << endl;}
		}else if(cc==2){
			if(pos1==0){
				cout << "1" << endl;
				cout << qq[0]+maxn;
			}else{
				cout << "1" << endl;
				cout << qq[pos1]-maxn << endl;
			}
	
}
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值