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. 手机Web网站,设置拒绝电脑访问

    最近一段时间,都在使用Jquery-Mobile + MVC做手机Web,有一些心得.体会 下面介绍如何拒绝电脑访问手机网站 电脑的浏览器,跟手机的浏览器内核不一样,这是我设置拒绝访问的思路. 下面是 ...

  2. SQL 语句修改列名 属性 默认值

    --修改字段名exec sp_rename '表名.列名','新列名' --修改字段属性alter table 表名 alter column 列名 nvarchar(100) null; --修改默 ...

  3. office2010删除多余空行

    选择 ctrl+H,弹出 "查找和替换"对话框,在"查找内容"输入"^p^p",并在"替换为"输入"^p&qu ...

  4. Lua 基础知识-面向对象

    通过函数闭包的方式来实现面向对象 -- 通过函数闭包的方式来实现面向对象 function People(name) local self = {} local function init() sel ...

  5. 武汉科技大学ACM :1003: 零起点学算法14——三位数反转

    Problem Description 水题 Input 输入1个3位数(题目包含多组测试数据) Output 分离该3位数的百位.十位和个位,反转后输出(每组测试数据一行) Sample Input ...

  6. Android-第一个Android程序

    Android项目的目录结构及功能 目录 功能 gen 自动生成的文件,不要修改 assets 项目中自定义的需要用到的资源,Android平台不能识别 res 工程资源,Android平台能够识别, ...

  7. nginx_笔记分享_配置篇

    参考http://www.howtocn.org/nginx:directiveindexhttp://blog.s135.com/ nginx 配置文档为 nginx.conf 比如我的配置文档 / ...

  8. 复杂事件处理引擎—Esper 处理模型

    1.esper的处理模型是持续性的——根据statement中事件流(event stream).视图(views).过滤器(filters)等的选择,esper引擎一旦处理事件数据,就会变更stat ...

  9. 转:Spine.JS+Rails重客户端Web应用技术选型思路:『风车』架构设计

    原文来自于:http://www.infoq.com/cn/articles/fengche-co-architecture 风车这个项目开始于 2011 年 11 月份,之前叫做 Pragmatic ...

  10. DbUtility-关于DataTable转成List的效率问题

    DbUtility中的方法ExecuteDataTableAsync()得到的是一个DataTable,而我们常见的情况下,我们需要的不是DataTable,而是List或IList,所以现在需要考虑 ...