详解OJ(Online Judge)中PHP代码的提交方法及要点
Introduction of How to submit PHP code to Online Judge Systems 
Introduction of How to commit submission in PHP to Online Judge Systems

在目前常用的在线oj中,codeforces、spoj、uva、zoj 等的题目可使用PHP实现基本算法,zoj是目前对PHP支持较好的中文OJ。

PHP是一门比较优秀的语言,但在算法实现上并没像C++那样提供方便的STL(Java、Python也提供了不少system类库可使用),不过PHP中的数组(array)十分强大灵活,用array结合class,实现链表,树,堆,栈等都是没有问题的,挺有挑战性的...
较C++而言,而PHP5.3版本之后提供的标准库SPL(Standard PHP Library)相当于PHP中的"STL",可以直接用 链表,树,堆,栈 等数据结构... ZOJ中的PHP版本是5.4.4的,用SPL应该是可以的...

OJ中针对与PHP的测试用例的输入方式是文件读取,输出是echoprint,注意加上"\n"表示换行... 整体而言,OJ中的PHP输入输出规范和C语言差不多,区别在用:PHP可以不声明变量类型,另外PHP中自定义的抽象数据结构ADT用class实现,而C中用struct.

STDIN是OJ中的PHP环境提供的常量,STDIN等价于fopen("php://stdin","r"));

PHP中的 fscanf($fd, "%d %d %d", $var1, $var2, $varN)==N 用来从文件中按行读取数据。

ZOJ 1001

提交网址:  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1

A + B Problem

Time Limit: 2 Seconds      Memory Limit: 65536 KB


Calculate a + b

Input

The input will consist of a series of pairs of integers a and b,separated by a space, one pair of integers per line.

Output

For each pair of input integers a and b you should output the sum of a and b in one line,and with one line of output for each line in input.

Sample Input

1 5

Sample Output

6

Hint

Use + operator

试着提交了几次代码,只有下面3种可以AC:

<?php
$fd = STDIN; // STDIN是oj提供的常量,等价于 fopen("php://stdin","r"));
// $fd=fopen('inputfileName','+r');
while(fscanf($fd, "%d %d", $a, $b)==2)
echo ($a + $b)."\n";
?>

将测试的输入放入inputfileName(有无文件扩展名皆可)中,使用注释中的$fd=fopen('inputfileName','+r'); 代替 $fd = STDIN;,即可进行本地测试

<?php
while(fscanf(STDIN, "%d %d", $a, $b) == 2)
echo ($a + $b)."\n"; // '\n' 或 PHP_EOL常量均不允许使用
?>

<?php
while(fscanf(STDIN, "%d %d", $a, $b) == 2)
print ($a + $b)."\n";

*推荐使用第一种写法,本地测试方便,上文已提过...

再附上ZOJ 1088相应的已AC代码:

<?php
function judge($m, $n)
{
$from = 0;
for($i = 2; $i < $n; $i++)
$from = ($from + $m)%($i);
if($from + 1 == 1) return 1;
else return 0;
}
$fd = STDIN; // STDIN是oj提供的常量,等价于 fopen("php://stdin","r"));
while(fscanf($fd, "%d", $n)==1 && $n!=0)
{
$m = 1;
while(!judge($m, $n))
{
++$m;
}
echo $m."\n";
}

本地测试 代码1(独立文件保存测试输入,fopen):

在php文件的同一目录下创建文件stdin,把测试数据放进去~

<?php
// To-Do: 约瑟夫环问题
$fd = fopen("stdin", "r");
// $fd = STDIN;
function judge($m, $n)
{
$from = 0;
for($i = 2; $i < $n; $i++)
$from = ($from + $m)%($i);
if($from + 1 == 1) return 1;
else return 0;
} while(fscanf($fd,"%d", $n)==1 && $n!=0)
{
$m = 1;
while(!judge($m, $n))
{
++$m;
}
echo $m."\n";
}

本地测试 代码2(测试数据放在程序代码中, fwrite,fopen,array,支持批量测试)

