详解OJ(Online Judge)中PHP代码的提交方法及要点
来源:互联网 发布:java项目管理 编辑:程序博客网 时间:2024/06/14 13:27
OJ中针对与PHP的测试用例的输入方式是文件读取,输出是echo或print,注意加上"\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
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:
将测试的输入放入inputfileName(有无文件扩展名皆可)中,使用注释中的$fd=fopen('inputfileName','+r'); 代替 $fd = STDIN;,即可进行本地测试。
或
*推荐使用第一种写法,本地测试略方便,上文已提过...
再附上ZOJ 1088相应的已AC代码:
本地测试 代码1(独立文件保存测试输入,fopen):
在php文件的同一目录下创建文件stdin,把测试数据放进去~
本地测试 代码2(测试数据放在程序代码中, fwrite,fopen,array,支持批量测试)
本地测试 代码3(测试数据放在程序代码中, file_put_contents,fopen,array,更简洁,不过file_put_contents( )只能向已经存在的文件中写入数据)
几点发现:
1. OJ中PHP的标准输入输出与C的fscanf基本一致;
2. 换行,要求使用"\n",如果用'\n' 或 PHP_EOL常量替代会提示Non-zero Exit Code;
3. PHP文件最后的结束符 ?> 可以省略,当然也可写上...
PHP标准库(SPL)中提供的数据结构 PHP version >= 5.3
目录列表
- SplDoublyLinkedList
- SplStack
- SplQueue
- SplHeap
- SplMaxHeap
- SplMinHeap
- SplPriorityQueue
- SplFixedArray
- SplObjectStorage
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.
- SplDoublyLinkedList
- SplStack
- SplQueue
堆(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.
- SplHeap
- SplMaxHeap
- SplMinHeap
- SplPriorityQueue
数组(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.
- SplFixedArray
映射(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.
- SplObjectStorage
其他
- SPL
- 简介
- 安装/配置
- 预定义常量
- 数据结构
- 迭代器
- 接口
- 异常
- SPL 函数
- 文件处理
- 各种类及接口
下面举一个splheap堆的实例:
此段代码执行结果如下:
相关链接:
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代码的提交方法及要点
- 详解OJ(Online Judge)中PHP代码的提交方法及要点【举例:ZOJ 1001 (A + B Problem)】
- online judge 提交代码应该注意的事项
- OJ(Online Judge)系统及ACM测试题库大全
- OJ(Online Judge)系统及ACM测试题库大全
- OJ(Online Judge)系统及ACM测试题库大全
- OJ(Online Judge)系统及ACM测试题库大全
- OJ(Online Judge)系统及ACM测试题库大全
- 关于UVa Online Judge提交总是Wrong answer的问题
- My Online Judge (GUET OJ)
- 2013年6月九度Online Judge程序猿求职及面试月赛 - 题目1:二进制中1的个数
- 推荐一家日本的online judge:aizu online judge
- 关于Online Judge 的Tips
- 程序员的Job Security - 我的OJ(Online Judge)比较
- 基于java的ACM online judge(OJ)在线判题系统实现
- OJ 中,提交代码时,C++ 和 G++的区别
- TestNG方法测试及注意要点 代码及配置详解(解决testng方法不执行问题)
- Online Judge
- Delphi中添加WebBrowser扩展接口
- kafka 配置项 server.properties
- Linux 删除文件夹和文件的命令
- OOD的原则
- json错误处理org.json.JSONException: No value for XXX
- 详解OJ(Online Judge)中PHP代码的提交方法及要点
- mybatis连mysql中文乱码
- 程序算法复杂度速查表,赶紧mark起来!
- AndroidJNI 通过C++调用JAVA
- Codeforces Round #367 (Div. 2) E. Working routine (十字链表)
- C++文件操作
- pcl可视化的那些事
- vbscript 不显示串口执行某cmd
- Linux删除创建的用户并同时删除帐户的主目录