【C/PTA —— 15.结构体2(课外实践)】

2023-12-08 13:15
文章标签 实践 15 结构 pta 课外

本文主要是介绍【C/PTA —— 15.结构体2(课外实践)】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

C/PTA —— 15.结构体2(课外实践)

  • 7-1 一帮一
  • 7-2 考试座位号
  • 7-3 新键表输出
  • 7-4 可怕的素质
  • 7-5 找出同龄者
  • 7-6 排队
  • 7-7 军训

7-1 一帮一

在这里插入图片描述

#include<stdio.h>
#include<string.h>struct student
{int a;char name[20];
};struct student1
{int b;char name1[20];
};int main()
{struct student  s1[50];struct student1 s2[50];struct student1 s3[50];int i, n, j = 0, t = 0, c, d;scanf("%d", &n);for (i = 0; i < n; i++){scanf("%d %s", &s1[i].a, s1[i].name);}for (i = 0; i < n; i++){if (s1[i].a == 1){s2[j].b = i;strcpy(s2[j].name1, s1[i].name);j++;}if (s1[i].a == 0){s3[t].b = i;strcpy(s3[t].name1, s1[i].name);t++;}}c = n / 2 - 1, d = n / 2 - 1;j = 0, t = 0;for (i = 0; i < n / 2; i++){if (s3[j].b < s2[t].b){printf("%s %s\n", s3[j].name1, s2[c].name1);j++;c--;}else{printf("%s %s\n", s2[t].name1, s3[d].name1);t++;d--;}}return 0;
}

7-2 考试座位号

在这里插入图片描述

#include<stdio.h>
struct student
{char num[17];int s;int k;
};int main()
{int n = 0;scanf("%d", &n);struct student stu[1000]={0};for (int i = 0; i < n; i++){scanf("%s %d %d", stu[i].num, &stu[i].s, &stu[i].k);}int m = 0,ret;     scanf("%d", &m);for (int i = 0; i < m; i++){scanf("%d", &ret);int j=0;for (j = 0; j < n; j++){if (ret == stu[j].s){printf("%s %d\n", stu[j].num, stu[j].k);}}}return 0;
}

7-3 新键表输出

在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>// 定义链表节点结构体
typedef struct ListNode {int val;struct ListNode* next;
} ListNode;// 定义头节点指针
ListNode* createList() {ListNode* head = NULL;int num;while (1) {scanf("%d", &num);if (num == -1) {break;}ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));newNode->val = num;newNode->next = NULL;if (head == NULL) {head = newNode;} else {ListNode* cur = head;while (cur->next != NULL) {cur = cur->next;}cur->next = newNode;}}return head;
}// 遍历链表,将奇数值节点插入新链表
ListNode* createNewList(ListNode* head) {ListNode* newHead = NULL;ListNode* cur = head;while (cur != NULL) {if (cur->val % 2 != 0) {ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));newNode->val = cur->val;newNode->next = NULL;if (newHead == NULL) {newHead = newNode;} else {ListNode* temp = newHead;while (temp->next != NULL) {temp = temp->next;}temp->next = newNode;}}cur = cur->next;}return newHead;
}// 打印链表
void printList(ListNode* head) {ListNode* cur = head;printf("%d", cur->val);cur = cur->next;while (cur != NULL) {printf(" %d", cur->val);cur = cur->next;}printf("\n");
}int main() {// 创建链表ListNode* head = createList();// 创建新链表ListNode* newHead = createNewList(head);// 打印新链表printList(newHead);return 0;
}

7-4 可怕的素质

在这里插入图片描述

#include <stdio.h>
#include<stdlib.h>
typedef struct student student;
struct student{int ret;struct student *next;
};
student *insert(int n);
void prin(student*,int n);
int main(){int n;student *stu1;scanf("%d", &n);stu1=insert(n);prin(stu1, n);return 0;
}
void prin(student*stu1,int n){student *p = stu1->next;while(p!=NULL){if(p->next!=NULL)printf("%d ", p->ret);elseprintf("%d", p->ret);p = p->next;}
}
student *insert(int n){student *head;head = (struct student*) malloc(sizeof(student));head->next = NULL;student *p = head, *q;int pos = 0;for (int i = 1; i <= n;i++){scanf("%d", &pos);q = (struct student *)malloc(sizeof(student));q->ret = i;q->next = NULL;if(i==1){head->next = q;}else if(pos==0){q->next = head->next;head->next = q;}else if(pos!=0){p = head->next;while(p->ret!=pos){p = p->next;}q->next = p->next;p->next = q;}}return head;
}

