PTA基础编程题目集 7-13 日K蜡烛图(C语言实现)

摘要:本文介绍了一个基于蜡烛图(K线)技术的股票价格趋势判断程序。给定开盘价(Open)、最高价(High)、最低价(Low)和收盘价(Close)四个价格,程序需要判断蜡烛的类型(BW-Solid、R-Hollow 或 R-Cross)以及是否存在上影线(Upper Shadow)或下影线(Lower Shadow)。文章提供了完整的输入输出格式说明、多个测试样例,并给出了详细的C语言实现代码,包含关键逻辑的中文注释和代码思路解析。

题目描述

股票价格涨跌趋势,常用蜡烛图技术中的K 线图来表示,分为按日的日K线、按周的周 K 线、按月的月 K 线等。以日K线为例,每天股票价格从开盘到收盘走完一天,对应一根蜡烛小图,要表示四个价格:开盘价格Open (早上刚刚开始开盘买卖成交的第1笔价格)、收盘价格Close (下午收盘时最后一笔成交的价格)、中间的最高价High和最低价Low。

如果Close<Open,表示为"BW-Solid"(即"实心蓝白蜡烛");如果Close>Open,表示为"R-Hollow"(即"空心红蜡烛");如果Open等于Close,则为"R-Cross"(即"十字红蜡烛")。如果Low比Open和Close低,称为"Lower Shadow"(即"有下影线"),如果High比Open和Close高,称为"Upper Shadow"(即"有上影线")。请编程序,根据给定的四个价格组合,判断当日的蜡烛是一根什么样的蜡烛。

输入格式:

输入在一行中给出4个正实数,分别对应Open、 High、 Low、 Close,其间以空格分隔。

输出格式:

在一行中输出日 K 蜡烛的类型。如果有上、下影线,则在类型后加上with 影线类型。如果两种影线都有,则输出with Lower Shadow and Upper Shadow。

输入样例:

in 复制代码
5.110 5.250 5.100 5.105
in 复制代码
5.110 5.110 5.110 5.110
in 复制代码
5.110 5.125 5.112 5.126

输出样例:

out 复制代码
BW-Solid with Lower Shadow and Upper Shadow
out 复制代码
R-Cross
out 复制代码
R-Hollow

蜡烛图类型判断流程图

