Last Defence (run time error)

Last Defence
时间限制:1000 ms | 内存限制:65535 KB
描述
Given two integers A and B. Sequence S is defined as follow:
• S0 = A
• S1 = B
• Si = |Si-1 – Si-2| for i ≥ 2
Count the number of distinct numbers in S .
输入
The first line of the input gives the number of test cases, T. T test cases follow. T is about 100000.
Each test case consists of one line – two space-separated integers A, B. (0 ≤ A, B ≤ 10^18).
输出
For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the number of distinct numbers in S .
样例输入
2
7 4
3 5
样例输出
Case #1: 6
Case #2: 5
来源
Yougth
我的思路:首先这道题run time error了。。任何两个数,进行题中的运算,得出是一组数,最后肯定是x, x,0,x,x,0,x,x,0……循环的,所以当遇到x,x,0时候就停止,然后把前面的数和x,0进行处理,计算一下一共有多少不同的数。。希望大家帮看一下,这个代码运行没有错误,结果也对。
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cmath>
using namespace std;
int a[10000000];
int main()
{
int T,i,j;
int g=1;
long long A,B;
cin>>T;
while(T--)
{
scanf("%lld%lld",&A,&B);
a[0]=A;
a[1]=B;
i=2;
while(1)
{
a[i]=fabs(a[i-1]-a[i-2]);
a[i+1]=fabs(a[i]-a[i-1]);
a[i+2]=fabs(a[i+1]-a[i]);
if(a[i]==a[i+1]&&a[i+2]==0)
{  a[i+1]=0;
break;
}
i++;
}
int n=i+2;
sort(a,a+i+2);
for(i=0;i<n-1;i++)
{
if(a[i]==a[i+1])
{
for(j=i;j<n-1;j++)
a[j]=a[j+1];
i--;
n--;
}
}
printf("Case #%d: %d
",g,n);
g=g+1;
}
return 0;
}

原文地址:https://www.cnblogs.com/NYNU-ACM/p/4236879.html