473,382 Members | 1,424 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

need a hint

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);}

Aug 5 '06 #1
5 1308
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);}


Aug 5 '06 #2
the shortest that compiled was only 45 bytes, no idea how it is possible

Aug 5 '06 #3
On 5 Aug 2006 10:58:44 -0700 in comp.lang.c++, "Podrzut_z_Laweczki"
<em*****@o2.plwrote,
>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!

Aug 6 '06 #4
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.

Aug 6 '06 #5
In article <11********************@i42g2000cwa.googlegroups.c om>,
em*****@o2.pl 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.
Aug 6 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: F. GEIGER | last post by:
Hi all, I use Leo for programming. Among other goodies Leo provides it let's me easily share code between different Python apps. When I have an app consisting of more than one file, they usually...
2
by: Ryan Wagner | last post by:
I am doing many SQL insert statements to insert records into an oracle database. I need the order of the records, after inserting all records, to be the same as the order I inserted them in. Right...
2
by: Anita | last post by:
Hi All, I have a question about lock hint for you : If the first user currently run a select command with share lock and hold it. What kind of lock (lock hint) should be used by the second...
9
by: john smile | last post by:
Hi All, I want to lock 2 tables on 2 servers using TABLOCKX hint. These tables function as semaphores in my application. It means when the tables are locked then other users will not be able to...
1
by: Mark P | last post by:
Is an insert with hint (in say std::set) only beneficial if the insertion occurs immediately before the pointed to location of the hint? Suppose the insertion occurs right after the hint location? ...
3
by: rudymoore | last post by:
I'm often find-ing the same string in a map<string,xxx> I want to pass a hint to map::find but was disappointed to find that only map::insert takes the hint. Why would map::insert take a hint...
6
by: Mark P | last post by:
Some time ago I posted here about inserting into a set with a hint: ...
7
by: piperzen | last post by:
Hi, I'm developing a set of language learning exercises for http://www.yale.edu/swahili , and we've run into a javascript question that the programmer doesn't know how to tackle (we mostly work...
18
by: pipito | last post by:
Hi...i am a beginner in C programming... my question is like this... im using the scanf in C to accept input data in output area, printf("name: ");scanf("%s",name); printf(Address:...
1
by: javabeginner123 | last post by:
i have a java prob, and i have to solve it fast, but i'm just getting to know it, so plz help me solve it with full code completed, thanks so much. the prob is to create a monter fight and there is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.