#mermaid-svg-jVn8yqWPO3DoyqnC{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-jVn8yqWPO3DoyqnC .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-jVn8yqWPO3DoyqnC .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-jVn8yqWPO3DoyqnC .error-icon{fill:#552222;}#mermaid-svg-jVn8yqWPO3DoyqnC .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-jVn8yqWPO3DoyqnC .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-jVn8yqWPO3DoyqnC .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-jVn8yqWPO3DoyqnC .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-jVn8yqWPO3DoyqnC .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-jVn8yqWPO3DoyqnC .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-jVn8yqWPO3DoyqnC .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-jVn8yqWPO3DoyqnC .marker{fill:#333333;stroke:#333333;}#mermaid-svg-jVn8yqWPO3DoyqnC .marker.cross{stroke:#333333;}#mermaid-svg-jVn8yqWPO3DoyqnC svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-jVn8yqWPO3DoyqnC p{margin:0;}#mermaid-svg-jVn8yqWPO3DoyqnC .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-jVn8yqWPO3DoyqnC .cluster-label text{fill:#333;}#mermaid-svg-jVn8yqWPO3DoyqnC .cluster-label span{color:#333;}#mermaid-svg-jVn8yqWPO3DoyqnC .cluster-label span p{background-color:transparent;}#mermaid-svg-jVn8yqWPO3DoyqnC .label text,#mermaid-svg-jVn8yqWPO3DoyqnC span{fill:#333;color:#333;}#mermaid-svg-jVn8yqWPO3DoyqnC .node rect,#mermaid-svg-jVn8yqWPO3DoyqnC .node circle,#mermaid-svg-jVn8yqWPO3DoyqnC .node ellipse,#mermaid-svg-jVn8yqWPO3DoyqnC .node polygon,#mermaid-svg-jVn8yqWPO3DoyqnC .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-jVn8yqWPO3DoyqnC .rough-node .label text,#mermaid-svg-jVn8yqWPO3DoyqnC .node .label text,#mermaid-svg-jVn8yqWPO3DoyqnC .image-shape .label,#mermaid-svg-jVn8yqWPO3DoyqnC .icon-shape .label{text-anchor:middle;}#mermaid-svg-jVn8yqWPO3DoyqnC .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-jVn8yqWPO3DoyqnC .rough-node .label,#mermaid-svg-jVn8yqWPO3DoyqnC .node .label,#mermaid-svg-jVn8yqWPO3DoyqnC .image-shape .label,#mermaid-svg-jVn8yqWPO3DoyqnC .icon-shape .label{text-align:center;}#mermaid-svg-jVn8yqWPO3DoyqnC .node.clickable{cursor:pointer;}#mermaid-svg-jVn8yqWPO3DoyqnC .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-jVn8yqWPO3DoyqnC .arrowheadPath{fill:#333333;}#mermaid-svg-jVn8yqWPO3DoyqnC .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-jVn8yqWPO3DoyqnC .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-jVn8yqWPO3DoyqnC .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-jVn8yqWPO3DoyqnC .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-jVn8yqWPO3DoyqnC .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-jVn8yqWPO3DoyqnC .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-jVn8yqWPO3DoyqnC .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-jVn8yqWPO3DoyqnC .cluster text{fill:#333;}#mermaid-svg-jVn8yqWPO3DoyqnC .cluster span{color:#333;}#mermaid-svg-jVn8yqWPO3DoyqnC 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-jVn8yqWPO3DoyqnC .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-jVn8yqWPO3DoyqnC rect.text{fill:none;stroke-width:0;}#mermaid-svg-jVn8yqWPO3DoyqnC .icon-shape,#mermaid-svg-jVn8yqWPO3DoyqnC .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-jVn8yqWPO3DoyqnC .icon-shape p,#mermaid-svg-jVn8yqWPO3DoyqnC .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-jVn8yqWPO3DoyqnC .icon-shape .label rect,#mermaid-svg-jVn8yqWPO3DoyqnC .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-jVn8yqWPO3DoyqnC .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-jVn8yqWPO3DoyqnC .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-jVn8yqWPO3DoyqnC :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 是







开始:输入Open, High, Low, Close
比较Close与Open
Close < Open?
类型 = BW-Solid

(实心蓝白蜡烛)
Close > Open?
类型 = R-Hollow

(空心红蜡烛)
类型 = R-Cross

(十字红蜡烛)
CheckShadow
Low < Open

且 Low < Close?
有下影线
无下影线
High > Open

且 High > Close?
有上影线
无上影线
输出:类型 + with Lower Shadow and Upper Shadow
输出:类型 + with Lower Shadow
输出:类型 + with Upper Shadow
输出:类型
结束

代码部分实现

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

