Pages

Saturday, November 9, 2013

SPOJ: Life, the Universe, and Everything -- TEST

This is first problem of SPOJ in classical problem set. It is like HELLO WORLD program for beginners who just started online programming in SPOJ. This problem is basically to get acquainted with how the Sphere Online Judge works.

Here is the problem description of  Life, the Universe, and Everything

"Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits."

Sample Input and Output

Input:
1
2
88
42
99

Output:
1
2
88

Solution of Life, the Universe, and Everything in C++

#include <iostream>
using namespace std;
int main()
{
  int i=0;
  while(1){
      cin>>i;
      if(i != 42)
          cout<<i<<endl;
      else
          break;
 }
  return 0;
}

Solution of Life, the Universe, and Everything in C

 #include <stdio.h>
int main()
{
    int i=0;
    while(1){
        scanf("%d",&i);
         if(i != 42)
            printf("%d\n", i);
         else break; 
    }
    return 0;
}

Solution of Life, the Universe, and Everything in JAVA

import java.io.*; 
public class Main
{
   public static void main(String[] args) throws IOException
   {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      int i;
      while(true){
         String s = br.readLine();
         i = Integer.parseInt(s);
         if(i != 42)
          System.out.println(i);
         else
          break;
      }

   }
}

Solution of Life, the Universe, and Everything in Brainf**k

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

Click here If you saw this language first time.

No comments:

Post a Comment