need a hint

  • Thread starter Podrzut_z_Laweczki
  • Start date
P

Podrzut_z_Laweczki

on how to make it 2 x shorter (it takes last digit from n!) just a hint
:)

#include <cstdio>
int main(){int
c;scanf("%d",&c);printf("%d\n",c?c==3?6:c==1||c==2||c==4?c:0:1);}
 
M

Mark P

Podrzut_z_Laweczki said:
on how to make it 2 x shorter (it takes last digit from n!) just a hint
:)

If by 2x shorter you mean reduce the size by a factor of 2, then I can't
help you. I can make it 2 characters shorter though (and, IMHO, only
very slightly less atrocious to read)...
#include <cstdio>
int main(){int
c;scanf("%d",&c);printf("%d\n",c?c==3?6:c==1||c==2||c==4?c:0:1);}

last line becomes:

c;scanf("%d",&c);printf("%d\n",(c<5)+3*(c==4)+5*(c==3)+(c==2);}
 
P

Podrzut_z_Laweczki

the shortest that compiled was only 45 bytes, no idea how it is possible
 
D

David Harmon

On 5 Aug 2006 10:58:44 -0700 in comp.lang.c++, "Podrzut_z_Laweczki"
on how to make it 2 x shorter (it takes last digit from n!) just a hint
:)

#include <cstdio>
int main(){int
c;scanf("%d",&c);printf("%d\n",c?c==3?6:c==1||c==2||c==4?c:0:1);}

It's not clear to me from your question exactly how you want to
improve your code. The code suffers severely from having too much
stuff crammed together without spacing (unless that is just
something your newsreader did to you.) The first major step toward
improving it is to space things out with standard indentation.

After that, replace the convoluted conditional expression with "if"
statements that can also be indented to clarify the structure.
Having done that, you will notice a nested "if" inside the first
"if". Reverse the condition to transform that into a chained "else
if" structure.

Bad:
if ()
if()
if()

Good:
if()
else if()
else if()

From there, the code should be comprehensible enough to make clear
the way to the modifications you wanted.

As follows:

#include <cstdio>
int main()
{
int c;
scanf("%d", &c);

int result;
if (c == 0)
result = 1;
else if (c == 3)
result = 6;
else if (c == 1 || c == 2 || c == 4)
result = c;
else
result = 0;

printf("%d\n", result);
}

If I have made any mistake in the above transformation, blame it on
the horribly convoluted styling of the original -- but recheck it
yourself before trusting it.

Finally, it appears at this point that a simple table lookup may be
a more straightforward approach to your problem. Remember, the
*first goal* is to write code that is plainly and clearly correct!
 
P

Podrzut_z_Laweczki

Having this unscrambled could you give a clue, how to make it short? It
won't be used anywhere besides some problem solving contest where
points are granted for the shortest code. The shortest code that does
the same (reads, checks a few conditions and prints the result) is 45
bytes long, this one is 99.
 
J

Jerry Coffin

Having this unscrambled could you give a clue, how to make it short? It
won't be used anywhere besides some problem solving contest where
points are granted for the shortest code. The shortest code that does
the same (reads, checks a few conditions and prints the result) is 45
bytes long, this one is 99.

If you don't mind it being written in ugly, non-portable C, it's not
terribly difficult to at least get close to that:

main(c){c=getch()-'/';putch("011264"[c*(c<6)]);}

It takes far most space to list problems with this though. It
uses/depends on:

1) getch/putch (shorter names, but utterly non-portable).
2) implicit return type from main
3) an old-style function header
4) implicit variable declaration
5) ASCII or ASCII-like character set
6) implicit function declarations
7) indexing a string literal
8) math on a boolean value

There may be even more ugliness that doesn't occur to me at the moment.
All in all, code to avoid under any reasonable circumstances!

I can't imagine a 45-byte version could be much better either. In
particular, consider that something like this:

#include <stdio.h>
int main(){int c;scanf("%d",&c");printf("%d",c);}

i.e. just the bare bones of reading and writing a number, with no logic
at all, is already 69 bytes long.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top