<?php
// To-Do: 约瑟夫环问题
$inFile="./stdin.txt";
$fd0 = fopen($inFile, "w+");
$data = array(
"3
4
5
6
7
8
9
10
11
12
0"
);
fwrite($fd0, $data[0]); // 将测试数据放进$data中
fclose($fd0);
$fd = fopen($inFile, "r");
// $fd = STDIN;
function judge($m, $n)
{
$from = 0;
for($i = 2; $i < $n; $i++)
$from = ($from + $m)%($i);
if($from + 1 == 1) return 1;
else return 0;
} while(fscanf($fd,"%d", $n)==1 && $n!=0)
{
$m = 1;
while(!judge($m, $n))
{
++$m;
}
echo $m."\n";
}

本地测试 代码3(测试数据放在程序代码中, file_put_contents,fopen,array,更简洁,不过file_put_contents( )只能向已经存在的文件中写入数据)

<?php
// To-Do: 约瑟夫环问题
$inFile="./stdin.txt";
$data = array(
"3
4
5
6
7
8
9
10
11
12
0"
);
file_put_contents($inFile, $data[0]); $fd = fopen($inFile, "r");
// $fd = STDIN;
function judge($m, $n)
{
$from = 0;
for($i = 2; $i < $n; $i++)
$from = ($from + $m)%($i);
if($from + 1 == 1) return 1;
else return 0;
} while(fscanf($fd,"%d", $n)==1 && $n!=0)
{
$m = 1;
while(!judge($m, $n))
{
++$m;
}
echo $m."\n";
}

几点发现:

1. OJ中PHP的标准输入输出与C的fscanf基本一致;

2. 换行,要求使用"\n",如果用'\n' 或 PHP_EOL常量替代会提示Non-zero Exit Code;

3. PHP文件最后的结束符 ?> 可以省略,当然也可写上...

PHP标准库(SPL)中提供的数据结构   PHP version >= 5.3

目录列表

SPL提供了一组标准数据结构. They are grouped here by their underlying implementation which usually defines their general field of application.

双向链表(DoublyLinkedList)

A Doubly Linked List (DLL) is a list of nodes linked in both directions to each others. Iterator's operations, access to both ends, addition or removal of nodes have a cost of O(1) when the underlying structure is a DLL. It hence provides a decent implementation
for stacks and queues.

堆(stack)

Heaps are tree-like structures that follow the heap-property: each node is greater than or equal to its children, when compared using the implemented compare method which is global to the heap.

数组(array)

Arrays are structures that store the data in a continuous way, accessible via indexes. Don't confuse them with PHP arrays: PHP arrays are in fact implemented as ordered hashtables.

映射(map)

A map is a datastructure holding key-value pairs. PHP arrays can be seen as maps from integers/strings to values. SPL provides a map from objects to data. This map can also be used as an object set.

其他

下面举一个splheap堆的实例:

<?php
class MySimpleHeap extends SplHeap
{
//compare()方法用来比较两个元素的大小,决定他们在堆中的位置
public function compare( $value1, $value2 ) {
return ( $value1 - $value2 );
}
} $obj = new MySimpleHeap();
$obj->insert( 4 );
$obj->insert( 8 );
$obj->insert( 1 );
$obj->insert( 0 ); echo $obj->top(); //8
echo $obj->count(); //4 foreach($obj as $number)
echo $number."\n";

此段代码执行结果如下:

相关链接:

http://acm.zju.edu.cn/onlinejudge/faq.do#sample

New SPL Features in PHP 5.3 http://matthewturland.com/2010/05/20/new-spl-features-in-php-5-3/

SPL 数据结构 http://php.net/manual/zh/spl.datastructures.php

详解OJ(Online Judge)中PHP代码的提交方法及要点【举例:ZOJ 1001 (A + B Problem)】的更多相关文章

  1. Scala 深入浅出实战经典 第61讲:Scala中隐式参数与隐式转换的联合使用实战详解及其在Spark中的应用源码解析

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载: 百度云盘:http://pan.baidu.com/s/1c0noOt ...

  2. Scala 深入浅出实战经典 第60讲:Scala中隐式参数实战详解以及在Spark中的应用源码解析

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...

  3. 详解Linux下iptables中的DNAT与SNAT设置(转)

    详解Linux下iptables中的DNAT与SNAT设置 这篇文章主要介绍了Linux下iptables中的DNAT与SNAT设置,是Linux网络配置中的基础知识,需要的朋友可以参考下   原文连 ...

  4. Java网络编程和NIO详解1:JAVA 中原生的 socket 通信机制

    Java网络编程和NIO详解1:JAVA 中原生的 socket 通信机制 JAVA 中原生的 socket 通信机制 摘要:本文属于原创,欢迎转载,转载请保留出处:https://github.co ...

  5. 详解如何在CentOS7中使用Nginx和PHP7-FPM安装Nextcloud

    转载地址:https://www.jb51.net/article/109382.htm 这篇文章主要介绍了详解如何在CentOS7中使用Nginx和PHP7-FPM安装Nextcloud,会通过 N ...

  6. 第7.18节 案例详解:Python类中装饰器@staticmethod定义的静态方法

    第7.18节 案例详解:Python类中装饰器@staticmethod定义的静态方法 上节介绍了Python中类的静态方法,本节将结合案例详细说明相关内容. 一.    案例说明 本节定义了类Sta ...

  7. 详解在Linux系统中安装Tomcat

    本文以在CentOS 7.6中安装Tomcat8.5为例进行安装,其他系统和版本都是大同小异的. 安装JDK 安装Tomcat之前,需要先安装JDK,可以参看之前的文章详解在Linux系统中安装JDK ...

  8. 详解如何在Laravel中增加自定义全局函数

    http://www.php.cn/php-weizijiaocheng-383928.html 如何在Laravel中增加自定义全局函数?在我们的应用里经常会有一些全局都可能会用的函数,我们应该怎么 ...

  9. 详解JavaScript数组过滤相同元素的5种方法

    详解JavaScript数组过滤相同元素的5种方法:https://www.jb51.net/article/114490.htm

随机推荐

  1. Eclipse配置注释模板详细介绍

    <引言> Eclipse 中提供了一个非常人性化的功能,可以自动生成注释为我们程序员做项目时提供便利,并且注释内容还具有定制化 可以根据自己的喜好配置不同的样式. <正文> 首 ...

  2. S系统的不好的实践

    多个项目 多个分支放在一个SVN里边维护,导致多股力量并行开发时候的代码覆盖的风险可能性很大,,  好的实践是维护独立的SVN,彼此分离开来

  3. Springboot & Mybatis 构建restful 服务二

    Springboot & Mybatis 构建restful 服务二 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务一 2 restful ...

  4. javafx安装

    可在官网http://efxclipse.bestsolution.at/ 下载 其中http://efxclipse.bestsolution.at/install.html#all-in-one ...

  5. font-size:0的妙用,用于解决inline或者inline-block造成的间隙

    1.图片间的缝隙(图片间的间隙一般是由换行.缩进造成的) <div> <img src="1.jpg"> <img src="2.jpg&q ...

  6. 解决:Adobe Acrobat Pro中设置背景颜色后,出现白色条纹

    找到  编辑->首选项->页面显示->渲染->使用2D图形加速 取消即可

  7. 进军微信小程序之准备工作

    小程序这么火,不去浪一浪怎么行?   更何况,现在微信“赦免”了个人认证,又更新了web开发工具,现在正是搞搞小程序的最佳时期! 那么一起来看看要做什么准备吧~   官方的文档很详细,可参考:小程序官 ...

  8. HDU5813 Elegant Construction

    Elegant Construction                                                                         Time Li ...

  9. sort()方法的应用(二)

    引用:函数作为参数 var fn_by = function(id) { return function(o, p) { var a, b; if (typeof o === "object ...

  10. Django+easyui 快速开发

    Django的使用我们可以查看上一篇博客,今天我们要在Django中使用easyui快速开发,在我们安装好Django, 我们可以道改地址那一下easyui 官方API文档(http://downlo ...