设为首页 加入收藏

TOP

Codeforces 328A-IQ Test(数列)
2015-07-20 17:30:17 来源: 作者: 【 】 浏览:2
Tags:Codeforces 328A-IQ Test 数列
A. IQ Test time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

Petya is preparing for IQ test and he has noticed that there many problems like: you are given a sequence, find the next number. Now Petya can solve only problems with arithmetic or geometric progressions.

Arithmetic progression is a sequence a1, a1?+?d, a1?+?2d, ..., a1?+?(n?-?1)d, where a1 and d are any numbers.

Geometric progression is a sequence b1, b2?=?b1q, ..., bn?=?bn?-?1q, where b1?≠?0, q?≠?0, q?≠?1.

Help Petya and write a program to determine if the given sequence is arithmetic or geometric. Also it should found the next number. If the sequence is neither arithmetic nor geometric, print 42 (he thinks it is impossible to find better answer). You should also print 42 if the next element of progression is not integer. So answer is always integer.

Input

The first line contains exactly four integer numbers between 1 and 1000, inclusively.

Output

Print the required number. If the given sequence is arithmetic progression, print the next progression element. Similarly, if the given sequence is geometric progression, print the next progression element.

Print 42 if the given sequence is not an arithmetic or geometric progression.

Sample test(s) input
836 624 412 200
output
-12
input
1 334 667 1000
output
1333
题意: 给4个数,判断是否为等差或等比数列(等比数列的定义与高中相同但是公比不能为1,公比为1其实就是等差数列了。。)如果是等比或等差数列,输入其下一项,否则输入“42”。如果下一项不是整数也输出“42”
#include 
   
    
#include 
    
      #include 
     
       #include 
      
        #include 
       
         #include 
        
          #include 
         
           #include 
           #include 
           
             #include 
            
              #include 
             
               #include 
              
                #include 
               
                 using namespace std; const int INF=0x3f3f3f3f; #define LL long long double a[5]; double is_dc() { for(int i=1;i<3;i++) if(a[i]-a[i-1]!=a[i+1]-a[i]) return INF; return a[1]-a[0]; } double is_db() { for(int i=1;i<3;i++) if(a[i]/a[i-1]!=a[i+1]/a[i]) return INF; return a[1]/a[0]; } int main() { while(scanf("%lf%lf%lf%lf",a,a+1,a+2,a+3)!=EOF) { if(is_dc()!=INF) { printf("%.0lf\n",a[3]+is_dc()); } else if(is_db()!=INF) { double t=is_db()*a[3]; if(t-floor(t)==0) { printf("%.0lf\n",t); } else puts("42"); } else puts("42"); } return 0; }
               
              
             
            
           
         
        
       
      
     
    
   
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Codeforces 327B-Hungry Sequence.. 下一篇二叉树的非递归遍历--京东2015笔..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·每日一道面试题-多线 (2025-12-26 06:20:17)
·java项目中哪些地方 (2025-12-26 06:20:14)
·Java真的是要没落了 (2025-12-26 06:20:12)
·C++ Lambda表达式保 (2025-12-26 05:49:45)
·C++ Lambda表达式的 (2025-12-26 05:49:42)