Codeforces 328A-IQ Test(数列)
1 second
256 megabytes
standard input
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.
The first line contains exactly four integer numbers between 1 and 1000, inclusively.
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.
836 624 412 200
-12
1 334 667 1000
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(数列)的更多相关文章
- codeforces A. IQ Test 解题报告
题目链接:http://codeforces.com/problemset/problem/328/A 一开始单纯地直接判断给出的序列是等差还是等比,连这一句“You should also prin ...
- [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)
[Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...
- Codeforces 446C - DZY Loves Fibonacci Numbers(斐波那契数列+线段树)
Codeforces 题目传送门 & 洛谷题目传送门 你可能会疑惑我为什么要写 *2400 的题的题解 首先一个很明显的想法是,看到斐波那契数列和 \(10^9+9\) 就想到通项公式,\(F ...
- 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 ...
- CodeForces 450B Jzzhu and Sequences 费波纳茨数列+找规律+负数MOD
题目:Click here 题意:给定数列满足求f(n)mod(1e9+7). 分析:规律题,找规律,特别注意负数取mod. #include <iostream> #include &l ...
- 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 ...
- Codeforces Round #205 (Div. 2)C 选取数列可以选择的数使总数最大——dp
http://codeforces.com/contest/353/problem/C Codeforces Round #205 (Div. 2)C #include<stdio.h> ...
- 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 ...
- 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 ...
随机推荐
- GDI+基础(3)
常用图形绘制 <%@ Page ContentType="image/gif" Language="C#" %> <!--ContentTyp ...
- 关于数据库中varchar/nvarchar类型数据的获取注意事项
当在页面后台获取数据库表中某字段的数据时,需注意该数据的类型.防止因实际数据的字符长度因达不到指定数据类型规定的字符长度而导致空格的占位符. 比如: MSSQL中某一表的结构如下: 表中的数据: ...
- 【转载】Java StringBuffer与StringBuider
本文转载微学苑java基础教程,原文地址链接:http://www.weixueyuan.net/view/6318.html String 的值是不可变的,每次对String的操作都会生成新的Str ...
- Ajax--JavaScript实现
Ajax:一种不用刷新整个页面便可与服务器通讯的办法 Ajax实现的步骤: 1.创建XMLHttpRequest对象 2.服务器向浏览器响应请求(注册监听) 3.浏览器与服务器建立连接 4.浏览器向服 ...
- URAL 1008 - Image Encoding(bfs坑爹题)
坑爹题,两种输入输出互相交换,裸bfs #include <stdio.h> #include <string.h> typedef struct { int x; int y ...
- Mysql 查询性能优化
查询优化,索引优化,库表结构优化需要齐头并进,一个不能落. 为什么查询速度会慢 在阐释编写快速的查询之前,需要清楚一点,真正重要的是响应时间.如果把查询看做是一个任务的话,那么它由一系列子任务构成,每 ...
- createwindow
WNDCLASS wndclass; wndclass.hbrBackground=(HBRUSH)getstockobject(WHITE_BRUSH); wndclass.hCursor=Load ...
- JavaScript实现回车键切换输入框焦点
用JavaScript实现回车键切换输入框焦点的功能,不是回车换行哦,在Textarea中,回车换行是默认功能,不过若要在textarea中使用 回车切换输入框焦点功能的话,回车换行就要失效了,不过i ...
- ACM、OI等比赛中的程序对拍问题
多年前请教于ZXYTIM(zxy)大牛,现在把windows环境下的版本贴出来. 手动数据调试效率太低,程序对拍还是非常实用的,特别适用于OI.蓝桥杯等这些比赛规则.可以用于暴力与AC算法之间的对拍. ...
- 摇滚吧HTML5!Jsonic超声波前端交互!
前些年吹过一阵canvas制作html5游戏的东风,相信不少同学重温了一把高中物理课本上的牛顿定律.时光如梭,你是否还记得牛顿定律后面一章的各种机械波的物理定律?环视四周,光纤.wifi.蓝牙.广播都 ...