银河

SKYIV STUDIO

  博客园 :: 首页 :: 博问 :: 闪存 :: :: :: 订阅 订阅 :: 管理 ::
  268 随笔 :: 2 文章 :: 2616 评论 :: 140万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

问题描述

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# 语言解答如下所示:

1
2
3
4
5
6
7
8
9
10
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# 语言:

1
2
3
4
5
6
open System
 
let mutable s = Console.ReadLine()
while s <> "42" do
  printfn "%s" s
  s <- Console.ReadLine()

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

1
2
3
4
5
6
7
let rec test() =
  let s = System.Console.ReadLine()
  if s <> "42" then
    printfn "%s" s
    test()
 
test()

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

1
2
3
4
5
6
7
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 语言的解答就非常简单了,只有一句话:

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

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

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

下面是 Python 语言的解答:

1
2
3
4
n = int(input())
while n != 42:
  print(n)
  n = int(input())

Java 语言的解答如下所示:

1
2
3
4
5
6
7
8
9
10
11
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 语言解答:

1
2
3
4
5
6
7
#include <stdio.h>
 
int main(void)
{
  for(int x; scanf("%d",&x) > 0 && x != 42; printf("%d\n", x)) ;
  return 0;
}

还有 C++ 语言解答:

1
2
3
4
5
6
7
8
9
#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

posted on   银河  阅读(1749)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示