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 + da1 + 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 <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
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 328A-IQ Test(数列)的更多相关文章

  1. codeforces A. IQ Test 解题报告

    题目链接:http://codeforces.com/problemset/problem/328/A 一开始单纯地直接判断给出的序列是等差还是等比,连这一句“You should also prin ...

  2. [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)

    [Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...

  3. Codeforces 446C - DZY Loves Fibonacci Numbers(斐波那契数列+线段树)

    Codeforces 题目传送门 & 洛谷题目传送门 你可能会疑惑我为什么要写 *2400 的题的题解 首先一个很明显的想法是,看到斐波那契数列和 \(10^9+9\) 就想到通项公式,\(F ...

  4. Codeforces 446-C DZY Loves Fibonacci Numbers 同余 线段树 斐波那契数列

    C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...

  5. CodeForces 450B Jzzhu and Sequences 费波纳茨数列+找规律+负数MOD

    题目:Click here 题意:给定数列满足求f(n)mod(1e9+7). 分析:规律题,找规律,特别注意负数取mod. #include <iostream> #include &l ...

  6. codeforces水题100道 第十七题 Codeforces Beta Round #25 (Div. 2 Only) A. IQ test (brute force)

    题目链接:http://www.codeforces.com/problemset/problem/25/A题意:在n个书中找到唯一一个奇偶性和其他n-1个数不同的数.C++代码: #include ...

  7. Codeforces Round #205 (Div. 2)C 选取数列可以选择的数使总数最大——dp

    http://codeforces.com/contest/353/problem/C Codeforces Round #205 (Div. 2)C #include<stdio.h> ...

  8. Codeforces Beta Round #25 (Div. 2)--A. IQ test

    IQ test time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  9. Codeforces Beta Round #25 (Div. 2 Only) A. IQ test【双标记/求给定数中唯一的奇数或偶数】

    A. IQ test time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

随机推荐

  1. OUTPUT新增记录入库示例C#+存储过程

    1.C#代码 public int Insert(Entity model) { var parameters = new SqlParameter[] { new SqlParameter(&quo ...

  2. 安装VS2010后,如何设置老版本的项目文件不是默认用VS2010打开

    1.系统先后安装了VS2008和VS2010,在打开用VS2008创建的项目文件时总是会默认用VS2010打开,选择打开方式都不行,很不方便,差点要把VS2010卸载了.     其实只需要简单设置V ...

  3. 【转】深入理解Java内存模型(三)——顺序一致性

    数据竞争与顺序一致性保证 当程序未正确同步时,就会存在数据竞争.java内存模型规范对数据竞争的定义如下: 在一个线程中写一个变量, 在另一个线程读同一个变量, 而且写和读没有通过同步来排序. 当代码 ...

  4. css3表格隔行变色和表格选中变颜色代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. Struts2 处理表单重复提交

    * 在表单页面中增加一个隐藏域:<s:token></s:token>(需要在表单内)        * 创建一个struts.xml的配置文件,具体配置如下:         ...

  6. 一行代码实现C#的四舍五入

    C# 使用的是”四舍六入五成双”的银行家算法: 1 2 Math.Round(2.5); // 2 Math.Round(1.5); // 2 由此可见,1.5的Round符合我们的四舍五入,于是Ha ...

  7. Cocos2d粒子系统二

    粒子系统的属性: 粒子的发射速度 重力模式(模式A): 重力 方向 速度 +- 变动 切向加速度 +- 变动 径向加速度 +- 变动 半径模式(模式B): 开始半径 +- 变动 结束半径 +- 变动 ...

  8. 转:SRIO错误侦测和管理机制

    http://www.deyisupport.com/question_answer/dsp_arm/c6000_multicore/f/53/t/22879.aspx http://www.deyi ...

  9. Android中的手势

    Android对两种手势行为提供了支持:1.对于第一种手势行为而言,Android提供了手势检测,并为手势检测提供了相应的监听器.2.对于第二种手势行为,Android允许开发者添加手势,并提供了相应 ...

  10. Valve开源了Direct3D到OpenGL的转译层,方便开发者迁移游戏到Linux(面向游戏玩家的六款最佳 Linux 发行版)

    Valve开源了Direct3D到OpenGL的转译层,方便开发者迁移游戏到Linux:https://github.com/ValveSoftware/ToGL Valve SteamBox主机系统 ...