您的位置:首页 > 产品设计 > UI/UE

csuoj-1715-Positive Con Sequences

2016-03-27 20:10 375 查看

Description



Input



Output



Sample Input

1 2 -1 4
2 4 8 -1
7 8 -1 21
5 -1 11 14
-1 2 4 6
-1 -1 -1 -1

Sample Output

3
16
-1
8
-1


给你四个数,其中一个是-1,让你去算,当是等差或者等比的时候这个数列是否满足,如果满足,则输出-1那里本来应该有的数,否则输出-1.

就是暴力过去就好了,但是需要注意一点就是要考虑最后输出的范围啊!!![1,10000],我就是因为这个wa了两发。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int a,b,c,d;
int val;
int x1,x2,y1,y2;
while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF)
{
y1=0;y2=0;x1=0;x2=0;
bool flag=false;
if(a==-1&&b==-1&&c==-1&&d==-1) break;
if(a==-1) val=1;
else if(b==-1) val=2;
else if(c==-1) val=3;
else if(d==-1) val=4;
if(val==1)
{
x1=c-b;
x2=d-c;
if(x1==x2){
a=b-x1;
if(a>0&&a<10001)
flag=true;
}
if(flag){printf("%d\n",a); continue ;}
else{
if(d%c==0)
{
y1=d/c;
if(c%b==0)
{
y2=c/b;
}
else
y2 = 0;
if(y1==y2) {
if(b%y1==0)
{
a=b/y1;
if(a>0&&a<10001)
flag=true;
}
}
}
}
if(flag) printf("%d\n",a);
else printf("-1\n");
}
else if(val==2)
{
x1=c-a;
x2=d-c;
if(x1==2*x2){
b=c-x2;
if(b>0&&b<10001)
flag=true;
}
if(flag){printf("%d\n",b); continue ;}
else{
if(d%c==0)
{
y1=d/c;
if(c%a==0)
{
y2=c/a;
}
else
y2 = 0;
if(y1*y1==y2) {
if(c%y1==0)
{
b=c/y1;
if(b>0&&b<10001)
flag=true;
}
}
}
}
if(flag) printf("%d\n",b);
else printf("-1\n");
}
else if(val==3)
{
x1=b-a;
x2=d-b;
if(2*x1==x2){
c=d-x1;
if(c>0&&c<10001)
flag=true;
}
if(flag){printf("%d\n",c); continue ;}
else{
if(b%a==0)
{
y1=b/a;
if(d%b==0)
{
y2=d/b;
}
else
y2 = 0;
if(y1*y1==y2) {
if(d%y1==0)
{
c=d/y1;
if(c>0&&c<10001)
flag=true;
}
}
}
}
if(flag) printf("%d\n",c);
else printf("-1\n");
}
else if(val==4)
{
x1=b-a;
x2=c-b;
if(x1==x2){
d=c+x1;
if(d>0&&d<10001)
flag=true;
}
if(flag){printf("%d\n",d); continue ;}
else{
if(b%a==0)
{
y1=b/a;
if(c%b==0)
{
y2=c/b;
}
else
y2 = 0;
if(y1==y2) {
d=c*y1;
if(d>0&&d<10001)
flag=true;
}
}
}
if(flag) printf("%d\n",d);
else printf("-1\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: