Life, the Universe, and Everything

问题描述

Sphere Online Judge (SPOJ) 网站的第一道题目是:“Life, the Universe, and Everything”,如下所示:

SPOJ Problem Set (classical)
1. Life, the Universe, and Everything
Problem code: TEST

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.

Example

Input:
1
2
88
42
99
Output:
1
2
88

Added by:Michał Małafiejski
Date:2004-05-01
Time limit:10s
Source limit:50000B
Languages:All
Resource:Douglas Adams, The Hitchhiker's Guide to the Galaxy

你要求写一个程序来暴力破解生命、宇宙及任何事情的终极答案。准确地说,就是把一些很小的数从输入复制到输出,遇到 42 就停止。输入的所有数都是一位或者两位的整数。这道题目的背景故事来源于英国作家道格拉斯·亚当斯所著的长篇科幻小说《银河系漫游指南》,书中说:亚瑟·丹特在马格西亚上被告知,地球其实只是一个实验。原来许多百万年前,老鼠其实是一种超智慧生物,它们建造了一部超级电脑深思,它们问超级电脑,生命、宇宙以及任何事情的终极答案是什么,经过一段长时间的计算,深思告诉老鼠的后人答案是 42,深思解释它只能计算出答案是什么,但答案的原因必须由另一部更高智能的电脑才能解释,而该部电脑就是地球。2005年6月四川科学技术出版社出版了《银河系漫游指南》,2011年8月上海译文出版社重新出版了这本科幻小说,书名改为《银河系搭车客指南》:

银河系漫游指南银河系搭车客指南

问题解答

这道题目是 SPOJ 网站的第一道题目,作为入门的测试,以便让用户熟悉在该网站解题的步骤,所以完全没有难度,其 C# 语言解答如下所示:

using System;

// http://www.spoj.pl/problems/TEST/
static class Test
{
  static void Main()
  {
    for (string s; (s = Console.ReadLine()) != "42"; Console.WriteLine(s)) ;
  }
}

翻译为对应的 F# 语言:

open System

let mutable s = Console.ReadLine()
while s <> "42" do
  printfn "%s" s
  s <- Console.ReadLine()

F# 不是纯函数语言,而是多范式的编程语言。上述 F# 程序使用命令范式,而改为函数式编程则如下所示,使用尾递归:

let rec test() =
  let s = System.Console.ReadLine()
  if s <> "42" then
    printfn "%s" s
    test()

test()

还可以把函数作为参数传递,也就是所谓的高阶函数:

let rec test readLine =
  let s = readLine()
  if s <> "42" then
    printfn "%s" s
    test readLine

test System.Console.ReadLine

注意上述 F# 程序上最后一行传递的是参数是 Console.ReadLine 函数本身,而不是该函数的返回值。这样才能够不断读取输入行。第一行定义 test 函数时也不用指明形参 readLine 的类型,F# 语言编译器能够根据上下文推断出正确的类型。在 C# 语言中函数是不能直接作为参数传递的,必须使用委托或者 Lambda 表达式。

而 Ruby 语言的解答就非常简单了,只有一句话:

while (num = gets).to_i != 42 do puts num end

Perl 语言的解答也非常简单:

while (($_ = <>) != 42) { print $_; }

下面是 Python 语言的解答:

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

Java 语言的解答如下所示:

import java.io.*;

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

当然少不了 C 语言解答:

#include <stdio.h> 

int main(void)
{ 
  for(int x; scanf("%d",&x) > 0 && x != 42; printf("%d\n", x)) ; 
  return 0; 
}

还有 C++ 语言解答:

#include <iostream>

using namespace std;
 
int main()
{
  for (int x; cin >> x, x != 42; cout << x << endl) ;
  return 0;
}

  

所有以上程序都在 SPOJ 网站提交通过。该网站运行在 Linux 操作系统下,支持四十多种编程语言。

在线集成开发环境

ideone.com 是一个“Onle IDE & Debugging Tool”。她的范例页面提供了很多编程语言的示例程序,这些示例程序都是用于解答以上问题的。这个网站和 SPOJ 网站的后台都是 Sphere Research Labs

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值