Jolly Jumpers

From Progteam

Revision as of 22:14, 22 June 2008 by Mlc413 (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Checkmark.jpg This problem has been solved by kerry.
Checkmark.jpg This problem has been solved by mlc413.



Jolly Jumpers
Problem Number 2575
Sorter: kerry
Source: Unknown
Link: http://acm.pku.edu.cn/JudgeOnline/problem?id=2575



Jolly Jumpers is problem number 2575 on the Peking University ACM site.

Kerry's C answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>


int main() {
int i, num;
int input[3000];
        while(1) {
                for(i=0;i<3000;i++)
                        input[i]=0;
                num=get_input(input);
                if(num<=0) {
                        printf("Not jolly\n");
                        continue;
                }
                if (num==50000)
                        return;
                get_answer(input, num);
        }

}

int get_input(int input[3000]) {
int num, i;
        if(scanf("%d", &num)==EOF)
                return(50000);
        if(num<=0)
                while(getchar()!='\n'){}
        for(i=0;i<num;i++)
                scanf("%d", &input[i]);
        return(num);

}

int compare(const void *a, const void *b) {
        return(*(int*)a-*(int*)b);
}

int get_answer(int input[3000], int num){
int i, answer[3000];

        if (num==1) {
                printf("Jolly\n");
                return;
        }
        for(i=0;i<3000;i++)
                answer[i]=0;
        for(i=0;i<num-1;i++)
                answer[i]=abs(input[i]-input[i+1]);

        qsort(answer, num-1, sizeof(int), compare);
        for(i=1;i<num;i++) {
                if(answer[i-1]!=i) {
                        printf("Not jolly\n");
                        return;
                }
        }
        printf("Jolly\n");
        return;
}


Melanie's Java Solution :



import java.util.*;

public class Main{

    public static Scanner in;
 
    public static void main(String[] args){
        in=new Scanner(System.in);

        doStuff();
    }

    public static void doStuff(){
       
	while(in.hasNextInt()){
	    int N = in.nextInt();
	    solve(N);
	}
    }

    public static void solve(int N){
	int[] input = new int[N];
	int[] diffs = new int[N-1];
	for(int i = 0; i < N; i++){
	    input[i] = in.nextInt();
	}
	
	for(int i =0; i < N-1; i++){
	    diffs[i] = Math.abs( input[i]-input[i+1]);
	}
	
	
	boolean jolly = true;
	Arrays.sort(diffs);
	for(int i = 0; i < N -1; i++){
	    if(diffs[i]  != i+1)
		jolly = false;
	}
	
	if(jolly){
	    System.out.println("Jolly");
	}
	else{
	    System.out.println("Not jolly");
	}
    }
}


Personal tools