Wednesday, August 24, 2011

101 The Blocks Problem

I'll dedicate some time from now on to solve programming problems using Java, as I've used only c++  in the past, and having a working knowledge of java is a must in the professional practice these days.

http://uva.onlinejudge.org/external/1/101.pdf

This is just the beginning as my time is very scarce lately:

[sourcecode language="java"]

package blocks_problem;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String line;
int numBlocks;
line = cin.nextLine();
numBlocks = Integer.parseInt(line);
System.out.println("Our block world has "+numBlocks+" blocks");
while(cin.hasNextLine()) {
line = cin.nextLine();
if(line.contentEquals("quit"))break;
String tokens[]  = line.split("\\s+");
for(int i=0; i<4; i++){
System.out.println(tokens[i]);
}
}
}
}

[/sourcecode]

 

No comments:

Post a Comment