7-2 Rank a Linked List
分析:
题意比较清楚简单,创建一个链表结构,记录结点的下一个结点和位置即可。

A linked list of n nodes is stored in an array of n elements. Each element contains an integer data and a next pointer which is the array index of the next node. It is guaranteed that the given list is linear – that is, every node, except the first one, has a unique previous node; and every node, except the last one, has a unique next node.

Now let’s number these nodes in order, starting from the first node, by numbers from 1 to n. These numbers are called the ranks of the nodes. Your job is to list their ranks.

Input Specification:
Each input file contains one test case. For each case, the first line gives a positive number n (≤105) which is the total number of nodes in the linked list. Then n numbers follow in the next line. The ith number (i=0,⋯,n−1) corresponds to next(i) of the ith element. The NULL pointer is represented by −1. The numbers in a line are separated by spaces.

Output Specification:
List n ranks in a line, where the ith number (i=0,⋯,n−1) corresponds to rank(i) of the ith element. The adjacent numbers in a line must be separated by exactly 1 space, and there must be no extra space at the beginning or the end of the line.

Sample Input:
5
3 -1 4 1 0
Sample Output:
3 5 1 4 2
Hint:
The given linked list is stored as 2->4->0->3->1->NULL. Hence the 0th element is ranked 3 since it is the 3rd node in the list; the 1st element is ranked 5 since it is the last (the 5th) node in the list; and so on so forth.
代码

#include<stdio.h>const int maxn = 100010;
struct Node{int next;int pos;
}node[maxn];int main(){int n, head, temp[maxn];bool vis[maxn] = {false};scanf("%d", &n);for(int i = 0; i < n; i++){scanf("%d", &node[i].next);if(node[i].next != -1){vis[node[i].next] = true;}temp[i] = node[i].next;}for(int i = 0; i < n; i++){if(vis[i] == false){head = i;break;}}int newnode = head;int count = 0;while(node[newnode].next != -1){node[newnode].pos = count++;newnode = node[newnode].next;}node[newnode].pos = count++;for(int i = 0; i < n; i++){if(temp[i] != -1){printf("%d", node[temp[i]].pos);}else{printf("%d", n);}if(i < n - 1){printf(" ");}}}

PAT(甲级)2021年冬季考试 7-2 Rank a Linked List相关推荐

  1. 【PAT】2021年冬季考试甲级,摸鱼游记、92分

    T1,简单模拟,20/20分 #include<bits/stdc++.h> using namespace std; const int maxn = 1e5+10; int a[max ...

  2. 记PAT 甲级 2021年 秋季考试 经验总结、线上考试细节、考场题解

    1.简单回顾 今天下午考完了秋季的考试,最终得了97分,第一题扣了3分,非常遗憾.还有一个多小时的时候就已经97了,回来和第一题的剩余3分周旋到最后,没能发现问题所在,不停考虑各种,我觉得可能是陷在已 ...

  3. 【PAT甲级】2020冬季 PAT 甲级

    2020冬季 PAT 甲级记录 第一次参加PAT,本来九月份报名的时候是打算到十二月份的时候把乙级的题库刷完,然后甲级的题库刷一半,结果因为各种各样的事情(主要是懒又没坚持0.0)这次直到考前乙级才刷 ...

  4. PAT甲级 2019年冬季 题解

    题目1:7-1 Good in C (20分) When your interviewer asks you to write "Hello World" using C, can ...

  5. PAT甲级线上考试备考

    我本人刷了两遍题库 之前考了两次都是四十几分,我认为目前甲级题库已经过时了 (对于有算法基础的同学来说) 前100题没有做的必要 除非是典型算法. DFS 和 回溯是相当重要的考点,如果有时间的话一定 ...

  6. 2021.9.11周六PAT甲级考试复盘与总结

    周六PAT甲级考试复盘与总结 先说结论:仍未步入"高手"行列:现在的学习节奏与方法是对的,有十万分的必要坚持下去. 题目 知识点 分数 T1 前缀和.二分 11 / 20 T2 排 ...

  7. 2021年春季PAT甲级考试

    作为一个绩点不高.牌子不硬的退役ACMer,去年冬天在nvpy的鼓励下,决定先试试PAT为考研做准备,于是认认真真把所有20分的题目过了一遍.放寒假以后,就开始练习所有25分的题目,并且每道题做完都总 ...

  8. pat甲级考试报名费_PAT(甲级)2019年冬季考试 题解

    总结写在前头: 考了字符串.链表(伪链表).图.树.考点比较均衡. 本次考试难度还行,需要较扎实的数据结构底子,对于字符串处理的技巧有一定的要求. 说句题外话,字符串是个重点,主要因为很多人会忽视字符 ...

  9. PAT学习资料汇总(PAT甲级、PAT顶级、PAT考试经验)

    二.PAT甲级 PAT甲级真题目录(按题型整理) PAT甲级真题目录(按题型整理)_love music.的博客-CSDN博客_pat甲级真题 PAT甲[所有题目+解析+代码示例+总结]附带所有历年整 ...

  10. 2021年冬季PAT乙级题解(C/C++语言)

    2021年冬季PAT乙级题解(C/C++语言) 7-1 自动打包机 (15 分) 原题 算法标签 模拟 代码 #include<bits/stdc++.h> #define int lon ...

最新文章

  1. TCP和UDP的最完整的区别
  2. Android Studio中架包打包和依赖冲突解决
  3. salt-api timeout 执行超时问题解决
  4. 使用JSON.parse(),JSON.stringify()实现对对象的深拷贝
  5. Beta冲刺博客集合贴
  6. [SceneKit专题]11-Reference-Nodes引用节点
  7. 设计模式-责任链模型
  8. 一文读懂 Serverless,将配置化思想复用到平台系统中
  9. 深入理解Java虚拟机——类加载机制
  10. matlab循环遍历数组_MatLab简易教程 #8 循环
  11. CDOJ 1805 矩阵 数学
  12. 职业高中计算机专业目标,职业高中计算机专业开设的必要性
  13. X 射线成像 新型数字技术 —— CMOS 探测器
  14. 《Android音视频开发》— Android 书籍
  15. 加权最小二乘(wls)滤波算法原理及实现
  16. 磁盘不见了只剩一个c盘_非常的奇葩,终于解决了硬盘从盘盘符消失的问题
  17. vim命令失效了怎么办
  18. dbv连oracle,oracle工具:DBV的用法
  19. 华硕服务器 u盘安装系统,华硕电脑u盘安装系统教程
  20. java输入小写字母_java中怎么实现从对话框输入一个大写字母将其转化为小写字母输出?...

热门文章

  1. Web移动端混合开发--IonicFramework
  2. 分享5个爬虫专业博客网站
  3. 华为云Centos7搭建hadoop集群一:云服务器准备
  4. 有赞搜索系统的技术内幕
  5. 色彩空间(一):色彩空间基础
  6. Spark快速大数据分析——Spark安装与IDEA 开发(贰)
  7. Spring Boot使用WebSocket实现群聊
  8. WSN基于自适应网格的多目标定位算法
  9. linux 服务器远程开机,Linux 下实现远程开机
  10. [转载]全国高校IPv6地址分配情况