当前位置:网站首页>Common misunderstandings caused by a time reporting assistant of Blue Bridge Cup basic questions
Common misunderstandings caused by a time reporting assistant of Blue Bridge Cup basic questions
2022-07-25 19:27:00 【Aricl.】
List of articles
- Based on practice —— Time Reporting Assistant
- Common misconceptions about : Returns the address of a local variable in a function
- Summary
One 、 Time Reporting Assistant
Problem description
Given the current time , Please read it in English .
Time spent h Sum points m Express , In English pronunciation , The way to read a time is :
If m by 0, Read out the tense , And then add “o'clock”, Such as 3:00 pronounce as “three o'clock”.
If m Not for 0, Read out the tense , And then read it out , Such as 5:30 pronounce as “five thirty”.
The pronunciation of the hour and the minute is in English number , among 0~20 pronounce as :
0:zero, 1: one, 2:two, 3:three, 4:four, 5:five, 6:six, 7:seven, 8:eight, 9:nine, 10:ten, 11:eleven, 12:twelve, 13:thirteen, 14:fourteen, 15:fifteen, 16:sixteen, 17:seventeen, 18:eighteen, 19:nineteen, 20:twenty.
30 pronounce as thirty,40 pronounce as forty,50 pronounce as fifty.
For greater than 20 Less than 60 The number of , First read the whole number of ten , And then add a single digit . Such as 31 Read first 30 add 1 How to read it , pronounce as “thirty one”.
According to the above rules 21:54 pronounce as “twenty one fifty four”,9:07 pronounce as “nine seven”,0:15 pronounce as “zero fifteen”.
Input format
The input contains two nonnegative integers h and m, The hours and minutes of time . Non zero numbers have no leading 0.h Less than 24,m Less than 60.
Output format
Output time and time in English .
The sample input
0 15
Sample output
zero fifteen
Their thinking :
At first glance, the text of this topic is very complicated , In fact, if you read it carefully twice, you will find that the topic is very simple , It is to realize a simple English time reporting function . The complexity lies in 24 There are many kinds of hours , We should solve it one by one according to the situation .24 The time of hour system mainly has the following forms :
1. The minute bit is 0, The hour is not 0, That is, the whole point . Such as :21:00 Output :twenty one o'clock
2. The hour is 0, Minutes are not zero , Such as :00:15 Output :zero fifteen
3. The hour is not 0, The minute digit is a whole ten digit , Such as :20:30 Output :twenty thirty
4. Hours is not 0, The minute bits have several dozen forms , Such as :21:37 Output : twenty one thirty seven
Therefore, the general functions of functions can be divided into two , Process the number on the hour , Handle minute digits , Then return a string to the main function , Output... In the main function . Function mainly uses switch Multiple branch statements to complete the matching between numbers and English strings , At the same time C In language string Library function , Complete the copy and splicing of strings .
The complete code is as follows :
#include<stdio.h>
#include<string.h>
// Set three global string array variables
char cha[20];
char s3[20];
char minute[20];
// Handle " Hours " position
char* function1(int h){
//char cha[20];
switch(h){
case 0:strcpy(cha,"zero");break;
case 1:strcpy(cha,"one");break;
case 2:strcpy(cha,"two");break;
case 3:strcpy(cha,"three");break;
case 4:strcpy(cha,"four");break;
case 5:strcpy(cha,"five");break;
case 6:strcpy(cha,"six");break;
case 7:strcpy(cha,"seven");break;
case 8:strcpy(cha,"eight");break;
case 9:strcpy(cha,"nine");break;
case 10:strcpy(cha,"ten");break;
case 11:strcpy(cha,"eleven");break;
case 12:strcpy(cha,"twelve");break;
case 13:strcpy(cha,"thirteen");break;
case 14:strcpy(cha,"fourteen");break;
case 15:strcpy(cha,"fifteen");break;
case 16:strcpy(cha,"sixteen");break;
case 17:strcpy(cha,"seventeen");break;
case 18:strcpy(cha,"eighteen");break;
case 19:strcpy(cha,"nineteen");break;
case 20:strcpy(cha,"twenty");break;
case 21:strcpy(cha,"twenty one");break;
case 22:strcpy(cha,"twenty two");break;
case 23:strcpy(cha,"twenty three");break;
case 24:strcpy(cha,"twenty four");break;
case 30:strcpy(cha,"thirty");break;
case 40:strcpy(cha,"forty");break;
case 50:strcpy(cha,"fifty");break;
}
return cha;
}
// Handle ten bits of minutes
char *function3(int x){
//char s3[20];
switch(x){
case 1:strcpy(s3,"ten ");break;
case 2:strcpy(s3,"twenty ");break;
case 3:strcpy(s3,"thirty ");break;
case 4:strcpy(s3,"forty ");break;
case 5:strcpy(s3,"fifty ");break;
}
return s3;
}
// Handle " minute " position
char *function2(int m){
//char minute[20];
if(m%10!=0){
if(m<10){
strcpy(minute,function1(m));
}
else{
// Because only 1~20 Only the numbers between have a specific English match , Others need to be disassembled
if(m>20&&m<60){
strcpy(minute,function3(m/10));// The parameter passed in is ten digits of the number of minutes
strcat(minute,function1(m%10));// The parameter passed in is one bit of the minute digit
}
// Figures that do not need to be disassembled
else{
strcpy(minute,function1(m));
}
}
}
else{
strcpy(minute,function1(m));// When m When the single digit of is zero , It's a whole ten
}
return minute;
}
int main(){
int h,m;
char hour[20],minute[20];
scanf("%d%d",&h,&m);
if(m!=0){
strcpy(hour,function1(h));
strcpy(minute,function2(m));
printf("%s %s",hour,minute);
}
else{
strcpy(hour,function1(h));
printf("%s o'clock",hour);
}
return 0;
}
Running results :


