Showing posts with label algorithms. Show all posts
Showing posts with label algorithms. Show all posts

Tuesday, March 8, 2011

Algothematics.. back again !!


Algothematics is back.. smarter and trickier !! Algothematics was first introduced in Zealicon '10 (annual TechFest of JSS Academy of Technical Education, Noida) and is slated for 9th March in Zealicon'11. Just as the last year, the event will be conducted online. All programmers across the world are, hence, invited.

Rules are the same. The event comprises of 15 levels. Each level has an ad-hoc mathematical problem which can either be solved manually (using pen and paper) or by conceiving certain algorithm. The submission requires only the answer to the question. Submission of source-code is not required.

Other tools can also be used for solving the problems. Spreadsheet, PARI-GP, MATLAB or anyother library (like BigInteger) are among them. Besides all these we always have GOOGLE with us... :)

Besides all this, Zealicon forum will contain an "Algothematics" thread under "online events" where all sorts of problems and their hints will be discussed. No questions extraneous to the event will be entertained.
LeaderBoard will be available at the home page itself, so that you can constantly check your position among others.

Please do not ruin the basic purpose of event by any hacking stuff. Those not conforming to the rules and regulations will be banned without prior information. Decision of Algothematics Team will be the last.

First two teams reaching the final level will be adjudged victorious..

So pull up your socks, hone your programming skills.. Algothematics is about to begin..!!! (9th March, 20:00)

* * * * *

Thursday, February 4, 2010

My way of writing a Quine

Here I m presenting a Quine written by me few days before. A Quine is a program that prints its own source code. In my source code user has an option of printing the source code n number of times.

Here is the code written in c:

#include < stdio.h >
int main()
{
int n;
char c;
FILE *f;
f=fopen(__FILE__,"r");
scanf("%d",&n);
while(n--)
{
while((c=getc(f))!=EOF)
putchar(c);
fseek(f,0,0);
}
return 0;
}



Here is the code written in c++:

#include <fstream>
#include<iostream>
#include <cstdio>
using namespace std;
int main()
{
int n;
string a;
cin>>n;
while(n--)
{
ifstream fin(__FILE__);
while(!fin.eof())
{
getline(fin,a,'\n');
cout<<a><<"\n";
}
}
return 0;
}

Thursday, January 21, 2010

My submissions to SPOJ problem- TEST

My interest lies in programming languages. Actually, I have a bad habit of trying every type of weird thing and this time I came up with the idea of trying a simple problem in every possible programming language I know. I tried to solve the first problem of SPOJ named Test. Here are my codes:

C:

#include < stdio.h >

int main()
{
int n;
scanf("%d",&n);
while (n!= 42) {
printf(""%d", n);
scanf("%d",&n);
}
return 0;
}

C++:

#include < iostream >
using namespace std;

int main()
{
int n;
cin>>n;
while (n!= 42) {
cout<>n;
}
return 0;
}

C#:

using System;
public class Test
{
public static void Main()
{
int n;
while ((n = int.Parse(Console.ReadLine()))!=42)
Console.WriteLine(n);
}
}

Java:

public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
String s;
while (!(s=r.readLine()).startsWith("42"))
System.out.println(s);
}
}

Python:

n = input()
  while n != 42:
  print n
  n = input()

PHP:

< ? php while(1) { fscanf(STDIN,"%d",$n); if ($n == 42) break; print "$n\n"; } ? >


BrainF*ck:

>+[>>,----------[>,----------]+++++[<-------->-]<[>>-<]>+[
-<+++++++[<------>-]<[>>-<]>+[
-<<[>>-<]>+[<<->>->]>
]<+++++++[<++++++>-]>>
]<++++++++[<+++++>-]< [<]<[>>[++++++++++.>]++++++++++.[[-]<]]<]
* * * * *

Tuesday, December 15, 2009

Some more problems of SPOJ...

I recently breached into the top 1200 ranks in the world, but sad news is, soon I'll be thrown out too. The points keep on decreasing on a regular basis. Never mind this works as an incentive to work on more and more problems. :)
So, m writing this post for some more problems of SPOJ. The problems I m going to discuss are of an average level.

BISHOPS: Its an easy problem, if we try verbally. The answer is (2*n-2) i.e. equal to the number of diagonals. But the difficulty lies in the range of the inputs. N<=10^100..!! OMG!!.. :).. Try strings..

MINCOUNT: Actually what I did.. I tried it out for certain test cases and found that the answer is.. (n*(n+1)/2)/3.. i.e. the total number of coins in the triangle divided by 3. But why its so.. its still unanswered for me.. kindly post the explanation if you get one..!! :)

