>> As such, we were psychics who cheated with our special abilities.
Cause that's clearly the only option! Not that his plagiarism software was faulty!
Though I assume that 'accusation' was in jest?
Printable View
>> As such, we were psychics who cheated with our special abilities.
Cause that's clearly the only option! Not that his plagiarism software was faulty!
Though I assume that 'accusation' was in jest?
>> But yeah, you and Daved clearly are colluding to win the prize
Darn, I figured changing the indentation style would fool you.
This idea would be interesting to expand. Perhaps a slightly harder computation and a "who can do it with the fewest semicolons?" style contest. I think what we have here is as far as I can go without delving into something that isn't off the top of my head, so it might be fun to try and figure out even more complex solutions.
Actually, no. He actually did not check the results closely and so just had a "guilty until proven innocent" attitude. After all, there were students who had cheated by copying others. Of course, when you have an abnormal amount of students mass cheating, if it is not some psychic broadcast of a model program, then it must be a plagiarism detection software problem :DQuote:
Though I assume that 'accusation' was in jest?
I agree, though it may be good to let things like efficiency be considered as well.Quote:
This idea would be interesting to expand. Perhaps a slightly harder computation and a "who can do it with the fewest semicolons?" style contest. I think what we have here is as far as I can go without delving into something that isn't off the top of my head, so it might be fun to try and figure out even more complex solutions.
I've found that most compilers produce excellent code for these things, so long as it can inline as much stuff as possible. Also, you have to understand what sort of constructs the compiler can completely optimize away. E.g. passing a functor down into several levels of algorithms. As long as you don't blow up the inliner, the functor will often be completely optimized out of existence, as long as its constructor has no side effects.
I had more in mind say, an extra semi-colon due to a sort.Quote:
I've found that most compilers produce excellent code for these things, so long as it can inline as much stuff as possible. Also, you have to understand what sort of constructs the compiler can completely optimize away. E.g. passing a functor down into several levels of algorithms. As long as you don't blow up the inliner, the functor will often be completely optimized out of existence, as long as its constructor has no side effects.
Just PM'd a dodgy solution that only works with single digits and is fudged to submission ... :)
So how about: In one hour from the time of this message, the floor is open to people posting their solutions publicly. I've seen some good stuff so far.
When I get home I'll post my solution, which is also the solution given by a few other people. You know who you are -- you used a standard algorithm to compute the answer. Other people (MacGyver, twomer) feel free to post what you've got.
Code:#include <iostream>
#include <iterator>
#include <numeric>
int main()
{
std::cout << std::accumulate(std::istream_iterator<int>(std::cin),
std::istream_iterator<int>(),
0);
return 0;
}
That takes out the fun! And why do we have to use a semicolon?
Code:#include <iostream>
void foo(int x, int y) {
if (std::cin >> x) {
if (foo(0, x + y), false) { }
}
else {
if (std::cout << y << std::endl) { }
}
}
int main () {
if (foo(0, 0), false) { }
}
And here's C, while we're at it.
Code:#include <stdio.h>
void foo(int x, int y) {
if (EOF != scanf("%d",&x)) {
if (foo(0, x + y), 0) { }
}
else {
if (printf("%d\n", y)) { }
}
}
int main(void) {
if (foo(0, 0),0) { }
}
No semicolons, but assumption about size of int * and char **, which is probably not a good idea to make:
Similar, but proper, if but contrived, C code with one semicolon:Code:#include <stdio.h>
int main(int argc, char *argv[])
{
while(--argc != 0) { }
while((scanf("%d", (int *)argv) == 1) && (((argc += *(int *)argv) % 1) == 0)) { }
while((printf("Sum = %d\n", argc) %1) != 0) { }
return 0;
}
Some natural obfuscation given the rules I guess. :)Code:#include <stdio.h>
int main(void)
{
int num=0, total=0;
while((scanf("%d", &num) == 1) && (((total += num) % 1) == 0)) { }
while((printf("Sum = %d\n", total) % 1) != 0) { }
return 0;
}
My solution:Not great but more entertaining than accumulate imo :)Code:#include <iostream>
#include <algorithm>
#include <iterator>
#define ch_ty(ty) std::istream_iterator<ty>::char_type
#define tr_ty(ty) std::istream_iterator<ty>::traits_type
#define cin_iter(ty) std::istream_iterator<ty, ch_ty(ty), tr_ty(ty)>( std::cin )
#define void_iter(ty) std::istream_iterator<ty, ch_ty(ty), tr_ty(ty)>()
int main( int argc, char *argv[] ) {
while ( (cin_iter(size_t)) != void_iter(size_t)
? ( std::cin.unget(),
argc += *cin_iter(size_t)
) : (
printf( "\nSUM: %d\n", --argc ), system("exit")
) );
}
I also did a main-recursion one for kicks.
Two things:
- unget() appears to only unget the last thing that was entered so if I entered 12 my istream iterator would only read 2. Is there a more robust way of ungetting? I thought that by changing the char_type and the traits type that it would accomplish that.
- Is there a better way of exiting the loop than system exiting? I couldn't thing of much else that would work.
My crummy solution:
That can't be against the rules, otherwise you'd have to count the semicolons in the headers (stdio.h for example) :)Code:#include <stdio.h>
#define SEMICOL ; /* the one semicolon */
int main(void)
{
int sum = 0, n = 0 SEMICOL
while(scanf("%d", &n) == 1)
sum += n SEMICOL
printf("Sum was %d\n", sum) SEMICOL
return 0 SEMICOL
}
I dunno, it seems to simple - like it needs to be disqualified -- maybe it's cause I thought of it :)
And for extra points, 1 set of brackets:
Code:#include <stdio.h>
#define SEMICOL ; /* the one semicolon */
#define B1 (
#define B2 )
int main B1 void B2
{
int sum = 0, n = 0 SEMICOL
while B1 scanf B1 "%d", &n B2 == 1 B2
sum += n SEMICOL
printf B1 "Sum was %d\n", sum B2 SEMICOL
return 0 SEMICOL
}
That #define SEMICOLON thing was exactly what I was thinking as I was reading the thread, and then in the very last post you beat me to it . . .
Here's another solution:
Recursion's no fun, because you need at least one return statement. Or a variable declared that you could pass as a pointer, I suppose. You could probably do a recursive solution with argc like MacGyver with no semicolons.Code:#include <stdio.h>
#include <stdlib.h>
int main() {
int num, sum = 0;
while(scanf("%i", &num) == 1) {
if(sum += num) {}
}
if(printf("%i\n", sum)) {}
}
Code:#include <stdio.h>
#include <stdlib.h>
int sum(int x, int y) {
if(scanf("%i", &y) == 1 && x += sum(y, 0)) {}
return x;
}
int main() {
if(printf("%i\n", sum(0, 0))) {}
}