Two 、 Common misconceptions about
1. You cannot return the address of a local variable in a function
The wrong sample :

The compiler gives a warning :

2. Error cause analysis
When using character arrays , A new backup will be created in the stack , It will be created as the function is called , It will be automatically destroyed as the function ends . Even if it returns an address , But at the end of this function , The contents of the memory space pointed to by this address have been released , Therefore, the content corresponding to that address can no longer be found through the returned address , So it is wrong to return the address of a local variable in a function .
3. Solution
(1) Use global variables
Because global variables have static storage , Only when the whole program is finished , The data stored in memory will be released .
(2) Use character pointers
When using character pointers , The string is stored in static storage ( Heap area ), So the returned address points to the content ( In the pile area ) Will not be released , So it can be found .( But this way of writing has one drawback , That is, after defining the string pointer , You can't change the string it points to , Unexpected errors may occur after the change ).
(3) Use static keyword
Use static Keyword declares it as a static variable , With the method 1 The same principle .
3、 ... and 、 Summary
1.static Static variables
static When used for function definition or variable declaration outside the code block , Used to modify the link properties of identifiers , From external links to internal links , But the storage type and scope are not affected , Functions or variables declared in this way can only be accessed in their source files .
When it is used for variable declaration inside a code block , Used to modify the storage type of variables , Change from automatic variable to static variable , But the link properties and scope are not affected .
2. Function can return local variables , But you cannot return the address of a local variable ( The pointer )
The scope of the local variable is inside the function , When the function returns , Local variable memory has been freed . therefore , If the function returns the value of a local variable , Address not involved , The program doesn't go wrong . So the function can return local variables . But if the function returns the address of a local variable ( The pointer ) Words , Error will be reported when the program is running . Because it's just a pointer ( Address ) After copying, it returns , And the memory pointed by the pointer ( Stored content ) Has been released , In this way, the pointer has no permission to access the memory , An error will be reported after calling . To be exact , A function cannot return a pointer to stack memory by ! Note that this refers to the stack , It is possible to return a pointer to heap memory , Because the contents of heap memory are not released until the end of the whole program .
边栏推荐
- i3-status 配置
- 微信小程序 27 进度条的动态实现和搜索框、热搜榜的静态搭建
- 高并发下如何保证数据库和缓存双写一致性?
- 平衡二叉树
- Wechat campus maintenance application applet graduation design finished product of applet completion work (3) background function
- 网上商城系统MySql数据库设计项目实战
- 阿姆利塔工程学院|用于情感分析的方面术语提取中优化采样的强化主动学习方法
- Introduction to web security ICMP testing and defense
- Clip can also do segmentation tasks? The University of Gottingen proposed a model clipseg that uses text and image prompt and can do three segmentation tasks at the same time, squeezing out the clip a
- How to be a self disciplined person?
猜你喜欢

伺服驱动器在机器人上的研究与应用

虹科分享|如何解决勒索软件安全漏洞

Based on easycv to reproduce Detr and dab-detr, the correct opening method of object query

小程序毕设作品之微信校园维修报修小程序毕业设计成品(4)开题报告
平衡二叉树

modelsim和quartus联合仿真PLL FIFO等IP核

小程序毕设作品之微信校园维修报修小程序毕业设计成品(3)后台功能

Improvement of wechat applet 29 hot search list ②

小程序毕设作品之微信校园维修报修小程序毕业设计成品(5)任务书

Solve the problem that the win10 account has no administrator rights
随机推荐
JS learning notes 16: switching pictures small project practice
[applet development] do you know about applet development?
Juzhi cloud computing opens a new era to the "proprietary cloud" of Youfu network
某公司网络设计与规划
鸿蒙-大喵计算画板-简介
Selenium运行慢 - 通过设置selenium加载策略加快运行速度
[Detr for 3D object detection] detr3d: 3D object detection from multi view images via 3D-to-2D queries
JS basic type reference type deep / shallow clone copy
modelsim和quartus联合仿真PLL FIFO等IP核
Wechat campus maintenance application applet graduation design finished product of applet completion work (8) graduation design thesis template
高并发下如何保证数据库和缓存双写一致性?
Scala基础【集合01】
GBASE 8s UDR内存管理_01_mi_alloc
Eve - 0day Threat Intelligence
小程序毕设作品之微信校园维修报修小程序毕业设计成品(6)开题答辩PPT
Empire CMS whole station | mobile number /qq lianghao mall source code | suitable for mobile terminal
ERROR: role “admin“ cannot be dropped because some objects depend on itDETAIL:
解决Win10账户没有了管理员权限
Pymoo learning (6): termination conditions
CRM configuration item command notes