【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;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/257620.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[⑧ADRV902x]: Digital Pre-Distortion (DPD)学习笔记

前言 DPD 数字预失真技术&#xff0c;是一种用于抑制功率放大器非线性失真的方法。 它通过在信号输入功率放大器&#xff08;PA&#xff09;之前插入一个预失真模块&#xff0c;对输入信号进行适当的调制&#xff0c;以抵消功率放大器引起的非线性失真&#xff0c;使功率放大器…

JAVA常用队列

阻塞队列介绍 Queue接口 public interface Queue<E> extends Collection<E> { //添加一个元素&#xff0c;添加成功返回true, 如果队列满了&#xff0c;就会抛出异常 boolean add(E e); //添加一个元素&#xff0c;添加成功返回true, 如果队列满了&#xff0c;返回…

主动而非被动:确保网络安全运营弹性的途径

金融部门处理威胁的经验对网络安全领域的任何人都有启发——没有什么可以替代提前摆脱潜在的风险和问题。 从狂野西部的银行劫匪到勒索软件即服务 (RaaS)&#xff0c;全球金融生态系统面临的威胁多年来发生了巨大变化。技术进步带动了金融业的快速发展&#xff0c;从现金交易到…

相控阵天线(十四):常规大阵列天线分布(椭圆阵列、三角阵列、矩形拼接阵列、栅格拼接阵列)

目录 简介椭圆阵列三角阵列子阵拼接的矩形阵列子阵拼接的圆形阵列圆形子阵拼接阵列子阵栅格拼接阵列 简介 前面的博客已经介绍过常见的平面阵有一些基本类型&#xff0c;本篇博客介绍一些实际工程中可能出现的阵列&#xff0c;包括椭圆阵列、子阵通过矩形拼接形成的矩形大阵列…

QT Windos平台下打包应用程序

一、windeployqt.exe windeployqt&#xff1a;是 Qt 框架自带的一个工具&#xff0c;用于将一个 Qt 应用程序在 Windows 操作系统下进行打包。它可以通过扫描应用程序的依赖项获取所需的 Qt 库文件、插件和翻译文件&#xff0c;以及复制应用程序可执行文件和所需的依赖项到指定…

CTF刷题记录

刷题 我的md5脏了KFC疯狂星期四坤坤的csgo邀请simplePHPcurl 我的md5脏了 g0at无意间发现了被打乱的flag&#xff1a;I{i?8Sms??Cd_1?T51??F_1?} 但是好像缺了不少东西&#xff0c;flag的md5值已经通过py交易得到了&#xff1a;88875458bdd87af5dd2e3c750e534741 flag…

公众号提高数量

一般可以申请多少个公众号&#xff1f;目前公众号申请数量的规定是从2018年底开始实施的&#xff0c;至今没有变化。规定如下&#xff1a;1、个人可以申请1个个人主体的公众号&#xff1b;2、企业&#xff08;有限公司&#xff09;可以申请2个公众号&#xff1b;3、个体户可以申…

uView框架的安装与Git管理

参考链接&#xff1a;Http请求 | uView - 多平台快速开发的UI框架 - uni-app UI框架 安装 打开我们项目的cmd进行下载&#xff1a; yarn add uview-ui 首先我们要确定&#xff0c;未下载前的文件目录以及下载后&#xff0c;是多了个文件目录node_modules 下载完成之后我们就…

Leetcode—219.存在重复元素II【简单】

2023每日刷题&#xff08;五十三&#xff09; Leetcode—219.存在重复元素II 实现代码 class Solution { public:bool containsNearbyDuplicate(vector<int>& nums, int k) {unordered_map<int, int> m;int n nums.size();for(int i 0; i < n; i) {if(m…

【无线网络技术】——无线个域网(学习笔记)

&#x1f4d6; 前言&#xff1a;手机、PC机、电视等消费类产品非常普及&#xff0c;人们希望有一种短距离、低成本、小功耗的无线通信方式&#xff0c;实现不同功能单一设备的互联&#xff0c;提供小范围内设备的自组网机制&#xff0c;并通过一定的安全接口完成自组小网与广域…

AI自动生成代码工具

AI自动生成代码工具是一种利用人工智能技术来辅助或自动化软件开发过程中的编码任务的工具。这些工具使用机器学习和自然语言处理等技术&#xff0c;根据开发者的需求生成相应的源代码。以下是一些常见的AI自动生成代码工具&#xff0c;希望对大家有所帮助。北京木奇移动技术有…

Microsoft 365 Copilot正式上线,如何稳定访问体验?

如果将微软对人工智能的投资看成一场豪赌&#xff0c;Microsoft Copilot无疑是现阶段最受瞩目的赌注。2023年9月正式发布的Microsoft Copilot是一种基于大型语言模型&#xff08;LLM&#xff09;和微软图形&#xff08;Microsoft Graph&#xff09;的数据和人工智能&#xff08…