I used Dev C++ 4.9.9.2 to code. I had 2 code versions of this problem, and I have a problems with first version, it makes me Compile Error with abs function on Online Judge ( but Compile Succesful on Dev C)
This code make me COMPILE ERROR ( Dev C++ compile it succesfully )
Code: Select all
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n;
int i;
int jolly = 0;
n = 0;
int a[3001];
int set[3001];
int dif;
cin>>n;
while (n && n > 0)
{
for (i=0;i<n;i++)
{
cin>>a[i];
set[i] = 0;
}
jolly = 1;
for (i=1;i<n;i++)
{
dif = abs(a[i] - a[i-1]);
if (dif != 0 && set[dif] == 0 && dif < n)
set[dif] = 1;
else
{
jolly = 0;
break;
}
}
if (jolly ==1)
cout<<"Jolly\n";
else
cout<<"Not jolly\n";
n = 0;
cin>>n;
}
return 0;
}
Code: Select all
code.cpp: In function 'int main()':
code.cpp:27: error: 'abs' was not declared in this scope
And then I fixed it as below to get ACCEPTED
Code: Select all
#include <iostream>
using namespace std;
int main()
{
int n;
int i;
int jolly = 0;
n = 0;
int a[3001];
int set[3001];
int dif;
cin>>n;
while (n && n > 0)
{
for (i=0;i<n;i++)
{
cin>>a[i];
set[i] = 0;
}
jolly = 1;
for (i=1;i<n;i++)
{
dif = a[i] - a[i-1];
if (dif < 0)
dif = - dif;
if (dif != 0 && set[dif] == 0 && dif < n)
set[dif] = 1;
else
{
jolly = 0;
break;
}
}
if (jolly ==1)
cout<<"Jolly\n";
else
cout<<"Not jolly\n";
n = 0;
cin>>n;
}
return 0;
}