复试题之我的解答

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 

转载于:https://www.cnblogs.com/DonePuzzle/archive/2009/03/15/1412686.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值