10038 - Jolly Jumpers
Moderator: Board moderators
Re: 10038 - Jolly Jumpers
Can anyone tell me why this code is not working ?
import java.util.*;
class JollyJumpers{
public static void main(String[] args){
try{
Scanner in=new Scanner(System.in);
StringTokenizer token;
int n;
while(in.hasNext()){
token=new StringTokenizer(in.nextLine());
n=Integer.parseInt(token.nextToken());
//System.out.println("("+n+")");
int[] a=new int[n];
int i=0;
while(token.hasMoreElements()){
a[i++]=Integer.parseInt(token.nextToken());
//System.out.println(a);
}
//ALGO START
System.out.println(jollyOrNot(a,n));
//ALGO END
}
System.exit(0);
}
catch(Exception e){
System.out.println(e);
}
}
public static String jollyOrNot(int [] a,int n){
ArrayList<Integer>results = new ArrayList<Integer>();
for(int j=0;j<n-1;j++){
int result=Math.abs(a[j]-a[j+1]);
if(result==0)
return("Not jolly");
if(result>=a.length)
return("Not jolly");
if(results.contains(result))
return("Not jolly");
results.add(result);
}
return("Jolly");
}
}
import java.util.*;
class JollyJumpers{
public static void main(String[] args){
try{
Scanner in=new Scanner(System.in);
StringTokenizer token;
int n;
while(in.hasNext()){
token=new StringTokenizer(in.nextLine());
n=Integer.parseInt(token.nextToken());
//System.out.println("("+n+")");
int[] a=new int[n];
int i=0;
while(token.hasMoreElements()){
a[i++]=Integer.parseInt(token.nextToken());
//System.out.println(a);
}
//ALGO START
System.out.println(jollyOrNot(a,n));
//ALGO END
}
System.exit(0);
}
catch(Exception e){
System.out.println(e);
}
}
public static String jollyOrNot(int [] a,int n){
ArrayList<Integer>results = new ArrayList<Integer>();
for(int j=0;j<n-1;j++){
int result=Math.abs(a[j]-a[j+1]);
if(result==0)
return("Not jolly");
if(result>=a.length)
return("Not jolly");
if(results.contains(result))
return("Not jolly");
results.add(result);
}
return("Jolly");
}
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10038 - Jolly Jumpers
Use class Main instead of JollyJumpers.
I also added another check to see if token hasMoreElements, if n=0, and didn't print the exception but I'm not sure if those are required.
Also see:http://uva.onlinejudge.org/index.php?op ... &Itemid=30
I also added another check to see if token hasMoreElements, if n=0, and didn't print the exception but I'm not sure if those are required.
Also see:http://uva.onlinejudge.org/index.php?op ... &Itemid=30
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 5
- Joined: Thu Aug 09, 2012 11:09 pm
Re: 10038 - Jolly Jumpers
ok , i've read through all the 18 pages of this topic and my code passed every test case mentioned here , but it still gives me WA !!
so , defiantly i missing something here , but i can't figure out what !!
here is the code :
so , defiantly i missing something here , but i can't figure out what !!
here is the code :
Code: Select all
#include <iostream>
#include <string>
#include <cmath>
#include <cstring>
#include <sstream>
#include <queue>
using namespace std ;
int main()
{
string tmp ;
stringstream ss ;
int n , largest , i = -1 , diff ;
queue<string> results ;
while(!cin.eof())
{
int* list ;
bool* coress , result = true ;
getline(cin , tmp) ;
ss << tmp ;
while(ss)
{
if(i == -1)
{
ss >> n ;
list = new int[n] ;
}
else
ss >> list[i] ;
i++ ;
}
ss.clear() ;
if(n == 1)
{
results.push("Jolly") ;
delete[] list , coress ;
i = -1 ;
continue ;
}
coress = new bool[n - 1] ;
memset(coress , false , sizeof(coress)) ;
for(i = 0 ; i < n - 1 ; i++)
{
diff = abs((float)list[i] - list[i+1]) ;
if(diff >= 1 && diff <= n - 1)
coress[diff - 1] = true ;
}
for(i = 0 ; i < n - 1 ; i++)
result = (result && coress[i]) ;
if(result)
results.push("Jolly") ;
else
results.push("Not jolly") ;
delete[] list , coress ;
i = -1 ;
}
while(!results.empty())
{
cout << results.front() << endl ;
results.pop() ;
}
return 0 ;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10038 - Jolly Jumpers
Don't use a float. My AC code uses scanf and doesn't read the input line by line.
Check input and AC output for thousands of problems on uDebug!
Re: 10038 - Jolly Jumpers
Any thought 's and input will help...
here is my input...
0
4 1 2 3 4
4 1 4 2 3
5 1 4 2 -1 6
10 1 2 3 4 5 6 7 8 9 10
10 1 2 4 7 11 16 22 29 37 46
10 -1 -2 -4 -7 -11 -16 -22 -29 -37 -46
10 -1 -1 -4 -7 -11 -16 -22 -29 -37 -46
1 1
2 1 2
2 2 1
4 0 4 2 3
4 1 3 2 4
1 2
6 1 4 3 7 5 10
1 1
5 3 4 2 3 5
1 10
4 1 4 2 3
5 1 4 2 -1 6
6 3 2 4 3 2 1
1 4
0
4 2342342234234234 34234235434534554 67574523423 23425346546546546546
9 10 5 1 4 6 12 19 27 26
4 1 4 2 3
5 1 4 2 -1 6
6 3 2 4 3 2 1
4 1 3 0 -1
4 1 3 2 -2
1 2000
2 1999 1998
3 1 2 4
3 4 2 1
3 104 102 101
3 -5 -7 -6
3 4 1 3
200 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
10 1 2 3 4 5 6 7 8 9 10
10 1 2 4 7 11 16 22 29 37 46
10 -1 -2 -4 -7 -11 -16 -22 -29 -37 -46
10 -1 -1 -4 -7 -11 -16 -22 -29 -37 -46
1 1
2 1 2
2 2 1
4 0 4 2 3
4 1 3 2 4
1 2
6 1 4 3 7 5 10
5 3 4 2 3 5
9 5 6 4 1 -3 2 8 15 7
9 10 5 1 4 6 12 19 27 36
9 10 5 1 4 6 12 19 27 26
2 0 257
2 1 4000
Here is my output.....
Jolly
Not jolly
Jolly
Not jolly
Not jolly
Jolly
Jolly
Not jolly
Jolly
Jolly
Jolly
Not jolly
Not jolly
Jolly
Jolly
Jolly
Not jolly
Jolly
Jolly
Not jolly
Not jolly
Jolly
Jolly
Not jolly
Jolly
Jolly
Not jolly
Not jolly
Jolly
Not jolly
Jolly
Jolly
Jolly
Jolly
Jolly
Jolly
Not jolly
Not jolly
Not jolly
Jolly
Jolly
Not jolly
Jolly
Jolly
Jolly
Not jolly
Not jolly
Jolly
Jolly
Not jolly
Jolly
Not jolly
Jolly
Not jolly
Not jolly
Kindly run the same input against your AC code..and let me know where could be the possibility of problem..
thanks in advance.
here is my input...
0
4 1 2 3 4
4 1 4 2 3
5 1 4 2 -1 6
10 1 2 3 4 5 6 7 8 9 10
10 1 2 4 7 11 16 22 29 37 46
10 -1 -2 -4 -7 -11 -16 -22 -29 -37 -46
10 -1 -1 -4 -7 -11 -16 -22 -29 -37 -46
1 1
2 1 2
2 2 1
4 0 4 2 3
4 1 3 2 4
1 2
6 1 4 3 7 5 10
1 1
5 3 4 2 3 5
1 10
4 1 4 2 3
5 1 4 2 -1 6
6 3 2 4 3 2 1
1 4
0
4 2342342234234234 34234235434534554 67574523423 23425346546546546546
9 10 5 1 4 6 12 19 27 26
4 1 4 2 3
5 1 4 2 -1 6
6 3 2 4 3 2 1
4 1 3 0 -1
4 1 3 2 -2
1 2000
2 1999 1998
3 1 2 4
3 4 2 1
3 104 102 101
3 -5 -7 -6
3 4 1 3
200 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
10 1 2 3 4 5 6 7 8 9 10
10 1 2 4 7 11 16 22 29 37 46
10 -1 -2 -4 -7 -11 -16 -22 -29 -37 -46
10 -1 -1 -4 -7 -11 -16 -22 -29 -37 -46
1 1
2 1 2
2 2 1
4 0 4 2 3
4 1 3 2 4
1 2
6 1 4 3 7 5 10
5 3 4 2 3 5
9 5 6 4 1 -3 2 8 15 7
9 10 5 1 4 6 12 19 27 36
9 10 5 1 4 6 12 19 27 26
2 0 257
2 1 4000
Here is my output.....
Jolly
Not jolly
Jolly
Not jolly
Not jolly
Jolly
Jolly
Not jolly
Jolly
Jolly
Jolly
Not jolly
Not jolly
Jolly
Jolly
Jolly
Not jolly
Jolly
Jolly
Not jolly
Not jolly
Jolly
Jolly
Not jolly
Jolly
Jolly
Not jolly
Not jolly
Jolly
Not jolly
Jolly
Jolly
Jolly
Jolly
Jolly
Jolly
Not jolly
Not jolly
Not jolly
Jolly
Jolly
Not jolly
Jolly
Jolly
Jolly
Not jolly
Not jolly
Jolly
Jolly
Not jolly
Jolly
Not jolly
Jolly
Not jolly
Not jolly
Kindly run the same input against your AC code..and let me know where could be the possibility of problem..
thanks in advance.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10038 - Jolly Jumpers
Check input and AC output for thousands of problems on uDebug!
Re: 10038 - Jolly Jumpers
Removed code after AC.
Last edited by gr81 on Tue Nov 27, 2012 3:24 am, edited 1 time in total.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10038 - Jolly Jumpers
Don't read from a file.
Check input and AC output for thousands of problems on uDebug!
Re: 10038 - Jolly Jumpers
freopen line is only for debug purpose on my PC, else it will be excluded in actual submission.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10038 - Jolly Jumpers
Instead of:
memset(jolly, 0, 3001);
Use:
memset(jolly, 0, sizeof(jolly));
memset(jolly, 0, 3001);
Use:
memset(jolly, 0, sizeof(jolly));
Check input and AC output for thousands of problems on uDebug!
Re: 10038 - Jolly Jumpers
I appreciate your help,but I am looking for any logical error. why I am getting WA in this source code.
thanks.
thanks.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10038 - Jolly Jumpers
That is your logical error and why you are getting WA. Change that one line as I described and remove the freopen line and you'll get AC.
http://www.cplusplus.com/reference/clib ... ng/memset/
memset's third argument is the number of bytes. An int is four bytes not one. The following lines are all equivalent and will accomplish what you were trying to do.
memset(jolly, 0, sizeof(jolly));
memset(jolly, 0, 3001*sizeof(int));
memset(jolly, 0, 3001*4);
memset(jolly, 0, 12004);
As you can see, writing memset(jolly, 0, 3001) will not clear the entire array. It would be straightforward to create a test case that your code fails.
http://www.cplusplus.com/reference/clib ... ng/memset/
memset's third argument is the number of bytes. An int is four bytes not one. The following lines are all equivalent and will accomplish what you were trying to do.
memset(jolly, 0, sizeof(jolly));
memset(jolly, 0, 3001*sizeof(int));
memset(jolly, 0, 3001*4);
memset(jolly, 0, 12004);
As you can see, writing memset(jolly, 0, 3001) will not clear the entire array. It would be straightforward to create a test case that your code fails.
Check input and AC output for thousands of problems on uDebug!
Re: 10038 - Jolly Jumpers
My God....I can't believe i made that mistake...thanks Brian..I will make the change and will try to submit again...
Thanks once again..
Thanks once again..
10038 - Jolly Jumpers
Code: Select all
code removed
Last edited by shondhi on Thu Nov 01, 2012 8:12 pm, edited 1 time in total.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10038 - Jolly Jumpers
print "Not jolly" instead of "Not Jolly"
Check input and AC output for thousands of problems on uDebug!