一个C++数组问题!

做一道C++题目

07:有趣的跳跃
查看提交统计提问
总时间限制: 1000ms 内存限制: 65536kB
描述
一个长度为n(n>0)的序列中存在“有趣的跳跃”当前仅当相邻元素的差的绝对值经过排序后正好是从1到(n-1)。例如,1 4 2 3存在“有趣的跳跃”,因为差的绝对值分别为3,2,1。当然,任何只包含单个元素的序列一定存在“有趣的跳跃”。你需要写一个程序判定给定序列是否存在“有趣的跳跃”。

输入
一行,第一个数是n(0 < n < 3000),为序列长度,接下来有n个整数,依次为序列中各元素,各元素的绝对值均不超过1,000,000,000。
输出
一行,若该序列存在“有趣的跳跃”,输出"Jolly",否则输出"Not jolly"。
样例输入
4 1 4 2 3
样例输出
Jolly

我的答案:

#include
using namespace std;

int abs(int a){
    if(a>=0)    return a;
    if(a<0)        return -a;
}

int main(){
    int n,temp=0;
    int a[3000],b[3000],c[3000];
    cin>>n;
    if(n==1){
        int ab;
        cin>>ab;
        cout<<"Jolly";
    }
    else {
        int b;
        cin>>b;
        a[0]=b; 
        for(int i=1;iint ab;
            cin>>ab;
            a[i]=ab;
            b[i]=abs(a[i]-a[i-1]);
            c[b[i]]=1;
        }
        for(int i=1;i<=n-1;i++){
            if(c[i]!=1){
                temp++;
                cout"Not jolly";
                break;
            }
            if(temp==0)    cout<<"Jolly";
        }
    }
} 

程序编译后报错

img


不知道问题出在哪,请帮忙指正

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;

const int N = 3005;
int n, a[N], b[N];

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 1; i < n; i++) b[i - 1] = abs(a[i] - a[i - 1]);
    sort(b, b + n - 1);
    for (int i = 0; i < n - 1; i++) {
        if (b[i] != i + 1) {
            cout << "Not jolly" << endl;
            return 0;
        }
    }
    cout << "Jolly" << endl;
    return 0;
}

错误信息指示:

invalid types 'int[int]' for array subscript
这个错误信息说明,在使用数组c[]时,使用了非整数类型作为下标,数组c[]是一个整型数组,因此下标必须是整数类型。

expected';' before string constant
这个错误信息说明,在输出字符串"Not Jolly"时忘记添加括号,因此编译器误以为它是一个函数调用,但未找到适当的参数列表。

解决方法:


cout"Not jolly";

修改为

cout<<"Not Jolly";

同时将

if(temp==0)    cout<<"Jolly";

放在循环之外,即

for(int i=1;i<=n-1;i++){
    if(c[i]!=1){
        temp++;
        cout<<"Not Jolly";
        break;
    }
}
if(temp==0) cout<<"Jolly";