UVA 10200 (高精度)

Euler is a well-known matematician, and, among many other things, he discovered that the formula
n
2 + n + 41 produces a prime for 0 ≤ n < 40. For n = 40, the formula produces 1681, which is 41 ∗ 41.
Even though this formula doesn’t always produce a prime, it still produces a lot of primes. It’s known
that for n ≤ 10000000, there are 47,5% of primes produced by the formula!
So, you’ll write a program that will output how many primes does the formula output for a certain
interval.
Input
Each line of input will be given two positive integer a and b such that 0 ≤ a ≤ b ≤ 10000. You must
read until the end of the file.
Output
For each pair a, b read, you must output the percentage of prime numbers produced by the formula in
this interval (a ≤ n ≤ b) rounded to two decimal digits.
Sample Input
0 39
0 40
39 40
Sample Output
100.00
97.56
50.00

打表不说,高精度什么鬼,能不能好好的了,输出加一个 1e-8 有毛线用啊,不加还不让过,第一次遇到这种高精度的体啊,被坑死了。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
using namespace std;
#define M 10010
#define LL long long

int al[M];
LL x;
bool isprime(LL x)
{
    for(LL i=2; i*i<=x; i++)
    {
        if(x % i == 0)
            return false;
    }
    return true;
}
int num = 0;
void dabiao()
{
    for(LL i=0; i<M; i++)
    {
        x = i * i + i + 41;
        if(!isprime(x))
        {
            num++;
        }
        al[i] = num;
    }
}

int main()
{
    dabiao();
    int a, b;
    double ans = 0;
    while(scanf("%d%d", &a, &b) != EOF)
    {
        if(!a)
        {
            ans = 1.0 - (double)al[b] / (double)(b+1);

        }
        else
        {
            ans = 1.0 - (double)(al[b] - al[a-1]) / (double)(b - a + 1);
        }
        ans = ans * 100.0;
        printf("%.2lf\n", ans + 1e-8);
    }

    return 0;
}
### 解决方案概述 对于UVa 11809 浮点数问题,核心挑战在于理解和处理浮点数表示及其精度限制。该问题涉及如何解析给定的二进制字符串来确定其代表的最大十进制数值[^3]。 ### 数据结构与算法设计 #### 输入分析 输入由多组测试案例组成,每组包含两个整数E和M,分别指示阶码长度和尾数长度。目标是从这些参数推导出可表示的最大正实数,并将其转换为科学计数法形式输出。 #### 关键概念解释 - **最大浮点数**:基于指定的E和M值构建最大的浮点数意味着设置所有的位都为最高有效状态——即全设为`1`。 - **指数部分**:考虑到偏置(bias),实际使用的指数应等于给出的E减去一个固定的偏移量;这里假设采用IEEE标准,则bias=(2^(E−1))−1。 - **小数部分**:因为隐含前导'1.'的存在,在计算最终结果时需额外加上这一项。 #### 实现细节 为了实现上述逻辑,可以按照如下方式编写程序: ```cpp #include <iostream> #include <cmath> using namespace std; int main() { int E, M; while (cin >> E >> M && !(E == 0 && M == 0)) { // 结束条件 double max_fraction = pow(2.0, M) - 1; // 尾数全部为1的情况下的分数部分 // 计算指数的实际大小(考虑偏置) int bias = pow(2, E - 1) - 1; int exp_value = (pow(2, E) - 1) - bias; // 构造最大浮点数 double result = (max_fraction / pow(2.0, M)) * pow(2.0, exp_value); cout << "Case " << ++case_num << ": "; printf("%.0f\n", result); // 输出不带小数点的形式 } } ``` 此代码片段展示了如何读取输入、执行必要的数学运算以及格式化输出以满足题目要求。特别需要注意的是,当涉及到浮点数操作时,应当谨慎对待可能出现的舍入误差等问题[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值