Problems A.请写一个程序,判断给定整数序列能否构成等差数列
输入说明:多组数据,每组输入数据由两行构成,第一行只有一个整数n(<1000),表示序列长度(即序列中整数的个数,0表示输入结束),第二行为n个整数,每个整数的取值区间都为[-32768----32767],整数之间以空格或挑格间隔。
输出说明:对于每一组数据,输出一个yes或no,表示该序列能否构成等差数列。
输入样本:
6
23 15 4 18 35 11
3
3 1 2
0
输出样本:
yes
no
1
#include
<
cstdlib
>
2 #include < iostream >
3 #include < vector >
4 #include < String >
5 using namespace std;
6 int check( int * pa, int n)
7 {
8 if (n == 2 || n == 1 )
9 return 1 ;
10 else if (n > 2 )
11 {
12 for ( int i = 1 ;i < n - 1 ;i ++ )
13 if (pa[i] * 2 != (pa[i - 1 ] + pa[i + 1 ]))
14 return 0 ;
15 }
16 return 1 ;
17 }
18 int main( int argc, char * argv[])
19 {
20 vector < int > vt;
21 int n = 1 ;
22 while ( true )
23 {
24 cin >> n;
25 if (n == 0 )
26 break ;
27 else {
28 int * pa = new int [n];
29 for ( int i = 0 ;i < n;i ++ )
30 cin >> pa[i];
31 vt.push_back(check(pa,n));
32 }
33
34 }
35 for ( int j = 0 ;j < vt.size();j ++ )
36 {
37 if (vt[j] == 1 )
38 cout << " yes\n " ;
39 else
40 cout << " no\n " ;
41 }
42 // cout<<"asf"<<endl;
43 system( " PAUSE " );
44 return EXIT_SUCCESS;
45 }
46
2 #include < iostream >
3 #include < vector >
4 #include < String >
5 using namespace std;
6 int check( int * pa, int n)
7 {
8 if (n == 2 || n == 1 )
9 return 1 ;
10 else if (n > 2 )
11 {
12 for ( int i = 1 ;i < n - 1 ;i ++ )
13 if (pa[i] * 2 != (pa[i - 1 ] + pa[i + 1 ]))
14 return 0 ;
15 }
16 return 1 ;
17 }
18 int main( int argc, char * argv[])
19 {
20 vector < int > vt;
21 int n = 1 ;
22 while ( true )
23 {
24 cin >> n;
25 if (n == 0 )
26 break ;
27 else {
28 int * pa = new int [n];
29 for ( int i = 0 ;i < n;i ++ )
30 cin >> pa[i];
31 vt.push_back(check(pa,n));
32 }
33
34 }
35 for ( int j = 0 ;j < vt.size();j ++ )
36 {
37 if (vt[j] == 1 )
38 cout << " yes\n " ;
39 else
40 cout << " no\n " ;
41 }
42 // cout<<"asf"<<endl;
43 system( " PAUSE " );
44 return EXIT_SUCCESS;
45 }
46