循环链表,链表,双向链表

1748:约瑟夫问题

总时间限制: 
1000ms
内存限制: 
65536kB
描述
约瑟夫问题:有n只猴子,按顺时针方向围成一圈选大王(编号从1到n),从第1号开始报数,一直数到m,数到m的猴子退出圈外,剩下的猴子再接着从1开始报数。就这样,直到圈内只剩下一只猴子时,这个猴子就是猴王,编程求输入n,m后,输出最后猴王的编号。

输入
每行是用空格分开的两个整数,第一个是 n, 第二个是 m ( 0 < m,n <=300)。最后一行是:

0 0

输出
对于每行输入数据(最后一行除外),输出数据也是一行,即最后猴王的编号
样例输入
6 2
12 4
8 3
0 0
样例输出
5
1
7
#include<iostream>
using namespace std; struct listnode { int num; listnode *next; }; listnode *head, *tail; void make_circle(int n) { head = new listnode; tail = head; for (int i = 1; i < n; i ++) { tail -> num = i; tail -> next = new listnode; tail = tail -> next; } tail -> num = n; tail -> next = head; } void process(int m) { listnode *ptr = head, *follow = tail; while (head != tail) { int times = m - 1; while (times --) { ptr = ptr -> next; follow = follow -> next; } follow -> next = ptr -> next; if (ptr == head) head = head -> next; if (ptr == tail) tail = follow; ptr = follow -> next; } cout << head -> num << endl; delete head; } int main() { int n, m; while (cin >> n >> m && (n != 0 || m != 0)) { make_circle(n); process(m); } }



6378:删除数组中的元素(链表)

总时间限制: 
1000ms
内存限制: 
65536kB
描述

给定N个整数,将这些整数中与M相等的删除 
假定给出的整数序列为:1,3,3,0,-3,5,6,8,3,10,22,-1,3,5,11,20,100,3,9,3 
应该将其放在一个链表中,链表长度为20 
要删除的数是3,删除以后,链表中只剩14个元素:1 0 -3 5 6 8 10 22 -1 5 11 20 100 9

要求:必须使用链表,不允许使用数组,也不允许不删除元素直接输出 
      程序中必须有链表的相关操作:建立链表,删除元素,输出删除后链表中元素,释放链表 
      不符合要求的程序即使通过,也会算作0分 

输入
输入包含3行:
第一行是一个整数n(1 <= n <= 200000),代表数组中元素的个数。
第二行包含n个整数,代表数组中的n个元素。每个整数之间用空格分隔;每个整数的取值在32位有符号整数范围以内。
第三行是一个整数k,代表待删除元素的值(k的取值也在32位有符号整数范围内)。
输出
输出只有1行:
将数组内所有待删除元素删除以后,输出数组内的剩余元素的值,每个整数之间用空格分隔。
样例输入
20
1 3 3 0 -3 5 6 8 3 10 22 -1 3 5 11 20 100 3 9 3
3
样例输出
1 0 -3 5 6 8 10 22 -1 5 11 20 100 9
#include<iostream>
using namespace std; struct listnode { int num; listnode *next; }; listnode *head, *tail; void create_linklist(int n) { head = new listnode; tail = head; for (int i = 0; i < n; i ++) { tail -> next = new listnode; tail = tail -> next; cin >> tail -> num; } tail -> next = NULL; } void delete_linklist() { listnode *tmp = head -> next; head = head -> next; while (head != NULL) { head = head -> next; delete tmp; tmp = head; } } void op(int M) { listnode *ptr = head -> next, *tmp = head; while (ptr != NULL) { if (ptr -> num == M) { if (ptr == tail) tail = tmp;//记得修改尾指针的位置 tmp -> next = ptr -> next; delete ptr; ptr = tmp -> next; } else { tmp = ptr; ptr = ptr -> next; } } } void show_list() { listnode *ptr = head -> next; while (ptr != NULL) { cout << ptr -> num << " "; ptr = ptr -> next; } cout << endl; } int main() { int n; cin >> n; create_linklist(n); int M; cin >> M; op(M); show_list(); delete_linklist(); }


6379:统计学生信息(使用动态链表完成)

总时间限制: 
1000ms
内存限制: 
65536kB
描述

利用动态链表记录从标准输入输入的学生信息(学号、姓名、性别、年龄、得分、地址)

其中,学号长度不超过20, 姓名长度不超过40, 性别长度为1, 地址长度不超过40

输入
包括若干行,每一行都是一个学生的信息,如:
00630018 zhouyan m 20 10.0 28#460
输入的最后以"end"结束
输出
将输入的内容倒序输出
每行一条记录,按照
学号 姓名 性别 年龄 得分 地址
的格式输出
样例输入
00630018 zhouyan m 20 10 28#4600
0063001 zhouyn f 21 100 28#460000
0063008 zhoyan f 20 1000 28#460000
0063018 zhouan m 21 10000 28#4600000
00613018 zhuyan m 20 100 28#4600
00160018 zouyan f 21 100 28#4600
01030018 houyan m 20 10 28#4600
0630018 zuyan m 21 100 28#4600
10630018 zouan m 20 10 28#46000
end
样例输出
10630018 zouan m 20 10 28#46000
0630018 zuyan m 21 100 28#4600
01030018 houyan m 20 10 28#4600
00160018 zouyan f 21 100 28#4600
00613018 zhuyan m 20 100 28#4600
0063018 zhouan m 21 10000 28#4600000
0063008 zhoyan f 20 1000 28#460000
0063001 zhouyn f 21 100 28#460000
00630018 zhouyan m 20 10 28#4600


#include<iostream>
#include<cstring>
using namespace std; struct listnode { char data[200]; listnode *next, *prev; }; listnode *head, *tail; void create_node() { char instruction[200]; head = new listnode; head -> next = new listnode; tail = head -> next; tail -> prev = head; listnode *tmp = head; while (1) { cin.getline(instruction, 200); if (strlen(instruction) == 3 && strcmp(instruction, "end") == 0) break; strcpy(tail -> data, instruction); tail -> next = new listnode; tail -> next -> prev = tail; tail = tail -> next; tmp = tmp -> next; } delete tail; tail = tmp; tail -> next = NULL; } void show_list() { while (tail != head) { cout << tail -> data << endl; tail = tail -> prev; delete tail -> next; } delete tail; } int main() { create_node(); show_list(); }

转载于:https://www.cnblogs.com/StillLife/p/7536879.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值