HDOJ 5328 Problem Killer 【等差等比数列】

HDOJ 5328 Problem Killer 【等差等比数列】

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5328


…wa了n次…
1、 if(i == 1) AP = 1; else AP =2; // ap、gp出现重复的时候 eg: 7 1 2 3 1 2 3 4
2、 APmax = APmax>AP ? APmax : AP; // 没有进入 else 的时候 eg: 3 1 2 3
3、(double)a[i] / (double)a[i-1] // 转double

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int MAXN = 1e6+5;
int a[MAXN];
int T, n;
int AP, GP;
int APmax, GPmax;
int APd;
double GPq;

void Resolve(){
    AP = 1;
    GP = 1;
    APmax = 1;
    GPmax = 1;
    for(int i = 2; i <= n; i++){
        if(a[i] - a[i-1] == APd) AP++;
        else{
            APd = a[i] - a[i-1];
            APmax = APmax>AP ? APmax : AP;
            if(i == 1) AP = 1;
            else AP =2;
        }
    }
    APmax = APmax>AP ? APmax : AP;

    for(int i = 2; i <= n; i++){
        if((double)a[i] / (double)a[i-1] == GPq) GP++;
        else{
            GPq = (double)a[i] / (double)a[i-1];
            GPmax = GPmax>GP ? GPmax : GP;
            if(i == 1)GP = 1;
            else GP = 2;
        }
    }
    GPmax = GPmax>GP ? GPmax : GP;

    //printf("%d\t%d\n", APmax, GPmax);
    if(n == 1) printf("1\n");
    else printf("%d\n", APmax>GPmax ? APmax: GPmax);
}

int main(){
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        for(int i = 1; i <= n; i++){
            scanf("%d", &a[i]);
        }
        Resolve();
    }

    return 0;
}

标程

#include <stdio.h>
#include <algorithm>
#define MAXN 1000005
int a[MAXN];

inline void solve(){ //inline 说明这个函数是内联的,在编译过程中内联函数会直接被源代码替换,
    //如果类中的某个函数会被调用很多次或者放在循环中,那么建议将这个函数声明为内联,可以提高程序的运行效率
    int n;
    scanf("%d",&n);
    int i;
    for (i=1;i<=n;i++) {
        scanf("%d",a+i);
    }
    int ans=std::min(2,n); // 长度为2以上的话结果就从2开始计数,长度为1的话结果为1

    int l=1;
    for (i=3;i<=n;i++) {
        bool f= 2*a[i-1]==a[i-2]+a[i];
        if (!f) l=i-1;
        ans=std::max(ans,i-l+1);
    }

    l=1;
    for (i=3;i<=n;i++) {
        bool f= 1LL*a[i-1]*a[i-1]==1LL*a[i-2]*a[i]; // 1LL 转换成long long
        if (!f) l=i-1;
        ans=std::max(ans,i-l+1);
    }

    printf("%d\n",ans);
}

int main(){
    int T;
    scanf("%d",&T);
    int i;
    for (i=1;i<=T;i++) {
        solve();
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-08-04 21:37  快扶哀家去刷题  阅读(154)  评论(0编辑  收藏  举报