7-5 找出同龄者

在这里插入图片描述

#include<stdio.h>
typedef struct student
{char name[10];int age;
}student;
int main()
{int n = 0;student stu[100];scanf("%d", &n);for (int i = 0; i < n; i++){scanf("%s %d", stu[i].name, &stu[i].age);}int m = 0;scanf("%d", &m);int j = 0;for (int i = 0; i < n; i++){if (stu[i].age != m){printf("%s", stu[i].name);j = i;break;}}for (int i = j + 1; i < n; i++){if (stu[i].age != m){printf(" %s", stu[i].name);}}return 0;
}

7-6 排队

在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>
typedef struct student student;
//还是双链表好用
struct student{student *prior;student *next;int ri, bi, fi,ret;double height;student *arret;
};
student* create(int n){student *head, *p, *q;head = (student*)malloc(sizeof(student));head->prior = NULL;head->next = NULL;head->arret = NULL;for (int i = 1; i <= n;i++){q = (student*)malloc(sizeof(student));scanf("%lf", &q->height);q->ret = i;if(i==1){head->next = q;head->arret = q;q->arret = NULL;q->prior = head;q->next = NULL;}else {p->next = q;p->arret = q;q->arret = NULL;q->prior = p;q->next = NULL;}p = q;}return head;
}
void swap(student *p1,student *p2){student *p1f, *p1b, *p2f, *p2b;p1f = p1->prior;p1b = p1->next;p2f = p2->prior;p2b = p2->next;p1f->next = p1b;p1b->prior = p1f;p1b->next = p2f;p2f->prior = p1b;p2f->next = p2b;if(p2b!=NULL)p2b->prior = p2f;
}
int main(){int n;scanf("%d", &n);student *stu1;stu1=create(n);student *p = stu1->next;for (int i = 1; i < n;i++){p = stu1->next;for (int j = 1; j < n - i + 1;j++){if(p->height<p->next->height){swap(p,p->next);}else p = p->next;}}p = stu1->next;for (int i = 1; i <= n;i++){if(i==1){p->fi = 0;p->bi = p->next->ret;}else if(i==n){p->bi = 0;p->fi = p->prior->ret;}else {p->fi = p->prior->ret;p->bi = p->next->ret;}p->ri = i;p = p->next;}p = stu1->arret;for (int i = 1; i <= n;i++){printf("%d %d %d\n", p->ri, p->fi, p->bi);p = p->arret;}return 0;
}

7-7 军训

