Re: 10038 - Jolly Jumpers
Posted: Thu Jul 01, 2010 10:41 am
jenovaforum wrote:This is the point where every 1 becomes fool....
Tnx for ur post i got AC without posting anything....
Sometime's other's mistakes can be ur Idea
![]()
![]()
jenovaforum wrote:This is the point where every 1 becomes fool....
Tnx for ur post i got AC without posting anything....
Sometime's other's mistakes can be ur Idea
![]()
![]()
Code: Select all
#include<stdio.h>
#include<iostream.h>
int main()
{
long long i=0,temp1,temp2,dif,flag;
int n,check=1;
struct node
{
long long data;
node *next;
}*list,*tmp,*last;
list=NULL;
while(scanf("%d",&n)==1)
{
flag=0;
dif=1;
check=1;
if(n==1)
{
scanf("%lld",&temp2);
printf("Not jolly\n");
}
else {
for(i=0;i<n;i++)
{
scanf("%lld",&temp2);
if(flag==1)
continue;
if(i==0)
temp1=temp2;
else dif=temp2-temp1;
temp1=temp2;
if(dif<0) dif=-dif;
if(dif>(n-1)) flag=1;
if(i>0&&list==NULL)
{
if(dif<0)
{
list=new node;
list->data=-dif;
}
else
{
list=new node;
list->data=dif;
}
list->next=NULL;
}
else if(i>1)
{
tmp=list;
last=tmp;
while(tmp->data<=dif&&tmp!=NULL)
{
last=tmp;
tmp=tmp->next;
}
if(last->data==dif)
flag=1;
else
{
node *tmp1;
tmp1=new node;
if(last==list)
{
tmp1->data=dif;
tmp1->next=list;
list=tmp1;
}
else
{
tmp1->next=tmp;
tmp1->data=dif;
last->next=tmp1;
}
}//end of flag1 else
}//end of else
}//end of for
if(flag==1)
printf("Not jolly\n");
else
{
//printf("dif=%d\n",(tmp->data)-(last->data));
last=list;
tmp=list;
while(tmp!=NULL)
{
if(tmp->data!=check)
{
printf("Not jolly\n");
break;
}
tmp=tmp->next;
check++;
}//end of nested while
if(tmp==NULL)
printf("Jolly\n");
else printf("Not jolly\n");
}
}//end of n=1 condition
}//end of while
exit(0);
return 0;
}Code: Select all
Removed after AC
That made me silly"Not Jolly" should be "Not jolly"
Code: Select all
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int inp;
bool list [3000];
int jumpers [3005];
string input;
int total_numbers;
void clear_first_number()
{
int initial = 0;
int final = 0;
char buffer;
for (int counter = 0; counter == counter ;++counter)
{
buffer = input[counter];
if (buffer == ' ')
{
final = counter;
++ final;
break;
}
}
input.erase(0, final);
}
int subtract (int one, int two)
{
int difference = one - two;
if (difference < 0)
{
difference = -1*difference;
}
return difference;
}
int main()
{
for (;;)// main loop till no input
{
int is_jolly = 1;
input.clear();
getline (cin, input);
if (input.length() == 0)
{
return 0;
}
stringstream(input) >> total_numbers;
clear_first_number();
for (int i = 0 ; i < total_numbers; ++i)//process input string
{
stringstream(input) >> inp;
jumpers[i] = inp;
clear_first_number();
}
//create subtractions
for (int j = 0; j < total_numbers ; ++j)
{
int difference = subtract(jumpers[j], jumpers[j+1]);
jumpers[j] = difference;
}
//set bool array all to false
for (int e = 0; e < total_numbers; ++e)
{
list [e] = false;
}
//make those indexes true which are present
for (int t = 0; t < total_numbers ; ++t)
{
list[jumpers[t]-1] = true;
}
//check if jolly
for (int d = 0; d < total_numbers-1; ++d)
{
if (list[d] == false)
{
is_jolly = 0;
}
}
//print statements
if (is_jolly == 1)
{
cout << "Jolly\n";
}
else
{
cout << "Not jolly\n";
}
}
}
Code: Select all
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int abs(int n);
map<int,int> sequence(int n);
void readInput();
int main()
{
while(cin)
{
readInput();
}
return 0;
}
int abs(int n)
{
int absoluteValue = n > 0 ? n : -n;
return absoluteValue;
}
/* Takes a number n and returns a map with a sequence of
integers from n to n - 1 as the key, and a value of 0. */
map<int,int> sequence(int n)
{
map<int,int> seq;
while(n > 1){
n--;
seq.insert(make_pair(n,0));
}
return seq;
}
void readInput()
{
bool jolly = true;
vector<int> absolutes;
int n;
int next;
int prev;
int count = 0;
cin>>n;
map<int,int>seq = sequence(n);
while(count < n)
{
if(count == 0)
{
prev = 0;
}
cin >> next;
absolutes.push_back(abs(prev - next));
prev = next;
count++;
}
for(int i = 0;i < absolutes.size();i++){
seq[absolutes[i]]++;
}
for(map<int,int>::const_iterator it = seq.begin();
it != seq.end();it++){
if(it->second == 0)
{
jolly = false;
}
}
if(jolly || n == 1)
{
cout << "Jolly"<<endl;
}
else
{
cout << "Not jolly"<<endl;
}
}
Code: Select all
import java.util.Scanner;
import java.util.HashSet;
class Main
{
public static void main(String []args)
{
Main jolly = new Main();
jolly.jollyJumper();
}
void jollyJumper()
{
java.util.Scanner console = new java.util.Scanner(System.in);
int n = console.nextInt();
int previous,ctr=1;
HashSet set = new HashSet();
if (n==1)
{
console.nextInt();
System.out.println("Jolly");
}
else
{
previous = console.nextInt();
while(n!=ctr)
{
set.add(Math.abs(previous - (previous = console.nextInt())));
ctr++;
}
if(set.size()== n-1)
{
if (isJolly(set,n-1))
System.out.println("Jolly");
else
System.out.println("Not jolly");
}
else
System.out.println("Not jolly");
}
}
boolean isJolly(HashSet diff,int n)
{
for(int ctr = 1; ctr<=n;ctr++)
{
if(!diff.contains(ctr))
return false;
}
return true;
}
}
DigitalVoid wrote:Heres my code and im wondering why im having WA. I tried those test cases on the board and it pass
Code: Select all
import java.util.Scanner; import java.util.HashSet; class Main { public static void main(String []args) { Main jolly = new Main(); jolly.jollyJumper(); } void jollyJumper() { java.util.Scanner console = new java.util.Scanner(System.in); int n = console.nextInt(); int previous,ctr=1; HashSet set = new HashSet(); if (n==1) { console.nextInt(); System.out.println("Jolly"); } else { previous = console.nextInt(); while(n!=ctr) { set.add(Math.abs(previous - (previous = console.nextInt()))); ctr++; } if(set.size()== n-1) { if (isJolly(set,n-1)) System.out.println("Jolly"); else System.out.println("Not jolly"); } else System.out.println("Not jolly"); } } boolean isJolly(HashSet diff,int n) { for(int ctr = 1; ctr<=n;ctr++) { if(!diff.contains(ctr)) return false; } return true; } }