zoukankan      html  css  js  c++  java
  • 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 a1a1 + da1 + 2d...a1 + (n - 1)d, where a1 and d are any numbers.

    Geometric progression is a sequence b1b2 = b1q...bn = bn - 1q, where b1 ≠ 0q ≠ 0q ≠ 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
    ",a[3]+is_dc());
    		}
    		else if(is_db()!=INF)
    		{
    			double t=is_db()*a[3];
    			if(t-floor(t)==0)
    			{
    				printf("%.0lf
    ",t);
    			}
    			else
    				puts("42");
    		}
    		else
    			puts("42");
    	}
    	return 0;
    }
  • 相关阅读:
    RabbitMQ详解
    嵌入式开发学习(10)<汇编写启动代码之设置栈、调用c语言、开关看门狗和开关iCache>
    嵌入式开发学习(8)<一步一步点亮LED灯>
    gcc编译神器之makefile
    嵌入式开发学习(6)<S5PV210开发板刷系统那点破事儿之二>
    嵌入式开发学习(5)<S5PV210开发板刷系统那点破事儿之一>
    嵌入式开发学习(4)<ARM汇编指令集详解>
    嵌入式开发学习(3)<ARM汇编指令集语法>
    嵌入式开发学习(2)<S5PV210启动过程详解>
    嵌入式开发学习(1)<ARM体系结构>
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4050236.html
Copyright © 2011-2022 走看看