Amusement Anticipation

Amusement Anticipation

amusement.c

,

amusement.cpp

,

amusement.c11

,

Amusement.java

,

amusement.py

It is was late Saturday morning at the end of October. The amus

ement park was going to be

open that afternoon for the first time after months of prolong

ed reconstruction.

Josse and Murry were sitting in the basement of the office build

ing at the park gate. They have

just finished debugging their advanced AI system management

of all fantastic attractions in the

park. “Work is done,” said Josse. “Now for some amusement. Ca

n you think of any algorithmic

problem?”

“Yes, of course,” replied Murry and grinned at his friend fro

m his heavily cluttered desk. “Con-

sider, for example, finite sequences of numbers. For me, the m

ost interesting sequences are those

that end with a long arithmetic sequence. In other words, I li

ke arithmetic sequences that span

from some index to the very last member. As there may be many su

ch subsequences, in order

to truly appreciate how interesting some sequence is, it is n

ecessary to determine which one is

the longest. Here is your initial sequence. You have to find th

e start of the longest continuous

arithmetic subsequence spanning to its end.”

“OK,” said Josse when he marked the correct place in the seque

nce. “That was easy. What

next?” “What next?” Repeated Murry and hesitated for a momen

t. Then he raised himself

determinedly from the chair. “Let’s go out to the park and find

some tougher problems there!”

Input Specification

There are more test cases. Each case consists of two lines. Th

e first line contains one integer

N

(1

N

1 000) specifying the length of the sequence. The second line

contains a sequence of

N

integers

X

i

(0

X

i

10

9

), separated by spaces.

Output Specification

For each test case, print a single line with the index of the fir

st member of the longest continuous

arithmetic subsequence that spans to the end of the given seq

uence. The index of the first element

in the sequence is always 1.

Sample Input

5

1 2 3 4 5

7

1 2 3 4 5 8 8

3

7 5 2

Output for Sample Input

1

6

2

AC代码(题意:找到间隔相同的最长的子串,这个串以最后一个元素为开头)

Select Code

#include <iostream>
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    //freopen("amusement.cpp", "r", stdin);
    long long a[1000+10], ans;
    int n, i;
    while(scanf("%d",&n)!=EOF)
    {
       int f = 1;
        ans = 0;
        for(i = 1; i<=n; i++)
            scanf("%lld",&a[i]);
            ans = a[n]-a[n-1];
        for(i = n; i>=1; i--)
        {
          if(a[i]-a[i-1]!=ans)
          {
           printf("%d\n",i);
           f = 0;
           break;
          }
        }
        if(f==1)
        printf("1\n");
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值