哎,用PHP提交总是Wrong Answer
#include <cstdio>
#include <cmath>
using namespace std;
int main()
{
int t;
scanf("%d", &t);
for (int i = 1; i <= t; i++) {
long long b, n;
scanf("%lld%lld", &b, &n);
printf("Case %d: ", i);
if (b == 0 || b == 1 || n == 0) {
printf("Math Error!\n");
} else {
printf("%.5f\n", log(n) / log(b));
}
}
return 0;
}
可能php中的每个例子中的两个数不是处于一行中,而fscanf在php中是读取一行的。换成自己实现读取整数后,提交终于成功
代码如下:
<?php
function readInt($file)
{
while (false !== ($ch = fgetc($file))) {
if (ord($ch) >= ord('0') && ord($ch) <= ord('9')) break;
}
$sum = ord($ch) - ord('0');
while (false !== ($ch = fgetc($file))) {
if (!(ord($ch) >= ord('0') && ord($ch) <= ord('9'))) break;
$sum = $sum * 10 + (ord($ch) - ord('0'));
}
return $sum;
}
$debug = true;
$file = STDIN;
if ($debug) $file = fopen('./spoj.txt', 'r');
$t = readInt($file);
for ($i = 1; $i <= $t; $i++) {
$b = readInt($file);
$n = readInt($file);
printf("Case %d: ", $i);
if (0 == $b || 1 == $b || 0 == $n) {
printf("Math Error!\n");
} else {
printf("%.5f\n", log($n) / log($b));
}
}
if ($debug) fclose($file);