need a hint

Discussion in 'C++' started by Podrzut_z_Laweczki, Aug 5, 2006.

  1. 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);}
     
    Podrzut_z_Laweczki, Aug 5, 2006
    #1
    1. Advertising

  2. Podrzut_z_Laweczki

    Mark P Guest

    Podrzut_z_Laweczki wrote:
    > 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);}
     
    Mark P, Aug 5, 2006
    #2
    1. Advertising

  3. the shortest that compiled was only 45 bytes, no idea how it is possible
     
    Podrzut_z_Laweczki, Aug 5, 2006
    #3
  4. Podrzut_z_Laweczki

    David Harmon Guest

    On 5 Aug 2006 10:58:44 -0700 in comp.lang.c++, "Podrzut_z_Laweczki"
    <> wrote,
    >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!
     
    David Harmon, Aug 6, 2006
    #4
  5. 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.
     
    Podrzut_z_Laweczki, Aug 6, 2006
    #5
  6. Podrzut_z_Laweczki

    Jerry Coffin Guest

    In article <>,
    says...
    > 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.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Aug 6, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marek

    How to create a hint?

    Marek, Dec 27, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    1,547
    =?Utf-8?B?a2VybWl0?=
    Dec 27, 2004
  2. Daniel Pope
    Replies:
    5
    Views:
    475
    Silvio Bierman
    Feb 22, 2004
  3. F. GEIGER
    Replies:
    0
    Views:
    259
    F. GEIGER
    Jun 8, 2004
  4. GHUM

    need hint for refactoring

    GHUM, Aug 9, 2006, in forum: Python
    Replies:
    1
    Views:
    301
    Diez B. Roggisch
    Aug 9, 2006
  5. Gerr
    Replies:
    4
    Views:
    131
Loading...

Share This Page