神奇的fans oj40

来源:互联网 发布:ubuntu安装桌面环境 编辑:程序博客网 时间:2024/05/16 06:06

神奇的fans

发布时间: 2017年5月25日 19:57   最后更新: 2017年5月26日 00:13   时间限制: 1000ms   内存限制: 128M

传说fans是一个数学天才。在他五岁那年,从一堆数字卡片中选出了4张 卡片:5,7,6,8。这4个数字有什么神秘之处呢?如果把这4张卡片自左往右的排成:5,6,7,8。你就会发现:原来这4个数字构成了等差数列!当年 fans选出了n组卡片,据说都能够构成等差数列。但是事实真的是这样吗?fans真的有这么神奇吗? n组数据就是fans选出的n组卡片,请你判断每一组卡片是否能构成等差数列.

第一个数为数据的组数n,表示后面有n行,每行中的第一个数为该组数据的元素个数m(1<=m<=100),其后是m个正整数(不会超出int的表示范围)。

如果能够构成等差数列,输出 yes ,否则输出 no 。

 复制
24 5 7 6 88 1 7 3 2 8 12 78 3
yesno
#include <algorithm>#include <iostream>#include <cstdio>using namespace std;int cmp(int a, int b){return a < b;}int main(){int t;cin >> t;while (t--){int n;cin >> n;int *p = new int[n];for (int i = 0; i < n; i++)scanf("%d",&p[i]);if (n == 1 || n == 2){cout << "yes" << endl;continue;}sort(p, p + n, cmp);int d = p[1] - p[0];int i = 1;for (; i < n-1; i++){if (d != p[i + 1] - p[i])break;}if (i >= n - 1)cout << "yes" << endl;elsecout << "no" << endl;}return 0;}