Contests and their Solutions
Started by TcM, Sep 22 2008 07:18 AM
37 replies to this topic
#1
Posted 22 September 2008 - 07:18 AM
Contests and their Solutions.
Well chili5 gave me these links:
USACO Training Program Gateway
Canadian Computing Competition
DWITE: Online Computer Programming Contest
I decided to pick up some problems and try them in C#, PHP and Java. I will pick some of them from time to time and work on it, afterward I will submit them here. I will not submit all of the solutions in all 3 languages because I'm still learning them.. so I'm not able to do everything with the 3 of them.. but I will try my best.
Some of the contests require input from a file, I will ignore that and just ask for normal input, and output will be shown on the screen.
You are free to submit your own solutions, comment or express your feeling about the solutions! Just follow the format of the post below!
Don't make off-topic posts, I WILL DELETE THEM!
I'm doing this just for fun and for practice.. I won't really submit these contests.
Well chili5 gave me these links:
USACO Training Program Gateway
Canadian Computing Competition
DWITE: Online Computer Programming Contest
I decided to pick up some problems and try them in C#, PHP and Java. I will pick some of them from time to time and work on it, afterward I will submit them here. I will not submit all of the solutions in all 3 languages because I'm still learning them.. so I'm not able to do everything with the 3 of them.. but I will try my best.
Some of the contests require input from a file, I will ignore that and just ask for normal input, and output will be shown on the screen.
You are free to submit your own solutions, comment or express your feeling about the solutions! Just follow the format of the post below!
Don't make off-topic posts, I WILL DELETE THEM!
I'm doing this just for fun and for practice.. I won't really submit these contests.
|
|
|
#2
Posted 22 September 2008 - 07:18 AM
Original Problem:
http://dwite.ca/ques..._a_pyramid.html
C#
Java
PHP
http://dwite.ca/ques..._a_pyramid.html
Quote
A pyramid is to be constructed out of stone cubes, 1 meter in length each. The pyramid will have a square base, and will be completely solid. Given the dimensions for the length of the base, and the height of the pyramid, and assuming that no material is wasted, calculate the minimum number of cube stones required to construct a pyramid of the supplied dimensions.
The input file DATA2.txt will contain two lines with one real values on each line, L and H; 0.00 < L, H <= 100.00 representing the Length of the base and the Height of the pyramid to be constructed.
The output file OUT2.txt will contain a single integer, representing the minimum number of blocks required for the construction.
The input file DATA2.txt will contain two lines with one real values on each line, L and H; 0.00 < L, H <= 100.00 representing the Length of the base and the Height of the pyramid to be constructed.
The output file OUT2.txt will contain a single integer, representing the minimum number of blocks required for the construction.
C#
Console.Write("Please Enter Base Length: ");
float L = float.Parse(Console.ReadLine());
Console.Write("Please Enter Height of Pyramid: ");
float H = float.Parse(Console.ReadLine());
float cubesNeeded = ((L * L) * H) / 3;
int ansCubes = (int)Math.Ceiling(cubesNeeded);
Console.Write("Total Number Of stones needed is: {0} stones", ansCubes);
Console.ReadLine();
Java
String l = JOptionPane.showInputDialog("Please Enter Base Length");
float L = Float.parseFloat(l);
String h = JOptionPane.showInputDialog("Please Enter Height of Pyramid");
float H = Float.parseFloat(h);
float cubesNeeded = ((L * L) * H) / 3;
int ansCubes = (int)Math.ceil(cubesNeeded);
System.out.print(ansCubes);
PHP
<?php $l = $_GET["l"]; $h = $_GET["h"]; $cubesNeeded = (($l * $l) * $h) / 3; echo ceil($cubesNeeded); ?>
#3
Guest_Jordan_*
Posted 22 September 2008 - 07:45 AM
Guest_Jordan_*
Nice work, you should probably move this to the code section instead of General Programming though.
#4
Posted 22 September 2008 - 08:37 AM
Thanks.
I moved the thread.
Another solution is coming in C#, maybe I'll try Java and perhaps PHP too. I want to learn these languages correctly!
I moved the thread.
Another solution is coming in C#, maybe I'll try Java and perhaps PHP too. I want to learn these languages correctly!
#5
Posted 22 September 2008 - 10:24 AM
Original Problem:
Your Ride Is Here
C#
Java - UPDATE
PHP
Your Ride Is Here
C#
int[] NumG = new int[6];
int[] NumC = new int[6];
ArrayList Letter = new ArrayList();
int letterCodeG = 0;
int letterCodeC = 0;
int finalCodeG = 1;
int finalCodeC = 1;
float finalCodeGGroup = 0;
float finalCodeCComet = 0;
Letter.Add(""); //Leave this empty, because it starts from index 0
Letter.Add("A");
Letter.Add("B");
Letter.Add("C");
Letter.Add("D");
Letter.Add("E");
Letter.Add("F");
Letter.Add("G");
Letter.Add("H");
Letter.Add("I");
Letter.Add("J");
Letter.Add("K");
Letter.Add("L");
Letter.Add("M");
Letter.Add("N");
Letter.Add("O");
Letter.Add("P");
Letter.Add("Q");
Letter.Add("R");
Letter.Add("S");
Letter.Add("T");
Letter.Add("U");
Letter.Add("V");
Letter.Add("W");
Letter.Add("X");
Letter.Add("Y");
Letter.Add("Z");
//GROUP
Console.Write("Please Enter Group Name: ");
string inputG = Console.ReadLine();
inputG = inputG.ToUpper();
for (int i = 0; i < inputG.Length; i++)
{
string stringLetterG = "" + inputG[i];
letterCodeG = Letter.IndexOf(stringLetterG);
NumG[i] = letterCodeG;
}
for (int i = 0; i < inputG.Length; i++)
{
finalCodeG = finalCodeG * (0+ NumG[i]);
}
finalCodeGGroup = finalCodeG % 47;
//COMET
Console.Write("Please Enter Comet Name: ");
string inputC = Console.ReadLine();
inputC = inputC.ToUpper();
for (int i = 0; i < inputC.Length; i++)
{
string stringLetterC = "" + inputC[i];
letterCodeC = Letter.IndexOf(stringLetterC);
NumC[i] = letterCodeC;
}
for (int i = 0; i < inputC.Length; i++)
{
finalCodeC = finalCodeC * (0 + NumC[i]);
}
finalCodeCComet = finalCodeC % 47;
if (finalCodeCComet == finalCodeGGroup)
Console.WriteLine("GO");
else
Console.WriteLine("STAY");
Console.ReadKey();
Java - UPDATE
//Declare Variables
int finalCodeG = 1;
int finalCodeC = 1;
float finalCodeGGroup = 0;
float finalCodeCComet = 0;
//GROUP
String inputG = JOptionPane.showInputDialog("Input Group");
for(int i = 0;i <= inputG.length()-1;i++)
{
finalCodeG = ((int)inputG.charAt(i)-64) * finalCodeG;
}
finalCodeGGroup = finalCodeG % 47;
//COMET
String inputC = JOptionPane.showInputDialog("Input Comet");
for(int i = 0;i <= inputC.length()-1;i++)
{
finalCodeC = ((int)inputC.charAt(i)-64) * finalCodeC;
}
finalCodeCComet = finalCodeC % 47;
if(finalCodeCComet == finalCodeGGroup)
System.out.print("GO");
else
System.out.print("STAY");
PHP
None. Maybe sometime in the future.
Edited by TcM, 23 September 2008 - 11:18 AM.
UPDATE
#6
Posted 22 September 2008 - 10:35 AM
You have a lot of unnecessary code, I'll do a solution real quick.
#7
Posted 22 September 2008 - 10:38 AM
Yeah I knew I had too much code for such a simple task, but I couldn't bother making it more compact.. I like to copy/paste lol.
#8
Posted 22 September 2008 - 11:05 AM
Original Problem:
Your Ride Is Here
Here is the solution in C++.
Your Ride Is Here
Here is the solution in C++.
/* Solution to Problem */
#include <iostream>
using namespace std;
int Calculated(char * calc_me);
int main() {
char * comet_name = (char*)calloc(sizeof(char), 7);
if (comet_name == NULL) {return 0;}
char * group_name = (char*)calloc(sizeof(char), 7);
if (group_name == NULL) {free(comet_name); return 0;}
cout << "Enter the name of the comet: ";
cin >> comet_name;
cout << "Enter the name of the group: ";
cin >> group_name;
if ((Calculated(comet_name) % 47) == (Calculated(group_name) % 47)) {
cout << "GO";
}
else {
cout << "STAY";
}
free (group_name);
free (comet_name);
return 0;
}
int Calculated (char * calc_me) {
int i;
int total = 1;
for (i = 0; i < 7; i++) {
if (calc_me[i] == '0') {break;}
total *= calc_me[i] - 64;
}
return total;
}
#9
Posted 22 September 2008 - 11:13 AM
Where do you define that A = 1, B = 2, C = 3 etc?
#10
Posted 22 September 2008 - 11:15 AM
I don't need to do that.
I just took 64 away from there ASCII value.
Voila.
I just took 64 away from there ASCII value.
Voila.
#11
Posted 22 September 2008 - 11:29 AM
Dang! How come I didn't think of that...
#12
Posted 22 September 2008 - 11:40 AM
My solution:
Your Ride Is Here
PHP
Doesnt get more simple than this. However, it uses uses arguments rather than prompting for input. Does the same thing though.
Your Ride Is Here
PHP
<?php
function ride($input) {
$x = 1;
for($i = 0; $i < strlen($input); $i++) {
$x *= (ord($input[$i])-64);
}
return $x%47;
}
echo (ride($argv[1]) == ride($argv[2])) ? "GO" : "STAY";
?>
Doesnt get more simple than this. However, it uses uses arguments rather than prompting for input. Does the same thing though.
Edited by TcM, 22 September 2008 - 11:46 AM.
Added URL


Sign In
Create Account


Back to top









