1050 - 找规律填数字
时间限制 : 1 秒
内存限制 : 32 MB
小宇正在读小学,今天老师布置了几道数学题目。小宇平时上课经常不专心,这些他可发愁了,怎么办呢?看看你能不能帮帮他。
题目是给你一组有规律序列的前面5个整数,请你给出它后面跟着的5个整数,如:1,2,3,4,5,,,_,__,_。这是个等差数列,后面应该是6,7,8,9,10,就这么简单。而且现在小宇已经知道这串序列要么是等差数列,要么是等比数列或者是斐波那契数列。
题目输入
输入包含多组测试数据。每组输入5个整数,每个数字之间隔一个空格,当5个数字都为0时输入结束。
题目输出
对于每组输入,输出这串数列的后面5个数字,每个数字之间隔一个空格。
输入/输出样例
输入格式
1 2 3 4 5 1 2 4 8 16 1 2 3 5 8 0 0 0 0 0
输出格式
6 7 8 9 10 32 64 128 256 512 13 21 34 55 89
C语言解答
#include<stdio.h> int main() { float a,b,c,d,e,x,x1,x2,x3,x4,x5; while(scanf("%f%f%f%f%f",&a,&b,&c,&d,&e)!=EOF&&a!=0&&b!=0&&c!=0&&d!=0&&e!=0) { if(b-a==c-b&&d-c==c-b&&e-d==d-c) { x=b-a; x1=e+x; x2=x1+x; x3=x2+x; x4=x3+x; x5=x4+x; printf("%.f %.f %.f %.f %.f\n",x1,x2,x3,x4,x5); } if(b/a==c/b&&d/c==c/b&&e/d==d/c) { x=b/a; x1=e*x; x2=x1*x; x3=x2*x; x4=x3*x; x5=x4*x; printf("%.f %.f %.f %.f %.f\n",x1,x2,x3,x4,x5); } if(c==a+b&&d==b+c&&e==c+d) { x1=d+e; x2=e+x1; x3=x2+x1; x4=x3+x2; x5=x4+x3; printf("%.f %.f %.f %.f %.f\n",x1,x2,x3,x4,x5); } } return 0; }
C++解答
#include<stdio.h> int main() { float a,b,c,d,e,x,x1,x2,x3,x4,x5; while(scanf("%f%f%f%f%f",&a,&b,&c,&d,&e)!=EOF&&a!=0&&b!=0&&c!=0&&d!=0&&e!=0) { if(b-a==c-b&&d-c==c-b&&e-d==d-c) { x=b-a; x1=e+x; x2=x1+x; x3=x2+x; x4=x3+x; x5=x4+x; printf("%.f %.f %.f %.f %.f\n",x1,x2,x3,x4,x5); } if(b/a==c/b&&d/c==c/b&&e/d==d/c) { x=b/a; x1=e*x; x2=x1*x; x3=x2*x; x4=x3*x; x5=x4*x; printf("%.f %.f %.f %.f %.f\n",x1,x2,x3,x4,x5); } if(c==a+b&&d==b+c&&e==c+d) { x1=d+e; x2=e+x1; x3=x2+x1; x4=x3+x2; x5=x4+x3; printf("%.f %.f %.f %.f %.f\n",x1,x2,x3,x4,x5); } } return 0; }
Java解答
import java.util.Scanner; public class Main{ private static Scanner s = new Scanner(System.in) ; public static void main(String[] args) { while(true){ int a = s.nextInt() ; int b = s.nextInt() ; int c = s.nextInt() ; int d = s.nextInt() ; int e = s.nextInt() ; if(a==0&&b==0&&c==0&&d==0&&e==0){ break ; } int sum = 0 ; int sum2 = 0 ; if(f(a, b, c, d, e)==1){ sum = e ; for (int i = 0; i < 5; i++) { sum = sum + e - d ; if(i==4){ System.out.println(sum) ; }else { System.out.print(sum+" ") ; } } }else if(f(a, b, c, d, e)==2){ sum = e ; for (int i = 0; i < 5; i++) { sum = sum * (e / d) ; if(i==4){ System.out.println(sum) ; }else { System.out.print(sum+" ") ; } } }else { sum = d ; sum2 = e ; int temp = 0 ; for (int i = 0; i < 5; i++) { temp = sum2 ; sum2 = sum + sum2 ; sum = temp ; if(i==4){ System.out.println(sum2) ; }else { System.out.print(sum2+" ") ; } } } } } public static int f(int a , int b , int c , int d , int e){ if((d-c==c-b)&&(e-d==d-c)&&(c-b==b-a)){ return 1 ; } else if(((double)d/c==(double)c/b)&&((double)e/d==(double)d/c)&&((double)c/b==(double)b/a)){ return 2 ; } else return 3 ; } }
Python解答
# coding=utf-8 a,b,c,d,e=map(int,input().split()) while a!=0 or b!=0 or c!=0 or d!=0 or e!=0: if b-a==c-b==d-c: print(e+b-a,e+(b-a)*2,e+(b-a)*3,e+(b-a)*4,e+(b-a)*5) elif b/a==c/b==d/c: print(int(e*b/a),int(e*(b/a)**2),int(e*(b/a)**3),int(e*(b/a)**4),int(e*(b/a)**5)) else: print(d+e,d+2*e,2*d+3*e,3*d+5*e,5*d+8*e) a,b,c,d,e=map(int,input().split())