Posts Tagged 'SPOJ'

I’m learning C++!

I thought it’d be nice to learn C++ alongside Python, so I submitted the same algorithm for SPOJ project code TEST in C++:

#include <iostream>
#include <cstdlib>

using namespace std;

int main () {
	int num;
	num = 0;
	while (num != 42) {
		cin >> num;
		if (num == 42) {
			return 0;
			}
		cout << num << '\n';
		}
	return 0;
	}

w00t yeah. Intrepid comes with a newer version of libtools than KDevelop currently has, so it wouldn’t compile correctly at first. I found this useful ubuntuforums.org post to help me out.

btw, for the purposes of this post, I indented the code above using lots of &nbsp;, which is really annoying. Does anyone have a better solution?

Advertisements

SPOJ — programming exercises

The Sphere Online Judge has thousands of programming exercises you can try (submit your code and see if it’s correct!) in lots of different languages. I’m only a (extremely) novice python programmer; here is my first submission to exercise 1 (code TEST):

#!/usr/bin/env python

n = 0
while n != 42:
    n = input()
    if n == 42:
        exit()
    print n

I think it’s a little ugly, with redundant bits of code. Maybe this could actually be done with around three lines rather than six? (Okay maybe not.)