HUBULLU: Same mincount type problem here too.. seeing the discussion in the forum I got some hints that who so ever plays first wins.. So, it became a very very easy question for me to crack but why it happens is yet not understood by me..

TRICOUNT: Its another mathematics problem.. try it for sometime with all yours geometrical anlysis and at one time you will definitely come out with a solution that is much like (t*(t+2)*(2*t+1))/8.. where t is the level of triangle. Try it now yourself..!!

SAMER08F: Another age old problem of how many squares are there in a given bunch of squares?? Answer lies in the sum all squared numbers till the number of squares in each side of the grid i.e. if N=8 then answer= (1*1) + (2*2) + ... + (8*8).

Seeing my last three posts of SPOJ I think now its easy for any newbie to get started. So, no more discussing algorithms of SPOJ problems from my side. Keep practicing and who knows one day you beat [Trichromatic] XilinX..!! :p

* * * * *

Saturday, November 21, 2009

SPOJ: Some very easy problems

Its been a month since I have started coding in SPOJ. There are over 5000 problems present there. Its really gets very tough when one tries to get some easy problems from that lot. So, I dedicate this post to beginners. Go for the below mentioned problems.

Some very easy classical problems:

TEST- This is the first problem I solved in SPOJ. I recommend the same for you. It gives a brief introduction of the whole environment. What you need to do is to accept and print the input by the user until you come across 42. The easiest problem at SPOJ and a confidence booster to begin with..!! ;)

ADDREV- Take two numbers as input. Reverse both. Add the reversed number. Reverse again. Simple..!! The thing you need to keep in mind is that you have to provide the number of test cases first. While taking the inputs you can print the result too. You need not store them and print later.

FASHION- Quite big explanation of the problem (a bit interesting too ;) ). The only thing you need to keep in mind is that you have to sort the degree of hotness of men and women and then multiply and add them up. If you do not sort them, it will give a WA.

MIRRORED- Print "Mirrored pair" wherever you confront "bd" or "db" or "pq" or "qp", otherwise print "Ordinary pair". Don't forget to print "Ready" in the beginning. You do not have to take the test cases as input here. Just end the input when you confront two spaces. I got an AC at very first attempt..!! Hope the same for you.. :)

NSTEPS- Analize the graph properly. Whenever x and y co-ordinates are even the corresponding output is (x+y) for input (x=y) and (x=y-2). When they are odd the corresponding ouput is (x+y-1) for input (x=y) and (y=x-2). You can also do the problem with a different approach too. You can form a series for the numbers, and hence, generate the output.

OFORTUNE- Common.. don't get astonished by the enormous length of the question. Try tackling the problem. It just needs six inputs and a simple interest and compound interest formula implementation thats it.. Try it.. You will gain a hell lot of points..!!

Some very easy challenge problems:

SIZECON- The difficulty doesn't lie in the problem statement. The difficulty lies in the choice of programming language. I managed to do it in 96 characters. Best solution in c++ has 70 characters in it. Perl language wins with just 6 characters..!!

BFWRITE- I have already written a post about this problem. Try learning Brainf_ck language and solve this problem too. Here's the link to my post.

More easy problems are being searched. Kindly mention some in comments if you confront one.. Here's my SPOJ handle.. vaibhav_pandey.

* * * * *

Friday, October 30, 2009

How to handle very large test cases in c++

Most of us come across some c++ problems where we are asked to get some simple calculations done. For example: we can get a problem of subtracting 1 from a 100 digit number. Now, subtracting one is not a big problem for us but the problem is '100 digit number'.

None of the datatype has capacity to store such a large number. Stop thinking about unsigned long long.. even it cant..!! So, here's a short solution to this cute problemo..

We can take the input as a string and process each of its elements as in elementary mathematics i.e. if last digit is'nt 0 then reduce it by 1(dont forget it to convert to int and then back to string).. if last digit is zero then make it nine and move back to the adjacent digit following the same procedure. Simple :)

Here's the source code..

#include
using namespace std;
int main()
{
int i,t=40;
string z;
cin>>z;
i=z.size()-1;
do
{
if(z[i]!='0')
{
z[i]=int(z[i])-1;
break;
}
z[i--]=57;

}while(i>=0);
Then print the string a.

The only problem comes when we give input as 10, 100, 1000, 10000 ... the answer comes out as 09, 099, 0999, 09999... instead of normal numbers such as.. 9, 99, 999, 9999... I have resolved this problem for myself.. I'll not upload the source.. try it out yourself..!! ;)

* * * * *