int main(void)
{
    // 定义四个价格变量:开盘价、最高价、最低价、收盘价
    double Open, High, Low, Close;
    // 定义两个标志变量:是否有下影线、是否有上影线
    int hasLower, hasUpper;
    // 定义蜡烛类型字符串指针
    const char *type;
    
    // 读取用户输入的四个价格值
    scanf("%lf %lf %lf %lf", &Open, &High, &Low, &Close);
    
    // 关键逻辑1:判断蜡烛类型(实心、空心还是十字)
    if (Close < Open) {
        // 收盘价低于开盘价,为实心蓝白蜡烛
        type = "BW-Solid";
    } else if (Close > Open) {
        // 收盘价高于开盘价,为空心红蜡烛
        type = "R-Hollow";
    } else {
        // 收盘价等于开盘价,为十字红蜡烛
        type = "R-Cross";
    }
    
    // 关键逻辑2:判断是否有下影线(最低价低于开盘价和收盘价)
    hasLower = (Low < Open && Low < Close) ? 1 : 0;
    // 关键逻辑3:判断是否有上影线(最高价高于开盘价和收盘价)
    hasUpper = (High > Open && High > Close) ? 1 : 0;
    
    // 输出蜡烛类型
    printf("%s", type);
    
    // 关键逻辑4:根据影线情况输出附加信息
    if (hasLower && hasUpper) {
        // 同时有上下影线
        printf(" with Lower Shadow and Upper Shadow");
    } else if (hasLower) {
        // 只有下影线
        printf(" with Lower Shadow");
    } else if (hasUpper) {
        // 只有上影线
        printf(" with Upper Shadow");
    }
    
    // 输出换行符
    printf("\n");
    
    return 0;
}

代码思路与关键点解析

本代码实现了日K蜡烛图类型的判断逻辑,整体思路清晰,分为以下几个关键步骤:

  1. 数据输入 :使用 scanf 函数读取用户输入的四个价格值(Open, High, Low, Close)。

  2. 蜡烛类型判断 :通过比较收盘价 Close 和开盘价 Open 的关系确定蜡烛类型:

    • Close < Open → "BW-Solid"(实心蓝白蜡烛)
    • Close > Open → "R-Hollow"(空心红蜡烛)
    • Close == Open → "R-Cross"(十字红蜡烛)
  3. 影线判断:分别检查是否有下影线和上影线:

    • 下影线:最低价 Low 同时低于开盘价 Open 和收盘价 Close
    • 上影线:最高价 High 同时高于开盘价 Open 和收盘价 Close
  4. 结果输出:先输出蜡烛类型,再根据影线情况追加相应的影线描述。

关键点说明

  • 使用三元运算符 ? : 简化了影线标志的赋值,使代码更简洁
  • 影线判断逻辑严格遵循题目定义:影线需要同时满足与两个价格(Open和Close)的比较关系
  • 输出格式严格按照题目要求,先类型后影线描述,用 "with" 连接
  • 代码结构清晰,变量命名具有自解释性,便于理解和维护

这段代码的时间复杂度为 O(1),空间复杂度为 O(1),适合处理单个测试用例的输入输出需求。

程序执行流程图

