山东科技大学2020年3月30日有关结构体的应用的两题题解
题目一
Description
在一个二维平面中,一个点的坐标含有横坐标x和纵坐标y两部分,而一个圆可以利用圆心和半径确定。现有如下两个结构体:
typedef struct point
{
int x;//横坐标
int y;//纵坐标
}POINT;
typedef struct circle
{
POINT center;//圆心点的坐标
int radius;//圆的半径
}CIRCLE;
其中,POINT和CIRCLE分别表示平面上的点圆。
需要你来编程判断平面上一个点与矩形之间的位置关系。
完成函数:
int judgeRelation(POINT p,CIRCLE cir);//用于判断点p和圆cir的位置关系。函数的返回值有3种可能:如果点在圆内,返回值为-1;如果点在圆上,返回值为0;如果点在圆外,返回值为1。
注意:在提交代码时,需要将结构体的定义一起提交。
Input
本题中所有的输入都是正整数。
输入分为2行。
第一行两个正整数表示点p的横坐标、纵坐标。
第二行3个正整数中的前2个是圆的圆心的横坐标和纵坐标,第3个是圆的半径。
Output
如果点在圆上,则输出:
The point is on the circle.
如果点在圆内,则输出:
The point is inside the circle.
如果点在圆外,则输出:
The point is outside the circle.
Sample Input
1 10 0 3
Sample Output
The point is inside the circle.
给定的主函数
int main()
{ POINT p;
CIRCLE cir;
int n;
scanf("%d %d",&(p.x),&(p.y));
scanf("%d %d %d",&(cir.center.x),&(cir.center.y),&(cir.radius));
n=judgeRelation(p,cir);
if(n==-1)
printf("The point is inside the circle.");
else if(n==0)
printf("The point is on the circle.");
else
printf("The point is outside the circle.");
return 0;
}
题解
#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstring>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
using namespace std;
struct POINT{
int x, y;
};
struct Struct{
int x, y;
};
struct CIRCLE{
Struct center;
int radius;
};
inline int judgeRelation(POINT p, CIRCLE cir){
if((p.x - cir.center.x) * (p.x - cir.center.x) + (p.y - cir.center.y) * (p.y - cir.center.y) < cir.radius * cir.radius){
return -1;
}
else if((p.x - cir.center.x) * (p.x - cir.center.x) + (p.y - cir.center.y) * (p.y - cir.center.y) == cir.radius * cir.radius){
return 0;
}
else{
return 1;
}
}
int main()
{ POINT p;
CIRCLE cir;
int n;
scanf("%d %d",&(p.x),&(p.y));
scanf("%d %d %d",&(cir.center.x),&(cir.center.y),&(cir.radius));
n=judgeRelation(p,cir);
if(n==-1)
printf("The point is inside the circle.");
else if(n==0)
printf("The point is on the circle.");
else
printf("The point is outside the circle.");
return 0;
}
分析
观察这道题主函数部分分别用了两个结构体,一个包含结构体的循环嵌套。
主函数部分的解读:
主函数定义了两个结构体分别代表这个点和所给出的圆和圆的半径。观察输入部分,这里有一个结构体部分运用了结构体嵌套。还定义了函数judgeRelation函数来判断点和函数的位置关系,由函数的返回值来确定。
所以我们应该构造三个结构体和一个函数,再用两点间的距离公式判断点的位置关系即可,这道题主要的要点就是结构体之间的嵌套。
题目二
Description
输入的日期中哪一天是最晚的。
请根据“Append Code”完成程序。append.c中调用了一个结构体类型struct date和一个函数get_maxdate ()。用C语言或C++编写自定义类型和函数实现,函数的原型为:struct date get_maxdate();
功能:用于输入日期并返回最大值。
函数的调用格式见“Append Code”。
Input
输入若干日期至EOF结束,格式为y-m-d,其中y、m、d是三个正整数表示年月日,均为合法日期,其中y的输入范围是1000~9999年。
Output
输出日期最晚的那一天。
Sample Input
2019-12-31
2020-01-01
2019-12-24
2019-12-30
2020-01-15
Sample Output
2020-01-15
HINT
Append Code
append.c, append.cc,
题解
#include<map>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstring>
#include<iostream>
#include<algorithm>
#define R register
#define LL long long
using namespace std;
int year_max, mounth_max, day_max;
struct Date{
int year;
int mounth;
int day;
inline void getmax(){
while(scanf("%d-%d-%d", &year, &mounth, &day) != EOF){
if(year > year_max || (year == year_max && mounth > mounth_max) || (year == year_max && mounth == mounth_max && day > day_max)){
year_max = year, mounth_max = mounth, day_max = day;
}
}
}
inline void put(){
printf("%d-", year_max);
if(mounth_max < 10){
printf("0%d-", mounth_max);
}
else{
printf("%d-", mounth_max);
}
if(day_max < 10){
printf("0%d", day_max);
}
else{
printf("%d", day_max);
}
}
};
int main()
{
Date d;
d.getmax();
d.put();
return 0;
}
分析
这道题给定的主函数部分非常简单,全部需要在结构体中完成,题目本身很简单但是需要注意的是在结构体中将变量初始化是在C++11的标准下,这个可能会在编译中出问题,可以将变量在全局变量中开辟,即可。