Search found 13 matches

by hedgehog1204
Mon Aug 21, 2006 8:54 am
Forum: Volume 1 (100-199)
Topic: 106 - Fermat vs. Pythagoras
Replies: 138
Views: 30389

Try using less multidimensional arrays. Here is the example of my code in Python:

def prime(i, j, k):
myMax = max([i, j, k])
for l in range(2, myMax):
if i%l==0 and j%l==0 and k%l==0:
return False
return True

file = open('input.txt')
for line in file:
nTriple = 0
n = int(line.strip ...
by hedgehog1204
Mon Aug 21, 2006 7:57 am
Forum: Volume 1 (100-199)
Topic: 105 - The Skyline Problem
Replies: 160
Views: 51459

Try this approach (Python):


max = 10000
h = [0 for i in range(max)]
file = open('input.txt')
for line in file:
vars = line.split()
for i in range(int(vars[0]), int(vars[2])):
if h[i] < int(vars[1]):
h[i] = int(vars[1])
i = 0
while h[i] == 0:
i += 1
print i, h[i],
hi = h[i]
for j in range(i+2, max):
if h[j] != hi ...
by hedgehog1204
Mon Aug 21, 2006 7:56 am
Forum: Volume 1 (100-199)
Topic: 105 - The Skyline Problem
Replies: 160
Views: 51459

Try this approach:


max = 10000
h = [0 for i in range(max)]
file = open('input.txt')
for line in file:
vars = line.split()
for i in range(int(vars[0]), int(vars[2])):
if h[i] < int(vars[1]):
h[i] = int(vars[1])
i = 0
while h[i] == 0:
i += 1
print i, h[i],
hi = h[i]
for j in range(i+2, max):
if h[j] != hi ...
by hedgehog1204
Mon Aug 21, 2006 7:53 am
Forum: Volume 1 (100-199)
Topic: 105 - The Skyline Problem
Replies: 160
Views: 51459

This works for me


max = 10000
h = [0 for i in range(max)]
file = open('input.txt')
for line in file:
vars = line.split()
for i in range(int(vars[0]), int(vars[2])):
if h[i] < int(vars[1]):
h[i] = int(vars[1])
i = 0
while h[i] == 0:
i += 1
print i, h[i],
hi = h[i]
for j in range(i+2, max):
if h[j] != hi ...
by hedgehog1204
Mon Aug 21, 2006 7:50 am
Forum: Volume 1 (100-199)
Topic: 105 - The Skyline Problem
Replies: 160
Views: 51459

It can be shorter


max = 10000
h = [0 for i in range(max)]
file = open('input.txt')
for line in file:
vars = line.split()
for i in range(int(vars[0]), int(vars[2])):
if h[i] < int(vars[1]):
h[i] = int(vars[1])
i = 0
while h[i] == 0:
i += 1
print i, h[i],
hi = h[i]
for j in range(i+2, max):
if h[j] != hi ...
by hedgehog1204
Mon Aug 21, 2006 7:48 am
Forum: Volume 1 (100-199)
Topic: 105 - The Skyline Problem
Replies: 160
Views: 51459

This can be shorter

This can be shorter. This is in Python, but can be easily converted to whatever:

# Program that solves The Skyline Problem
# http://acm.uva.es/p/v1/105.html

max = 10000 # maximum x coordinates
h = [0 for i in range(max)] # skyline heights for x=i

file = open('input.txt')
for line in file ...
by hedgehog1204
Sun Aug 20, 2006 1:09 pm
Forum: Volume 1 (100-199)
Topic: 102 - Ecological Bin Packing
Replies: 485
Views: 116808

This problem can be solved without using arrays

just linear programming and single data types. Here is the example in Python (easy to convert to other languages):

# Program that solves the Ecological Bin Packing problem
# http://acm.uva.es/p/v1/102.html

file = open('input.txt')
for line in file:
vars = line.strip().split(' ') # get numbers ...
by hedgehog1204
Sun Aug 20, 2006 12:58 pm
Forum: Volume 1 (100-199)
Topic: 101 - The Blocks Problem
Replies: 635
Views: 108923

Use comments/subrountines/stacks

Your code will look better if you use comments/subroutines. Also, this problem would be solved more efficiently using stacks. Here is the example of the Python program (feel free to convert it to C++):


# Program that solves The Blocks Problem
# http://acm.uva.es/p/v1/101.html

# class Robot that ...
by hedgehog1204
Sun Aug 20, 2006 12:50 pm
Forum: Volume 1 (100-199)
Topic: 101 - The Blocks Problem
Replies: 635
Views: 108923

and without comments

it will look like this:

class Robot:

def __init__(self, size):
self.n = size
self.blocks = [[i] for i in range(size)]

def __str__(self):
tempStr = ""
for i in range(self.n):
tempStr += str(i)+":"
for el in self.blocks[i]:
tempStr += " "+str(el)
tempStr += "\n"
return tempStr

def ...
by hedgehog1204
Sun Aug 20, 2006 12:47 pm
Forum: Volume 1 (100-199)
Topic: 101 - The Blocks Problem
Replies: 635
Views: 108923

your code is too complicated

You should use functions/subroutines in your program, or OOP. Here is the example of what it should like in Python (feel free to translate it into C++):

# Program that solves The Blocks Problem
# http://acm.uva.es/p/v1/101.html

# class Robot that solves The Blocks Problem
class Robot:
# Class ...
by hedgehog1204
Sun Aug 20, 2006 12:28 pm
Forum: Volume 1 (100-199)
Topic: 103 - Stacking Boxes
Replies: 200
Views: 50442

...and without comments its cuter:

# Program that solves the Stacking Boxes problem
# http://acm.uva.es/p/v1/103.html

boxes = []
nBox = 0
dim = 0

# sort boxes by their measurements using bubble sort
def sort_boxes():
for i in range(nBox-1):
for j in range(nBox-i-1):
for k in range(dim ...
by hedgehog1204
Sun Aug 20, 2006 12:24 pm
Forum: Volume 1 (100-199)
Topic: 103 - Stacking Boxes
Replies: 200
Views: 50442

Python program for 103

Here is cute Python code to solve the 103 problem:

# Program that solves the Stacking Boxes problem
# http://acm.uva.es/p/v1/103.html

boxes = [] # list of boxes
nBox = 0 # number of boxes
dim = 0 # dimensions of boxes

# sort boxes by their measurements using bubble sort
def sort_boxes():
for i ...
by hedgehog1204
Sat Oct 04, 2003 12:05 am
Forum: Volume 1 (100-199)
Topic: 100 - The 3n + 1 problem
Replies: 1394
Views: 318271

Here is my code

Here is my code:
[cpp]#include <stdio.h>

int circleLength(int n)
{
int cl=1;

if (n==1) return 1;
else
{
for ( ;n!=1; cl++)
{
if (n%2 == 0) n=n/2;
else n = 3*n+1;
}
}
return cl;
}


main()
{
int i, j;
int cl;

while(scanf("%d %d", &i, &j) != EOF)
{
int max = 1;
printf("%d %d ", i ...

Go to advanced search