标签:水题
题意:求最长的连续的等差 或者 等比序列
思路: 暴力,输入的时候将它们的差和比分别存在两个数组中
ps:此题动不动就超时 很无语, 必须用传统的输入输出, 不能加memset, 如果wa了
考虑下n==2时答案是否为2;
代码:
#include <algorithm> #include <iostream> #include <sstream> #include <cstdlib> #include <cstring> #include <iomanip> #include <cstdio> #include <string> #include <bitset> #include <vector> #include <queue> #include <stack> #include <cmath> #include <list> #include <map> #include <set> #define sss(a,b,c) scanf("%d%d%d",&a,&b,&c) #define mem1(a) memset(a,-1,sizeof(a)) #define mem(a) memset(a,0,sizeof(a)) #define ss(a,b) scanf("%d%d",&a,&b) #define s(a) scanf("%d",&a) #define INF 0x3f3f3f3f #define w(a) while(a) #define PI acos(-1.0) #define LL long long #define eps 10E-9 #define N 1000000 + 5 #define mod 100000000 using namespace std; void mys(int& res) { int flag=0; char ch; while(!(((ch=getchar())>='0'&&ch<='9')||ch=='-')) if(ch==EOF) res=INF; if(ch=='-') flag=1; else if(ch>='0'&&ch<='9') res=ch-'0'; while((ch=getchar())>='0'&&ch<='9') res=res*10+ch-'0'; res=flag?-res:res; } void myp(int a) { if(a>9) myp(a/10); putchar(a%10+'0'); } /*************************THE END OF TEMPLATE************************/ double arr[N] = {0}; double a1[N] = {0};//from 1 to n-1 double a2[N] = {0};//from 1 to n-1 int main() { int t, n ,x ,y; s(t); w(t--){ s(n); if(n == 2){ ss(x,y); printf("2\n"); } else{ scanf("%lf",&arr[1]); int ans1 = -1, ans2 = -1, bg1 = 0, bg2 = 0; for(int i=2; i<=n; i++){ scanf("%lf",&arr[i]); a1[i-1] = arr[i] - arr[i-1]; a2[i-1] = (arr[i] / arr[i-1]); if(i>2){ if(a1[i-1] == a1[i-2]) bg1 ++; if(a1[i-1] != a1[i-2] || i==n){ ans1 = max(ans1, bg1); bg1 = 0; } if(a2[i-1] == a2[i-2]) bg2 ++; if(a2[i-1] != a2[i-2] || i==n){ ans2 = max(ans2, bg2); bg2 = 0; } } } printf("%d\n", max(ans1, ans2) + 2); } } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:水题
原文地址:http://blog.csdn.net/bigsungod/article/details/47165901