PAT-1133 Splitting A Linked List(链表分解)
Cutting an integer means to cut a K digits long integer Z into two integers of (K/2) digits long integers A and B. For example, after cutting Z = 167334, we have A = 167 and B = 334. It is interesting to see that Z can be devided
by the product of A and B, as 167334 / (167 x 334) = 3. Given an integer Z, you are supposed to test if it is such an integer.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<= 20). Then N lines follow, each gives an integer Z (10<=Z<=231). It is guaranteed that the number of digits of Z is an even number.
Output Specification:
For each case, print a single line "Yes" if it is such a number, or "No" if not.
Sample Input:
3
167334
2333
12345678
Sample Output:
Yes
No
No
题目大意:这道题形式与之前的乙级题反转链表很相似,给出一条链表以及一个整数k,要求将链表节点分为三部分输出:值小于0,值大于等于0小于等于k,值大于k,并且每部分节点的相对顺序不改变。
主要思路:先定义代表节点的结构体,包含地址(add),值(val),下个地址(next),获取输入并将每个节点存在其地址作为索引的数组中,然后从首节点开始遍历整个链表,如果节点值<0,则直接输出;如果<=k,则存入容器vec1中;如果>k,则存入容器vec2中,然后再依次遍历输出vec1和vec2中的节点。输出的时候注意,在第一次输出的时候只需要输出当前地址和节点值,其余时候需要输出两次地址(前一次作为上一个节点的next)和一次值。
#include <cstdio>
#include <vector>
using namespace std;
typedef struct{
int add;
int val;
int next;
}Node;
Node node[100000];
vector<Node> vec1, vec2;
int main(void) {
int first, n, k, i;
Node x;
scanf("%d%d%d", &first, &n, &k);
for (i = 0; i < n; i++) {
scanf("%d%d%d", &x.add, &x.val, &x.next);
node[x.add] = x; //节点放入其地址作为索引的数组中
}
//输出小于 0 的部分
bool is_first = true;
for (i = first; i != -1; i = node[i].next) {
if (node[i].val < 0) {
if (is_first) {
printf("%05d %d ", node[i].add, node[i].val);
is_first = false;
}
else
printf("%05d\n%05d %d ", node[i].add, node[i].add, node[i].val);
}
else if (node[i].val <= k)
vec1.push_back(node[i]);
else
vec2.push_back(node[i]);
}
//输出 [0, k] 的部分
for (i = 0; i < vec1.size(); i++) {
if (is_first) {
printf("05d %d ", vec1[i].add, vec1[i].val);
is_first = false;
}
else
printf("%05d\n%05d %d ", vec1[i].add, vec1[i].add, vec1[i].val);
}
//输出大于 k 的部分
for (i = 0; i < vec2.size(); i++) {
if (is_first) {
printf("%05d %d ", vec2[i].add, vec2[i].val);
is_first = false;
}
else
printf("%05d\n%05d %d ", vec2[i].add, vec2[i].add, vec2[i].val);
}
printf("-1\n");
return 0;
}
PAT-1133 Splitting A Linked List(链表分解)的更多相关文章
- PAT 1133 Splitting A Linked List[链表][简单]
1133 Splitting A Linked List(25 分) Given a singly linked list, you are supposed to rearrange its ele ...
- PAT 1133 Splitting A Linked List
Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...
- PAT A1133 Splitting A Linked List (25 分)——链表
Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...
- PAT A1133 Splitting A Linked List (25) [链表]
题目 Given a singly linked list, you are supposed to rearrange its elements so that all the negative v ...
- 1133 Splitting A Linked List
题意:把链表按规则调整,使小于0的先输出,然后输出键值在[0,k]的,最后输出键值大于k的. 思路:利用vector<Node> v,v1,v2,v3.遍历链表,把小于0的push到v1中 ...
- PAT1133:Splitting A Linked List
1133. Splitting A Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT_A1133#Splitting A Linked List
Source: PAT A1133 Splitting A Linked List (25 分) Description: Given a singly linked list, you are su ...
- PAT-1133(Splitting A Linked List)vector的应用+链表+思维
Splitting A Linked List PAT-1133 本题一开始我是完全按照构建链表的数据结构来模拟的,后来发现可以完全使用两个vector来解决 一个重要的性质就是位置是相对不变的. # ...
- PAT 1074 Reversing Linked List[链表][一般]
1074 Reversing Linked List (25)(25 分) Given a constant K and a singly linked list L, you are suppose ...
随机推荐
- OpenWrt-19.07.2 For HC5861(极路由3) /HiWiFi/Gee最新固件,极路由3刷openwrt
OpenWrt For HiWiFi(HC5861) 自编译精减固件,极路由3自用固件 HC5861-uboot.bin v19.07.2 下载 支持 NTFS 读写 支持 Wi-Fi 5G 驱动 默 ...
- Java高效开发IntelliJ IDEA 2019.1 新特性
1. 重构类.文件.符号,Action 搜索 IntelliJ IDEA(以下简称 IDEA) 中的搜索可以分为以下几类 类搜索,比如 Java,Groovy,Scala 等类文件 文件搜索,类文件之 ...
- Android 项目 Android 学习手册(一)
前言: 当每次查询android 知识的时候,内心是凌乱的,总觉得要是有一个工具多好, 尤其在手机端如何可以查询的话,会非常完美,能大大减少选择查询的时间, 之前见了很多java 学习手册,把一些重要 ...
- 内蒙古特检院利用物联网/RFID技术提高电梯检测水平
随着电梯检验工作信息化进程的进一步深入,内蒙古特检院从检验工作中寻找新方法.新手段,为检验员新引入电梯检验手持终端设备,力求提高电梯检验水平,将"电梯安全惠民工程"落到实处. 电梯 ...
- SaltStack漏洞导致的挖矿排查思路
描述 SaltStack是一套C/S架构的运维工具,服务端口默认为4505/4506,两个端口如果对外网开放危害非常大,黑客利用SaltStack的远程命令执行漏洞CVE-2020-11651可以直接 ...
- golang之array
golang使用array表示固定大小的数组,使用slice表示动态数组. package main import "fmt" func main() { var a = [5]i ...
- ArcMap 0 (ArcGIS10.2安装)
一如GIS深似海,从此相逢是故人(这句话适合初步接触GIS的,我算是初窥门径.还是道行太浅,只是多了感慨) 前言: 1. 本人GIS专业,对于ArcGIS较为熟悉.由于专业和其它经历需要,接触过不少各 ...
- C. Barcode dp
https://codeforces.com/problemset/problem/225/C 这个题目和之前一个题目很像 https://www.cnblogs.com/EchoZQN/p/1090 ...
- 使用 if elseif else 指定条件
nrows = 4; ncols = 6; A = ones(nrows,ncols); 遍历矩阵并为每个元素指定一个新值.对主对角线赋值 2,对相邻对角线赋值 -1,对其他位置赋值 0. for c ...
- 【Spark】帮你搞明白怎么通过SparkSQL整合Hive
文章目录 一.创建maven工程,导包 二.开发代码 一.创建maven工程,导包 <properties> <scala.version>2.11.8</scala.v ...