Sorry, it only *looked* like it worked, I should've checked the input specification.
You probably want to use fgets(), I am not that familiar with C, maybe you should check C forum - I bet someone asked that question already. So, no, I don't have that much experience
I thought you were someone from U of C, no, I probably don't know you
public static void main(String[] args)throws Exception {
Scanner cin=new Scanner(System.in);
BufferedReader keyin=new BufferedReader(new InputStreamReader(System.in));
int choose=0;
int []original=new int[52];
int []position=new int[52];
int []result=new int[52];
int times=Integer.parseInt(keyin.readLine());
String suit="",value="";
String [][]ans=new String[100][52];
private String readLine() {
StringBuffer sb = new StringBuffer();
int b = 0;
while (b != '\n' && b >= 0) {
try {
b = System.in.read();
} catch (IOException e) {
return null;
}
if (b != '\r' && b != '\n' && b >= 0)
sb.append((char) b);
}
if (sb.length() == 0 && b < 0)
return null;
return sb.toString().trim(); // remove trim() if you need whitespace
}
Interesting... that compiles? Why are you implementing Runnable if you are running a single thread? Hm, I have to check if I can use threads after all...
Anyway - I haven't checked your code, but WA with Java might mean anything on UVa, including Run Time Error. [EDIT] It crashes with more than one test case.
And - next time, use already existing threads, don't create new ones.
Darko wrote:Interesting... that compiles? Why are you implementing Runnable if you are running a single thread? Hm, I have to check if I can use threads after all...
Anyway - I haven't checked your code, but WA with Java might mean anything on UVa, including Run Time Error. [EDIT] It crashes with more than one test case.
And - next time, use already existing threads, don't create new ones.
Sorry about that, but could you please elaborate on "crashes with more than one test case."
When I run it at home this is what I get when I use three cases
3
2
2 1 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 52 51
52 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 1
1
2
King of Spades
2 of Clubs
4 of Clubs
5 of Clubs
6 of Clubs
7 of Clubs
8 of Clubs
9 of Clubs
10 of Clubs
Jack of Clubs
Queen of Clubs
King of Clubs
Ace of Clubs
2 of Diamonds
3 of Diamonds
4 of Diamonds
5 of Diamonds
6 of Diamonds
7 of Diamonds
8 of Diamonds
9 of Diamonds
10 of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds
Ace of Diamonds
2 of Hearts
3 of Hearts
4 of Hearts
5 of Hearts
6 of Hearts
7 of Hearts
8 of Hearts
9 of Hearts
10 of Hearts
Jack of Hearts
Queen of Hearts
King of Hearts
Ace of Hearts
2 of Spades
3 of Spades
4 of Spades
5 of Spades
6 of Spades
7 of Spades
8 of Spades
9 of Spades
10 of Spades
Jack of Spades
Queen of Spades
Ace of Spades
3 of Clubs
2
2 1 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 49 41 42 43 44 45 46 47 48 40 50 52 51
52 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 1
1
2
King of Spades
2 of Clubs
4 of Clubs
5 of Clubs
6 of Clubs
7 of Clubs
8 of Clubs
9 of Clubs
10 of Clubs
Jack of Clubs
Queen of Clubs
King of Clubs
Ace of Clubs
2 of Diamonds
3 of Diamonds
4 of Diamonds
5 of Diamonds
6 of Diamonds
7 of Diamonds
8 of Diamonds
9 of Diamonds
10 of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds
Ace of Diamonds
2 of Hearts
3 of Hearts
4 of Hearts
5 of Hearts
6 of Hearts
7 of Hearts
8 of Hearts
9 of Hearts
10 of Hearts
Jack of Hearts
Queen of Hearts
King of Hearts
Ace of Hearts
Jack of Spades
3 of Spades
4 of Spades
5 of Spades
6 of Spades
7 of Spades
8 of Spades
9 of Spades
10 of Spades
2 of Spades
Queen of Spades
Ace of Spades
3 of Clubs
2
2 1 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 52 51
52 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 50 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 32 51 1
1
2
King of Spades
2 of Clubs
4 of Clubs
5 of Clubs
6 of Clubs
7 of Clubs
8 of Clubs
9 of Clubs
10 of Clubs
Jack of Clubs
Queen of Clubs
King of Clubs
Ace of Clubs
2 of Diamonds
3 of Diamonds
4 of Diamonds
5 of Diamonds
6 of Diamonds
7 of Diamonds
8 of Diamonds
9 of Diamonds
10 of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds
Ace of Diamonds
2 of Hearts
3 of Hearts
4 of Hearts
5 of Hearts
6 of Hearts
Queen of Spades
8 of Hearts
9 of Hearts
10 of Hearts
Jack of Hearts
Queen of Hearts
King of Hearts
Ace of Hearts
2 of Spades
3 of Spades
4 of Spades
5 of Spades
6 of Spades
7 of Spades
8 of Spades
9 of Spades
10 of Spades
Jack of Spades
7 of Hearts
Ace of Spades
3 of Clubs
I copied your Main.java, I just made 2 cases out of that sample one, and it crashes. Maybe you misunderstood something, but this is what the input file would look like:
I copied your Main.java, I just made 2 cases out of that sample one, and it crashes. Maybe you misunderstood something, but this is what the input file would look like:
2
2
2 1 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 52 51
52 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 1
1
2
King of Spades
2 of Clubs
4 of Clubs
5 of Clubs
6 of Clubs
7 of Clubs
8 of Clubs
9 of Clubs
10 of Clubs
Jack of Clubs
Queen of Clubs
King of Clubs
Ace of Clubs
2 of Diamonds
3 of Diamonds
4 of Diamonds
5 of Diamonds
6 of Diamonds
7 of Diamonds
8 of Diamonds
9 of Diamonds
10 of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds
Ace of Diamonds
2 of Hearts
3 of Hearts
4 of Hearts
5 of Hearts
6 of Hearts
7 of Hearts
8 of Hearts
9 of Hearts
10 of Hearts
Jack of Hearts
Queen of Hearts
King of Hearts
Ace of Hearts
2 of Spades
3 of Spades
4 of Spades
5 of Spades
6 of Spades
7 of Spades
8 of Spades
9 of Spades
10 of Spades
Jack of Spades
Queen of Spades
Ace of Spades
3 of Clubs
2
2 1 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 52 51
52 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 1
1
2
King of Spades
2 of Clubs
4 of Clubs
5 of Clubs
6 of Clubs
7 of Clubs
8 of Clubs
9 of Clubs
10 of Clubs
Jack of Clubs
Queen of Clubs
King of Clubs
Ace of Clubs
2 of Diamonds
3 of Diamonds
4 of Diamonds
5 of Diamonds
6 of Diamonds
7 of Diamonds
8 of Diamonds
9 of Diamonds
10 of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds
Ace of Diamonds
2 of Hearts
3 of Hearts
4 of Hearts
5 of Hearts
6 of Hearts
7 of Hearts
8 of Hearts
9 of Hearts
10 of Hearts
Jack of Hearts
Queen of Hearts
King of Hearts
Ace of Hearts
2 of Spades
3 of Spades
4 of Spades
5 of Spades
6 of Spades
7 of Spades
8 of Spades
9 of Spades
10 of Spades
Jack of Spades
Queen of Spades
Ace of Spades
3 of Clubs
Again - what is the thing you are posting? Is that the output of your program? Can you describe how you test your solution (in detail)?
And - having the latest JDK doesn't help here - I am compiling it in Eclipse to target 1.3. Java here is 1.1, but 1.3 is the lowest Eclipse can do.
Anyway - I put that input into the file called "in", compile Main.java and run it with "java Main < in". I am not sure why you don't want to accept the fact that it crashes?
I shouldn't be debugging this for you - but here it is: You assume that the file ends with a blank line. It does not. And it might not end with anything by EOF. And - I don't think it would get accepted anyway, seems like you are missing a newline at the end.
Darko wrote:Again - what is the thing you are posting? Is that the output of your program? Can you describe how you test your solution (in detail)?
And - having the latest JDK doesn't help here - I am compiling it in Eclipse to target 1.3. Java here is 1.1, but 1.3 is the lowest Eclipse can do.
Anyway - I put that input into the file called "in", compile Main.java and run it with "java Main < in". I am not sure why you don't want to accept the fact that it crashes?
I shouldn't be debugging this for you - but here it is: You assume that the file ends with a blank line. It does not. And it might not end with anything by EOF. And - I don't think it would get accepted anyway, seems like you are missing a newline at the end.
Unbelievable. I never would have thought it possible. Well you were right, using an input file does crash the program.
You see I have only been using the cmd window and simply copied and pasted input into it. It was never a problem for me so far as I already solved a few problems using only the cmd window.
I had no idea I was supposed to be using an input file.
I would like to show my appreciation by saying thank you.
import java.io.*;
import java.util.*;
class Question5 {
static BufferedReader br;
static ArrayList<Integer> deck;
static ArrayList<String[]> shuffleList;
static String[] shuffleForm;
static String[] cardNum = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};
public static void main (String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
br.readLine();
//for every set of input
for (int i=0; i<n; i++) {
initDeck();
readShuffle();
test();
toText();
System.out.println();
shuffleList.clear();
shuffleForm = null;
}
}
//initialize deck
public static void initDeck() {
deck = new ArrayList<Integer>(52);
deck.clear();
for (int i=1; i<=52; i++) {
deck.add(i);
}
}
//read number of shuffle
public static void readShuffle() throws IOException {
int numShuffle = Integer.parseInt(br.readLine());
shuffleList = new ArrayList<String[]>(numShuffle);
for (int i=0; i<numShuffle; i++) {
String line = br.readLine();
line += " " + br.readLine();
StringTokenizer st = new StringTokenizer(line, " ");
shuffleForm = new String[52];
for (int j=0; j<shuffleForm.length; j++) {
shuffleForm[j] = st.nextToken();
}
shuffleList.add(shuffleForm);
}
}
//shuffle process
public static void test() throws IOException {
int count = 0;
String input = br.readLine();
while (br.ready() && !input.equals("")) {
int shuffleNum = Integer.parseInt(input);
shuffle(shuffleNum);
count++;
input = null;
input = br.readLine();
}
}
//shuffle the deck
public static void shuffle(int shuffleNum) {
String[] shuffleUsed = shuffleList.get(shuffleNum-1);
ArrayList<Integer> newDeck = new ArrayList<Integer>(52);
for (int i=0; i<shuffleUsed.length; i++) {
int pos = Integer.parseInt(shuffleUsed[i]);
newDeck.add(deck.get(pos-1));
}
deck = newDeck;
}
//convert card from number to text
public static void toText() {
for (int i=0; i<deck.size(); i++) {
String suit;
int card = deck.get(i);
if (card<13) {
suit = "Clubs";
}
else if (card<26) {
suit = "Diamonds";
}
else if (card<39) {
suit = "Hearts";
}
else {
suit = "Spades";
}
while (card>13) {
card-=13;
}
System.out.println(cardNum[card-1] + " of " + suit);
}
}
}
Can someone help me too? I have tried multiple input and all the stuffs and it runs well. But when I submit it says Runtime error. Please someone help me...