PTA基础编程题目集 7-15 计算圆周率(C语言实现)

摘要:本文介绍了一种基于特定数学关系式计算圆周率近似值的方法。通过用户输入的阈值控制计算精度,当级数项的绝对值小于阈值时停止累加。文章详细说明了输入输出格式,提供了算法流程图展示计算过程,并给出了完整的C语言实现代码,包含详细的注释说明关键变量和计算逻辑。

题目描述

根据下面关系式,求圆周率的值,直到最后一项的值小于给定阈值。

关系式:

复制代码
π/2 = 1 + 1/3 + 2!/(3×5) + 3!/(3×5×7) + ... + n!/(3×5×7×...×(2n+1)) + ...

输入格式:

输入在一行中给出小于 1 的阈值。

输出格式:

在一行中输出满足阈值条件的近似圆周率,输出到小数点后 6 位。

输入样例:

复制代码
0.01

输出样例:

复制代码
3.132157

算法流程

下面是计算圆周率的算法流程图,展示了从输入阈值到输出π值的完整过程:
#mermaid-svg-Cm3sb7h5f6c8GcaE{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-Cm3sb7h5f6c8GcaE .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Cm3sb7h5f6c8GcaE .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Cm3sb7h5f6c8GcaE .error-icon{fill:#552222;}#mermaid-svg-Cm3sb7h5f6c8GcaE .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Cm3sb7h5f6c8GcaE .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Cm3sb7h5f6c8GcaE .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Cm3sb7h5f6c8GcaE .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Cm3sb7h5f6c8GcaE .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Cm3sb7h5f6c8GcaE .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Cm3sb7h5f6c8GcaE .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Cm3sb7h5f6c8GcaE .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Cm3sb7h5f6c8GcaE .marker.cross{stroke:#333333;}#mermaid-svg-Cm3sb7h5f6c8GcaE svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Cm3sb7h5f6c8GcaE p{margin:0;}#mermaid-svg-Cm3sb7h5f6c8GcaE .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-Cm3sb7h5f6c8GcaE .cluster-label text{fill:#333;}#mermaid-svg-Cm3sb7h5f6c8GcaE .cluster-label span{color:#333;}#mermaid-svg-Cm3sb7h5f6c8GcaE .cluster-label span p{background-color:transparent;}#mermaid-svg-Cm3sb7h5f6c8GcaE .label text,#mermaid-svg-Cm3sb7h5f6c8GcaE span{fill:#333;color:#333;}#mermaid-svg-Cm3sb7h5f6c8GcaE .node rect,#mermaid-svg-Cm3sb7h5f6c8GcaE .node circle,#mermaid-svg-Cm3sb7h5f6c8GcaE .node ellipse,#mermaid-svg-Cm3sb7h5f6c8GcaE .node polygon,#mermaid-svg-Cm3sb7h5f6c8GcaE .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Cm3sb7h5f6c8GcaE .rough-node .label text,#mermaid-svg-Cm3sb7h5f6c8GcaE .node .label text,#mermaid-svg-Cm3sb7h5f6c8GcaE .image-shape .label,#mermaid-svg-Cm3sb7h5f6c8GcaE .icon-shape .label{text-anchor:middle;}#mermaid-svg-Cm3sb7h5f6c8GcaE .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-Cm3sb7h5f6c8GcaE .rough-node .label,#mermaid-svg-Cm3sb7h5f6c8GcaE .node .label,#mermaid-svg-Cm3sb7h5f6c8GcaE .image-shape .label,#mermaid-svg-Cm3sb7h5f6c8GcaE .icon-shape .label{text-align:center;}#mermaid-svg-Cm3sb7h5f6c8GcaE .node.clickable{cursor:pointer;}#mermaid-svg-Cm3sb7h5f6c8GcaE .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-Cm3sb7h5f6c8GcaE .arrowheadPath{fill:#333333;}#mermaid-svg-Cm3sb7h5f6c8GcaE .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Cm3sb7h5f6c8GcaE .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Cm3sb7h5f6c8GcaE .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Cm3sb7h5f6c8GcaE .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-Cm3sb7h5f6c8GcaE .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Cm3sb7h5f6c8GcaE .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-Cm3sb7h5f6c8GcaE .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Cm3sb7h5f6c8GcaE .cluster text{fill:#333;}#mermaid-svg-Cm3sb7h5f6c8GcaE .cluster span{color:#333;}#mermaid-svg-Cm3sb7h5f6c8GcaE div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Cm3sb7h5f6c8GcaE .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-Cm3sb7h5f6c8GcaE rect.text{fill:none;stroke-width:0;}#mermaid-svg-Cm3sb7h5f6c8GcaE .icon-shape,#mermaid-svg-Cm3sb7h5f6c8GcaE .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Cm3sb7h5f6c8GcaE .icon-shape p,#mermaid-svg-Cm3sb7h5f6c8GcaE .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-Cm3sb7h5f6c8GcaE .icon-shape .label rect,#mermaid-svg-Cm3sb7h5f6c8GcaE .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Cm3sb7h5f6c8GcaE .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-Cm3sb7h5f6c8GcaE .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-Cm3sb7h5f6c8GcaE :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 否
是
开始
输入阈值 threshold
初始化: sum=1.0, term=1.0, n=1
计算下一项: term = term * n / (2n+1)
累加: sum = sum + term
term < threshold?
n = n + 1
计算 π = 2 * sum
输出 π (保留6位小数)
结束

