0
1
I just found one of my first C++ programs in some deep caves on my disk...
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int a , b , c , d , e , f , g , h , i , j , x , n , N , S;
enum cycle { A , B , C };
string Str_;
cycle Cy;
void next_Cy();
int main() {
S = 0;
Cy = A; // Set first cycle
cout << "Enter n:\n";
getline ( cin, Str_ );
stringstream (Str_) >> x; // take x
if ( x >= 10 ) {
n = (x / 10)-1; // defining n
for ( N = 0; N <= n; N++ ) {
a = N*10+0; // defining the digits
b = N*10+1;
c = N*10+2;
d = N*10+3;
e = N*10+4;
f = N*10+5;
g = N*10+6;
h = N*10+7;
i = N*10+8;
j = N*10+9;
switch ( Cy ) { // switch cycle cases
case A:
S += a + d + f + g + j;
break;
case B:
S += a + c + f + i;
break;
case C:
S += a + b + e + f + h;
break;
}
next_Cy(); // next cycle ..
}
cout << S << endl;
}
return 0;
}
// function to define next cycle
// A becomes B
// B becomes C
// C becomes A again ,,
void next_Cy() {
if ( Cy == A ) {
Cy = B;
}
else {
if ( Cy == B ) {
Cy = C;
}
else {
Cy = A;
}
}
}
(I can't believe that I wrote this shit some day in my life.)
CHALLENGE: Try to find out what this program does. (Yes, it does something "useful".)
Compiling and executing the code is of course allowed. (Yes, it does compile and run.)
% g++ puzzle.cpp -o puzzle
% ./puzzle
Enter n:
9000
18895500
%
So basically, you just enter a number (which should be greater than 9 because reasons) and then you will get some result, whatever that is.
By the way, I didn't modify the code or anything, these variables were really labeled like that.
PS: My favorite comment is // take x
.
The first to find out what this code does, wins.
The explanation having the most upvotes wins.
Use the spoil tag on your answer!
I believe that this kind of question is more suited for Stack Overflow – Optimizer – 2015-01-09T13:52:18.970
1Is this really a programming puzzle or code golf? – KSFT – 2015-01-09T13:54:31.833
@Optimizer, Humm, but SO is for "serious" programming problems, not for puzzles et. al. I already know what this code does. I just thought it might be fun to figure out. Please pardon my misplaced post. – YouniS Bensalah – 2015-01-09T13:59:16.180
@younishd your's is not really a puzzle as "the first to find wins" is not a criteria for puzzles, but for debugging. If you can change the winning criteria which gives more users a shot at this , then it will be called a puzzle/challenge. – Optimizer – 2015-01-09T14:03:48.310
1@KSFT: I do not think this question is appropriate for Stack Overflow, but we could ponder this as a new type of challenge. I am not certain whether this would be a good challenge though (with the caveat that I have not yet looked at the program in this question, which may be a first good example). – Wrzlprmft – 2015-01-09T14:05:17.320
@Optimizer, I'm okay with that, but how would you formulate the winning criteria? – YouniS Bensalah – 2015-01-09T14:09:29.743
@Optimizer, like this? – YouniS Bensalah – 2015-01-09T14:13:07.687
I have no idea :) – Optimizer – 2015-01-09T14:13:11.787
@KSFT, thanks for being open. – YouniS Bensalah – 2015-01-09T14:13:24.203
@Optimizer, here's another idea for this to become a "real puzzle": find out what the program does (without spoiling it) and write a similar program that is even more complex/obfuscated/bizarre. – YouniS Bensalah – 2015-01-09T14:17:23.860
1That should work if you can define what is more complex/... – Optimizer – 2015-01-09T14:19:32.313
1You should state that all answers should be in spoiler tags so as not to take away the challenge for those who want to figure it out on their own. – Jordon Biondo – 2015-01-09T14:58:06.433
I think this is a good idea for a new type of puzzle. We should have a new tag for this kind of puzzle. – KSFT – 2015-01-09T15:08:36.920
@JordonBiondo, done. – YouniS Bensalah – 2015-01-09T15:09:59.197
@KSFT
code-riddle
orcode-guess
? – YouniS Bensalah – 2015-01-09T15:19:07.007@KSFT (cc younishd) I think we should discuss this on meta (both if this is a good fit for PPCG, and we could also vote on a tag, if it is) – Martin Ender – 2015-01-09T15:29:57.213
There should probably be one question where people post everything like this, just like the unscramble question. – jimmy23013 – 2015-01-09T15:57:55.110
I think I have some obfuscated code somewhere that I could post if we decide it's a good fit. – KSFT – 2015-01-09T16:06:28.497
@MartinBüttner So...has it been decided not to allow it? Has anyone made a post on meta about it? – KSFT – 2015-01-09T19:47:01.347
@KSFT I don't think anything has been decided, because I haven't seen a meta post on it. – Martin Ender – 2015-01-09T19:56:27.273