Zhejiang University PTA < basic programming topic set >: 7-13 K candle chart
Topic content
The trend of stock price rise and fall is usually represented by K-line chart in candle chart technology, which is divided into daily K-line, weekly K-line and monthly K-line. Take the daily K-line as an example. Every day, the stock price goes from the opening to the closing one day, corresponding to a small candle chart, which represents four prices: the opening price Open (the first price of the opening transaction just in the morning), the closing price Close (the last price of the closing transaction in the afternoon), the highest price in the middle and the lowest price Low.
If Close < Open, it means BW solid; if Close > Open, it means R-Hollow; if Open equals Close, it means R-Cross. If Low is lower than Open and Close, it is called "Lower Shadow" (i.e. with Lower Shadow), and if High is higher than Open and Close, it is called "Upper Shadow" (i.e. with Upper Shadow). Please program, according to the given four price combinations, to determine what kind of candle the day is.
Input format
Input four positive real numbers in a row, corresponding to Open, High, Low and Close, separated by spaces.
Output format
Output the type of day K candle in one line. If there are up and down hatches, add with hatches after the type. If there are both hatches, output with Lower Shadow and Upper Shadow.
Input sample 1
5.110 5.250 5.100 5.105
Output sample 1
BW-Solid with Lower Shadow and Upper Shadow
Input example 2
5.110 5.110 5.110 5.110
Output sample 2
R-Cross
Input example 3
5.110 5.125 5.112 5.126
Output example 3
R-Hollow
Code 1: C language
It's only a third right
#include "stdio.h" int main(){ double Open, High, Low, Close; scanf("%f %f %f %f",&Open,&High,&Low,&Close); if (Close < Open) { if (Low < Open && Low < Close) { if (High > Open && High > Close) printf("BW-Solid with Lower Shadow and Upper Shadow"); else printf("BW-Solid with Lower Shadow"); } else if (High > Open && High > Close) printf("BW-Solid with Upper Shadow"); else printf("BW-Solid"); } else if (Close > Open){ if (Low < Open && Low < Close) { if (High > Open && High > Close) printf("R-Hollow with Lower Shadow and Upper Shadow"); else printf("R-Hollow with Lower Shadow"); } else if (High > Open && High > Close) { printf("R-Hollow with Upper Shadow"); } else printf("R-Hollow"); } else if (Close == Open) { if (Low < Open && Low < Close) { if (High > Open && High > Close) printf("R-Cross with Lower Shadow and Upper Shadow"); else printf("R-Cross with Lower Shadow"); } else if (High > Open && High > Close) printf("R-Cross with Upper Shadow"); else printf("R-Cross"); } return 0; }
Code 2: Python
This is right
# -*- coding: utf-8 -*- Open, High, Low, Close = input().split() Open = eval(Open) High = eval(High) Low = eval(Low) Close = eval(Close) if Close < Open: if Low < Open and Low < Close: if High > Open and High > Close: print("BW-Solid with Lower Shadow and Upper Shadow") else: print("BW-Solid with Lower Shadow") elif High > Open and High > Close: print("BW-Solid with Upper Shadow") else: print("BW-Solid") elif Close > Open: if Low < Open and Low < Close: if High > Open and High > Close: print("R-Hollow with Lower Shadow and Upper Shadow") else: print("R-Hollow with Lower Shadow") elif High > Open and High > Close: print("R-Hollow with Upper Shadow") else: print("R-Hollow") elif Close == Open: if Low < Open and Low < Close: if High > Open and High > Close: print("R-Cross with Lower Shadow and Upper Shadow") else: print("R-Cross with Lower Shadow") elif High > Open and High > Close: print("R-Cross with Upper Shadow") else: print("R-Cross")