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;
}
相关推荐
问商十三载1 小时前
AI 引擎生成式优化两种思路怎么选?2026 对比分析附选型方法
人工智能·算法
前端炒粉1 小时前
简易实现ssr
开发语言·前端·javascript
库克克2 小时前
【C++】 unordered_map 与unordered_set
开发语言·c++
雪的季节2 小时前
人工智能 AI 5问
开发语言
hanlin032 小时前
刷题笔记:力扣第704、977、209题(数组相关)
笔记·算法·leetcode
李迟3 小时前
一种轻量级C++ CSV文件读写库的实现方案
开发语言·c++
陕西企来客3 小时前
生成式引擎优化(GEO)行业白皮书:从流量分配到认知占有的战略跃迁
人工智能·算法·机器学习·技术好geo优化
小园子的小菜3 小时前
Java 并发编程:线程安全队列全解 —— 阻塞与非阻塞实现原理及源码深度剖析
java·开发语言