IQ Test

                                                        IQ Test

Description

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 Input
Input
836 624 412 200

Output
-12
Input
1 334 667 1000
Output
1333
Hint

This problem contains very weak pretests!



一个非常简单的等差中项,等比中项的题目,不要想得太复杂了,关键是利用(a + c) == b * 2,
b * b == a * c判断和输出,
需要注意的是,尽量用乘法,避免用除法。

#include <stdio.h>

int main()
{
    int a,b,c,d;
    int x,y,z;
    scanf("%d%d%d%d",&a,&b,&c,&d);
    if((a + c) == b << 1 && (b + d) == c << 1)
    {
        printf("%d\n",(d << 1) - c);
    }
    else if((b * b == a * c) && (c * c == b * d) && ((d * d) % c == 0))
    {
        printf("%d\n",(d * d)/c);
    }
    else
        printf("42\n");
    return 0;
}

下面是同学的代码,运行起来更快一些,在判断下面一个数是不是小数的时候,他用了强制转换,并且确保公比大于0.

#include<stdio.h>
int main()
{
    float a,b,c,d,e,g;
    scanf("%f%f%f%f",&a,&b,&c,&d);
    if(a-b==b-c&&b-c==c-d)
    {
        printf("%.0f\n",d-(a-b));
    }
        else if(a/b==b/c&&b/c==c/d)
        {
            if(a>b)
            e=d/(a/b);
            else
                e=d*(b/a);
            int f;
            f=(int)e;
            if(f==e)
         printf("%.0f\n",e);
         else
           printf("42\n");
        }
    else
        printf("42\n");
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值