AMC-ICPC Live Archive 7295 和 CSU 1715

对于这两道破题花了我一上午的时间,我感到很愤怒!!!因为自己少看了两个0,WA了一遍又一遍…最后看了题,哭了…QAQ,这道题应该是那种怎么写怎么过的题,简单得要死…
两道题目链接:
AMC-ICPC Live Archive 7295, click here.
CSU 1715 click hrere.

题意很简单,就是四个数,可能是等差或等比数列,如果是,求出-1所在位置的数字是多少,否则输出-1.
hint : uva-Live中的输出数据范围为[1,1000000],csu中的数据输出范围为[1,10000].
两种方法:
    1>  一位一位模拟;
    2>  直接暴力,简单粗暴.

二话不说,上代码:

1>   第一种比较麻烦的方法------模拟:(出代码中的数据范围可以根据题意需要改动.)
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int a,b,c,d,x,y;
    while(cin>>a>>b>>c>>d,a!=-1||b!=-1||c!=-1||d!=-1)
    {
        if(a==-1)
        {
            x=c-b;
            y=d-c;
            if(x==y)
            {
                a=b-x;
                if(a>0&&a<1000001)
                    cout<<a<<endl;
                else
                    cout<<"-1"<<endl;
            }
            else
            {
                if(c%b==0&&d%c==0)
                {
                    x=c/b;
                    y=d/c;
                    a=b/x;
                    if(x==y&&b%x==0&&a<1000001&&a>0)
                            cout<<a<<endl;
                        else
                            cout<<"-1"<<endl;
                }
                else
                    cout<<"-1"<<endl;
            }
        }

        if(b==-1)
        {
            x=d-c;
            y=c-a;
            b=a+x;
            if(y==2*x&&b<1000000&&b>0)
                cout<<b<<endl;
            else
            {
                if(d%c==0&&c%a==0)
                {
                    x=d/c;
                    y=c/a;
                    b=c/x;
                    if(x*x==y&&c%x==0&&b<1000001&&b>0)
                        cout<<b<<endl;
                    else
                        cout<<"-1"<<endl;
                }
                else
                    cout<<"-1"<<endl;
            }
        }
        if(c==-1)
        {
            x=b-a;
            y=d-b;
            c=b+x;
            if(x*2==y&&c<1000001&&c>0)
                cout<<c<<endl;
            else
            {
                if(b%a==0&&d%b==0)
                {
                    x=b/a;
                    y=d/b;
                    c=d/x;
                    if(x*x==y&&d%x==0&&c<1000001&&c>0)
                        cout<<c<<endl;
                    else
                        cout<<"-1"<<endl;
                }
                else
                    cout<<"-1"<<endl;
            }
        }

        if(d==-1)
        {
            x=c-b;
            y=b-a;
            d=c+x;
            if(x==y&&d<1000001&&d>0)
                cout<<d<<endl;
            else
            {
                if(c%b==0&&b%a==0)
                {
                    x=c/b;
                    y=b/a;
                    d=c*x;
                    if(x==y&&d<=1000000&&d>0)
                        cout<<d<<endl;
                    else
                        cout<<"-1"<<endl;
                }
                else
                    cout<<"-1"<<endl;
            }
        }
    }
    return 0;
}
    2>  第二种方法------直接暴力.(给出代码中的数据范围可以根据题意需要改动.)

#include<iostream>
using namespace std;
int main()
{
    int a[5];
    while(cin>>a[0]>>a[1]>>a[2]>>a[3],a[1]!=-1||a[2]!=-1||a[3]!=-1||a[0]!=-1)
    {
        bool flag=false;
        int p;
        for(int i=0; i<4; i++)
            if(a[i]==-1)
            {
                p=i;
                break;
            }
        for(int i=1; i<=1000000; i++)
        {
            a[p]=i;
            if(a[1]*a[1]==a[0]*a[2]&&a[2]*a[2]==a[1]*a[3])
            {
                cout<<i<<endl;
                flag=true;
                break;
            }
            else if(a[1]-a[0]==a[2]-a[1]&&a[3]-a[2]==a[2]-a[1])
            {
                cout<<i<<endl;
                flag=true;
                break;
            }
        }
        if(!flag)
            cout<<-1<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值