详解OJ(Online Judge)中PHP代码的提交方法及要点

Posted Wss0130

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了详解OJ(Online Judge)中PHP代码的提交方法及要点相关的知识,希望对你有一定的参考价值。

详解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]  view plain  copy  
  1. <?php  
  2.     $fd = STDIN; // STDIN是oj提供的常量,等价于 fopen("php://stdin","r"));    
  3.         // $fd=fopen('inputfileName','+r');  
  4.     while(fscanf($fd"%d %d"$a$b)==2)  
  5.         echo ($a + $b)."\\n";   
  6. ?>  

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


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

[php]  view plain  copy  
  1. <?php  
  2. while(fscanf(STDIN, "%d %d"$a$b) == 2)  
  3.     print ($a + $b)."\\n";  


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


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

[php]  view plain  copy  
  1. <?php  
  2. function judge($m$n)  
  3.   
  4.     $from = 0;  
  5.     for($i = 2; $i < $n$i++)  
  6.       $from = ($from + $m)%($i);  
  7.     if($from + 1 == 1) return 1;  
  8.     else return 0;  
  9.   
  10.     $fd = STDIN;  // STDIN是oj提供的常量,等价于 fopen("php://stdin","r"));  
  11.     while(fscanf($fd"%d"$n)==1 && $n!=0)  
  12.       
  13.         $m = 1;  
  14.         while(!judge($m$n))  
  15.           
  16.             ++$m;  
  17.           
  18.         echo $m."\\n";  
  19.       


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

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

[php]  view plain  copy  
  1. <?php  
  2. // To-Do: 约瑟夫环问题  
  3.     $fd = fopen("stdin""r");  
  4.     // $fd = STDIN;  
  5.     function judge($m$n)  
  6.       
  7.         $from = 0;  
  8.         for($i = 2; $i < $n$i++)  
  9.           $from = ($from + $m)%($i);  
  10.         if($from + 1 == 1) return 1;  
  11.         else return 0;  
  12.       
  13.   
  14.     while(fscanf($fd,"%d"$n)==1 && $n!=0)  
  15.       
  16.         $m = 1;  
  17.         while(!judge($m$n))  
  18.           
  19.             ++$m;  
  20. 关于北大OJ(Online Judge)

    Online Judge(OJ)搭建——5配置

    Online Judge(OJ)搭建——2数据库构建

    开发者养成常见在线测评平台(OJ,Online Judge)测评状态

    浅谈online judge平台 spj [special judge] 使用 | 修改问题

    编程--在线提交系统(Online Judge)