#mermaid-svg-puuby53hrE4m55Az{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-puuby53hrE4m55Az .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-puuby53hrE4m55Az .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-puuby53hrE4m55Az .error-icon{fill:#552222;}#mermaid-svg-puuby53hrE4m55Az .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-puuby53hrE4m55Az .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-puuby53hrE4m55Az .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-puuby53hrE4m55Az .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-puuby53hrE4m55Az .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-puuby53hrE4m55Az .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-puuby53hrE4m55Az .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-puuby53hrE4m55Az .marker{fill:#333333;stroke:#333333;}#mermaid-svg-puuby53hrE4m55Az .marker.cross{stroke:#333333;}#mermaid-svg-puuby53hrE4m55Az svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-puuby53hrE4m55Az p{margin:0;}#mermaid-svg-puuby53hrE4m55Az .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-puuby53hrE4m55Az .cluster-label text{fill:#333;}#mermaid-svg-puuby53hrE4m55Az .cluster-label span{color:#333;}#mermaid-svg-puuby53hrE4m55Az .cluster-label span p{background-color:transparent;}#mermaid-svg-puuby53hrE4m55Az .label text,#mermaid-svg-puuby53hrE4m55Az span{fill:#333;color:#333;}#mermaid-svg-puuby53hrE4m55Az .node rect,#mermaid-svg-puuby53hrE4m55Az .node circle,#mermaid-svg-puuby53hrE4m55Az .node ellipse,#mermaid-svg-puuby53hrE4m55Az .node polygon,#mermaid-svg-puuby53hrE4m55Az .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-puuby53hrE4m55Az .rough-node .label text,#mermaid-svg-puuby53hrE4m55Az .node .label text,#mermaid-svg-puuby53hrE4m55Az .image-shape .label,#mermaid-svg-puuby53hrE4m55Az .icon-shape .label{text-anchor:middle;}#mermaid-svg-puuby53hrE4m55Az .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-puuby53hrE4m55Az .rough-node .label,#mermaid-svg-puuby53hrE4m55Az .node .label,#mermaid-svg-puuby53hrE4m55Az .image-shape .label,#mermaid-svg-puuby53hrE4m55Az .icon-shape .label{text-align:center;}#mermaid-svg-puuby53hrE4m55Az .node.clickable{cursor:pointer;}#mermaid-svg-puuby53hrE4m55Az .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-puuby53hrE4m55Az .arrowheadPath{fill:#333333;}#mermaid-svg-puuby53hrE4m55Az .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-puuby53hrE4m55Az .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-puuby53hrE4m55Az .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-puuby53hrE4m55Az .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-puuby53hrE4m55Az .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-puuby53hrE4m55Az .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-puuby53hrE4m55Az .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-puuby53hrE4m55Az .cluster text{fill:#333;}#mermaid-svg-puuby53hrE4m55Az .cluster span{color:#333;}#mermaid-svg-puuby53hrE4m55Az 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-puuby53hrE4m55Az .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-puuby53hrE4m55Az rect.text{fill:none;stroke-width:0;}#mermaid-svg-puuby53hrE4m55Az .icon-shape,#mermaid-svg-puuby53hrE4m55Az .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-puuby53hrE4m55Az .icon-shape p,#mermaid-svg-puuby53hrE4m55Az .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-puuby53hrE4m55Az .icon-shape .label rect,#mermaid-svg-puuby53hrE4m55Az .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-puuby53hrE4m55Az .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-puuby53hrE4m55Az .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-puuby53hrE4m55Az :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Close < Open
Close > Open
Close == Open
hasLower && hasUpper
hasLower && !hasUpper
!hasLower && hasUpper
!hasLower && !hasUpper
影线判断
hasLower = (Low < Open && Low < Close) ? 1 : 0
hasUpper = (High > Open && High > Close) ? 1 : 0
开始程序执行
定义变量:

Open, High, Low, Close, hasLower, hasUpper, type
读取输入:

scanf读取4个价格
判断蜡烛类型

Close vs Open
type = 'BW-Solid'
type = 'R-Hollow'
type = 'R-Cross'
输出蜡烛类型:printf(type)
影线组合判断
输出 ' with Lower Shadow and Upper Shadow'
输出 ' with Lower Shadow'
输出 ' with Upper Shadow'
不输出影线信息
输出换行符:printf('\

')
返回0,程序结束

相关推荐
csdn_aspnet1 小时前
C# 从凸包中删除点(Deleting points from Convex Hull)
开发语言·c#
Databend1 小时前
从全表排序到概率统计:Databend 如何用 KLL、Top-N 与 CMS 改进基数估计
大数据·数据库·算法
cts6181 小时前
Python全栈claude.md文档
开发语言·python
imuliuliang1 小时前
分治法在大数据计算中的并行化应用探索7
算法
夜不会漫长1 小时前
数据结构:栈和队列(1)
数据结构
Lsir10110_2 小时前
【力扣hot100】矩阵题通关复盘
算法·leetcode·矩阵
退休倒计时2 小时前
【每日一题】LeetCode 207. 课程表 TypeScript
算法·leetcode·typescript
忘路之远近i2 小时前
受够阿里云自带终端后,我用 Cursor + grill-me 做了个运维面板
服务器·开发语言·人工智能·python·阿里云·云计算
Tim_102 小时前
【C++】020、野指针&悬空指针
java·开发语言