0
点赞
收藏
分享

微信扫一扫

西南交通大学840数据结构编程大题-2018年

胡桑_b06e 2022-03-27 阅读 61

文章目录


四、算法及程疗设计题(20 分,共2小题,每小题》客在试卷上的内容无
效)

第一题

1.算法设计:
二叉树结点指针类型定义为

typedef struct node { 
    int data; 
    struct node *lchild, *rchild; 
} BT;

编写一个算法函数,形参传入二叉树根结点地址root,函效值返回二叉树所有结点的data域值之和。

#include <stdio.h>
typedef struct node {
    int data;
    struct node *lchild, *rchild;
} BT;
int getSum(BT * root){
    if (root==NULL) return 0;
    return root->data + getSum(root->lchild) + getSum(root->rchild);
}

第二题

程序设计:
要求计算任意一天在本年中为第几天(每年的1月1日为该年的第1天) ? (注意考虑闰年问题:能被4整除但不能被100整除,或能被400整除的年份即为闰年)
(1)要求写一个函数Days, 实现上面的计算。
(2)定义一个日期结构体DAY (包括年、月、日),保存日期信息
(3)要求日期由用户输入,将其保存为DAY结构体的一个变量,由主函数将该变量传递给Days函数
(4)计算结果在主函数中进行输出。

#include <stdio.h>
typedef struct {
    int day, year, month;
} DAY;

int is_runlian(int year) {
    // 是否为闰年
    return (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
}

int Days(DAY cur) {
    int months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if (is_runlian(cur.year)) months[2]++;
    int sum = 0;
    for (int i = 0; i < cur.month; i++)
        sum += months[i];
    sum += cur.day;
    return sum;
}

int main() {
    DAY cur;
    printf("please input year month day, use space split\n:");
    scanf("%d%d%d", &cur.year, &cur.month, &cur.day);
    printf("%d", Days(cur));
    return 0;
}
举报

相关推荐

0 条评论