codeforces 382C
来源:互联网 发布:jdk 7u55 linux x64 编辑:程序博客网 时间:2024/05/21 11:01
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:
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
34 1 7
2-2 10
110
-1
41 3 5 9
17
44 3 4 5
0
22 4
30 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;}
- codeforces 382C
- CodeForces 382C【模拟】
- Codeforces 382 C. Arithmetic Progression
- codeforces#382C. Arithmetic Progression
- Codeforces #382 C Tennis Championship
- 【17.76%】【codeforces round 382C】Tennis Championship
- Codeforces Round #382 (Div. 2) C
- Codeforces Round #382 (Div. 2)C
- Codeforces-340-C(c++)
- Codeforces-507-C(c++)
- CodeForces 731C C
- CodeForces-612C C
- CODEFORCES 265C CODEFORCES, 265C
- codeforces #78 div2 C
- Codeforces 117C Cycle
- CodeForces 209C
- CodeForces 156C Cipher
- codeforces 148 div2 C
- android中三种菜单的简单实现
- android 中action Bar的使用
- andoid自定义dialog实现登陆界面
- 自定义DialogFragment如何与打开它…
- android notification通知的…
- codeforces 382C
- 2498 AOE网上的关键路径
- android 蓝牙bluetooth的简单使用
- 使用 ipmitool 实现 Linux 系统下对服务器的 ipmi 管理
- android wifi的打开关闭以及…
- Android Sdk Manager 无法下载资源
- android 画廊控件Gallery的使用
- tabHost选项卡的简单使用
- Activity com.example.weibo.…