spoj Find Log

本文探讨了使用PHP处理数学运算时遇到的WrongAnswer问题,分析了原因在于PHP的输入方式,并提供了一种通过自定义函数逐字符读取并转换为整数的方法,最终实现了正确解答。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

哎,用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);


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值