Just another tech blog.

A blog abt GNU/Linux, programming, hacking, and my life.

Archive for July, 2006

Some good looking codes and interesting problems

Posted by ajay on July 21, 2006

I recently came across some awesome piece of codes. I dont understand any of them, but I find it worth sharing here.

1. A c program to solve The Tower of Hanoi problem without using functions,  stack etc. and does the task in 6 lines. Moreover, the code looks like a tower as well :).

main(
){int
z,y,n
;scanf(“%d”,&n);
for(y=1;(1<<n)-y
;y<<=z-1,printf(
“disk %i from %i to %i.\n”/**/
,z,(y&y-1)%3,((y|y-1)+1)%3),y
++)for(z=1;!(y&1);z++,y>>=1);}

2. The queen of problems – N queens problem. If you are a computer science student, you must have heard about n queens problem and probably solved that as well.  This C program solves the problem very quickly, and for the rest – just have a look at the code.

int v,i,j,k,l,s,a[99];main(){for(scanf(“%d”,&s);*a-s;v=a[j*=v]-a[i],k=i<
s,j+=(v=j<s&&(!k&&!!printf(2+”\n\n%c”-(!l<<!j),” #Q”[l^v?(l^j)&1:2])&&++
l||a[i]<s&&v&&v-i+j&&v+i-j))&&!(l%=s),v||(i==j?a[i+=k]=0:++a[i])>=s*k&&
++a[–i]);printf(“\n\n”);}

3. Prints first 15000 digits of PI.

a[52514],b,c=52514,d,e,f=1e4,g,h;main(){for(;b=c-=14;h=printf(“%04d”,
e+d/f))for(e=d%=f;g=–b*2;d/=g)d=d*b+f*(h?a[b]:f/5),a[b]=d%–g;}

4.  An interesting problem –

what is the “condition” such that, this snippet of code prints HelloWorld!!

if condition

printf(“Hello);

else

print(“World!!);

my friend abhilash gave a nice solution for that ..

if ( printf(“Hello”)<0){

printf(“Hello”);

else

printf(“World!!);

5. Another interesting problem –

Write a small C program, which while compiling takes another program
from input terminal, and on running gives the result for the second
program. (NOTE: The key is, think UNIX).
Suppose, the program is answer.c
Then, while compiling
$ cc -o answer answer.c
int main()
{
printf(“Hello World\n”);
}
^D
$ ./answer

Hello World
$
if anybody can solve this problem please let me know by commenting over the post with the solution.

Happy coding.

Advertisements

Posted in Programming | 23 Comments »

Some Special Codes

Posted by ajay on July 16, 2006

I was solving problems some time ago and came to know some good facts about c and c++  and wrote some interesting codes.

1. A C program which can print the file name it is kept in ;).

#include<stdio.h>

main(){

printf(“the source file name is %s\n”,__FILE__);

}

actually __FILE__ is a macro which stands for the file name the programme is kept in and the compiler does the rest.

2. Usage of assignment suppression operator in scanf  – Suppose you have some crap input (the input is provided to you but that is not of any use to your program) then what normally we do is take a dummy variable and scan the input in that variable.  Example –

int dummy;

scanf(“%d”,&dummy);

but there is one more method which can save memory and time

what you do is

scanf(“%*d”);

In this method, we provide a character * to the scanf function and by doing that; scanf will scan the input from standard input but it wont assign it to any variable. This is scanned in a buffer of sufficient size and you dont have to worry about that.

3.  My source code for problem KAMIL on spoj

x=1;main(){for(;scanf(“%*[^DFLT\n]”)+1;x=getchar()<11?printf(“%d\n”,x),1:x*2);}

If anybody has a better idea on this problem then please do comment on the post.

4. Printing something with variable width  –

Suppose you are to print some formatted output in some program and for that sometime you require to print a variable in a fix width ..

for example if you have to pring an integar in width of 5 or more then you do

printf(“%5d”, var);

now if var is 10 it will append 2 leading spaces to complete the atleast 5 width rule.

now suppose you dont want leading spaces but leading zeroes, then here is another method to write the same –

printf(“%05d”,var);

now if var is 10 it will print 00010 and so on.

Another condition comes if the width in which we want to print the variable is not known at the compile time – suppose you want to print variable with a width which is contained in another variable, then we can use –

printf(“%*d”,x,var);

here x is the width length variable. Whenever we do this * operator .. then it looks for the next integar argument provided in the arguments and then  assumes that it is the width in which  the variable is to be printed.

Now if x = 2 then it will b the same as

printf(“%2d”,var); ..

this way we can work with this width thing pretty well.

If any of you guys know some nice facts about c or cpp language, then please post them as comments. Any gud suggestions related to things I posted are also welcome. Have Fun.

Posted in Programming | 45 Comments »