在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>
typedef struct queue queue;
struct queue{int rank;queue *prior;queue *next;int size;
};
void print(queue *queue1){queue *p = queue1->next;while(p!=NULL){if(p->next!=NULL)printf("%d ", p->rank);elseprintf("%d\n", p->rank);p = p->next;}
}
queue *delete_LB(queue *queue1){queue *k = queue1;queue1->prior->next = queue1->next;if(queue1->next!=NULL)queue1->next->prior = queue1->prior;queue1 = queue1->prior;free(k);return queue1;
}
queue *create(int n){queue *head;queue *p, *q;head = (queue*)malloc(sizeof(queue));head->prior = NULL;head->next = NULL;for (int i = 1; i <= n;i++){q = (queue *)malloc(sizeof(queue));q->rank = i;if(i==1){head->next = q;q->prior = head;q->next = NULL;}else {p->next = q;q->prior = p;q->next = NULL;}p = q;}return head;
}
int main(){int n;scanf("%d", &n);queue document[105];queue *a;for (int i = 1; i <= n;i++){int count;scanf("%d", &count);a = create(count);a->size = count;queue *p;while(a->size>3){p = a->next;for (int j = 1; j <= count; j++){if (j % 2 == 0)//这里无需判断是否size>3,因为无论是否满足,都必须在进行的一轮内将所有2的报数删除;{p=delete_LB(p);a->size--;}p = p->next;}count = a->size;p = a->next;if(a->size>3)//这里加上size>3的判断才能保证n=40的情况下37不会被删除,否则还会进行一次j=3时的删除操作;特殊情况(即处理完上一轮2的报数后size恰好为3,但是此时没有加入判断的话循环会继续运行,会多删除1项){for (int j = 1; j <= count; j++){if (j % 3 == 0) {p = delete_LB(p);a->size--;}p = p->next;}}count = a->size;}document[i] = *a;}for (int i = 1; i <= n;i++){print(&document[i]);}return 0;
}

这篇关于【C/PTA —— 15.结构体2(课外实践)】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/469975

相关文章

Python 中的 with open文件操作的最佳实践

《Python中的withopen文件操作的最佳实践》在Python中,withopen()提供了一个简洁而安全的方式来处理文件操作,它不仅能确保文件在操作完成后自动关闭,还能处理文件操作中的异... 目录什么是 with open()?为什么使用 with open()?使用 with open() 进行

Spring Boot 配置文件之类型、加载顺序与最佳实践记录

《SpringBoot配置文件之类型、加载顺序与最佳实践记录》SpringBoot的配置文件是灵活且强大的工具,通过合理的配置管理,可以让应用开发和部署更加高效,无论是简单的属性配置,还是复杂... 目录Spring Boot 配置文件详解一、Spring Boot 配置文件类型1.1 applicatio

tomcat多实例部署的项目实践

《tomcat多实例部署的项目实践》Tomcat多实例是指在一台设备上运行多个Tomcat服务,这些Tomcat相互独立,本文主要介绍了tomcat多实例部署的项目实践,具有一定的参考价值,感兴趣的可... 目录1.创建项目目录,测试文China编程件2js.创建实例的安装目录3.准备实例的配置文件4.编辑实例的

Python 中的异步与同步深度解析(实践记录)

《Python中的异步与同步深度解析(实践记录)》在Python编程世界里,异步和同步的概念是理解程序执行流程和性能优化的关键,这篇文章将带你深入了解它们的差异,以及阻塞和非阻塞的特性,同时通过实际... 目录python中的异步与同步:深度解析与实践异步与同步的定义异步同步阻塞与非阻塞的概念阻塞非阻塞同步

Python Dash框架在数据可视化仪表板中的应用与实践记录

《PythonDash框架在数据可视化仪表板中的应用与实践记录》Python的PlotlyDash库提供了一种简便且强大的方式来构建和展示互动式数据仪表板,本篇文章将深入探讨如何使用Dash设计一... 目录python Dash框架在数据可视化仪表板中的应用与实践1. 什么是Plotly Dash?1.1

使用Java实现通用树形结构构建工具类

《使用Java实现通用树形结构构建工具类》这篇文章主要为大家详细介绍了如何使用Java实现通用树形结构构建工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录完整代码一、设计思想与核心功能二、核心实现原理1. 数据结构准备阶段2. 循环依赖检测算法3. 树形结构构建4. 搜索子

利用Python开发Markdown表格结构转换为Excel工具

《利用Python开发Markdown表格结构转换为Excel工具》在数据管理和文档编写过程中,我们经常使用Markdown来记录表格数据,但它没有Excel使用方便,所以本文将使用Python编写一... 目录1.完整代码2. 项目概述3. 代码解析3.1 依赖库3.2 GUI 设计3.3 解析 Mark

springboot集成Deepseek4j的项目实践

《springboot集成Deepseek4j的项目实践》本文主要介绍了springboot集成Deepseek4j的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录Deepseek4j快速开始Maven 依js赖基础配置基础使用示例1. 流式返回示例2. 进阶

Android App安装列表获取方法(实践方案)

《AndroidApp安装列表获取方法(实践方案)》文章介绍了Android11及以上版本获取应用列表的方案调整,包括权限配置、白名单配置和action配置三种方式,并提供了相应的Java和Kotl... 目录前言实现方案         方案概述一、 androidManifest 三种配置方式

Spring Boot中定时任务Cron表达式的终极指南最佳实践记录

《SpringBoot中定时任务Cron表达式的终极指南最佳实践记录》本文详细介绍了SpringBoot中定时任务的实现方法,特别是Cron表达式的使用技巧和高级用法,从基础语法到复杂场景,从快速启... 目录一、Cron表达式基础1.1 Cron表达式结构1.2 核心语法规则二、Spring Boot中定