流程说明:

  1. 初始化 :设置累加和 sum 为第一项 1.0,当前项 term 为 1.0,项序号 n 为 1
  2. 循环计算:根据递推公式计算下一项的值
  3. 累加求和:将当前项加到总和中
  4. 精度判断:检查当前项是否小于用户输入的阈值
  5. 循环或结束:如果项值仍大于阈值,增加 n 继续计算;否则跳出循环
  6. 结果计算:根据关系式 π/2 = sum,计算 π = 2 × sum
  7. 格式化输出:输出保留6位小数的圆周率近似值

该算法通过控制阈值来平衡计算精度与性能,阈值越小,计算项数越多,结果越精确。

代码部分实现

c 复制代码
#include <stdio.h>

int main(void)
{
    double threshold;                     // 用户输入的阈值,用于控制计算精度
    scanf("%lf", &threshold);            // 读取阈值
    
    double sum = 1.0;                    // 累加和,初始为第一项 1(对应关系式中的 1)
    double term = 1.0;                   // 当前项的值,初始为第一项 1
    int n = 1;                           // 当前项的序号,从 1 开始(对应关系式中的 n)
    
    while (1) {
        // 根据递推公式计算下一项:term_{n} = term_{n-1} * n / (2n+1)
        term *= (double)n / (2 * n + 1);
        // 将当前项累加到 sum 中
        sum += term;
        // 如果当前项已经小于阈值,则停止累加
        if (term < threshold) {
            break;
        }
        n++;                              // 准备计算下一项
    }
    
    // 根据关系式 π/2 = sum,因此 π = 2 * sum
    double pi = 2.0 * sum;
    printf("%.6f\n", pi);                // 输出圆周率,保留 6 位小数
    
    return 0;
}
相关推荐
erzong23123 分钟前
Linux操作系统:线程概念与控制
linux·算法
普通人641 分钟前
php+html+canvas实现树状打开的结构
开发语言·javascript·html5·1024程序员节
wuminyu1 小时前
JMM学习总结:深入理解happens-before关系
java·linux·c语言·jvm·c++
电商API_180079052471 小时前
反向海淘多币种结算:汇率该在哪个时点被冻结
java·开发语言·数据库·跨境电商·1688 api
Nil2081 小时前
leetcode 5最长回文子串
算法·leetcode·职场和发展
xxwl5851 小时前
区域赛分类题解
c++·笔记·学习·算法
m0_674571971 小时前
力扣 hot 100 python
python·算法·leetcode
开开心心就好2 小时前
多份文档怎么批量打印?支持多种格式
java·服务器·开发语言·pdf·ocr·散列表·启发式算法
Want5952 小时前
Python炫酷烟花①
开发语言·python
青少儿编程课堂2 小时前
蓝桥杯+全国青少年信息素养大赛+电子学会PythonC/C++考级:最长公共子序列LCS解析
c++·python·算法·bfs·信息学竞赛