数据结构题目-链表

目录

问题 S: 算法2-25 有序单链表删除重复元素(附加代码模式)

问题 T: 案例3-1.1:线性表元素的区间删除

问题 U: 案例3-1.2:最长连续递增子序列

问题 V: 案例3-1.3:求链表的倒数第m个元素(附加代码模式)

问题 W: 案例2-1.6:两个有序链表序列的合并(附加代码模式)

问题 X: 案例3-1.4:一元多项式的乘法运算

问题 Y: 进阶实验2-3.3:两个有序链表序列的交集(附加代码模式)

问题 Z: 算法2-3~2-6:Big Bang

问题 AA: 算法2-24 单链表反转(附加代码模式)

问题 AB: 约瑟夫问题


问题 S: 算法2-25 有序单链表删除重复元素(附加代码模式)

内存限制:128 MB时间限制:1.000 S

评测方式:文本比较命题人:2011014323

提交:3364解决:1603

返回比赛提交提交记录侧边提交

题目描述

根据一个递增的整数序列构造有序单链表,删除其中的重复元素
本题是附加代码模式,主函数main和打印链表的代码会自动附加在同学们提交的代码后面,请同学们在提交的时候注释附加代码。
附加代码如下:
 

void PrintList(const List &list){

    Node *p = list->next;

    if(p == NULL){

        cout<<"list is empty"<<endl;

    }else{

        while(p != NULL){

            cout << p->data << " ";

            p = p->next;

        }

    }

    cout << endl;

}

int main(){

    int n;

    while(cin >> n){

        if( n ==0) {

            cout<<"list is empty"<<endl;

            continue;

        }

        List list;      

        InitList(list); // 初始化单链表 

        for(int i=0;i<n;i++){

            int v;

            cin >> v;

            AddNode(list,v); // 在单链表的末尾增加节点 

        }

        PrintList(list);  // 输出链表

        RemoveDuplicate(list);  // 删除重复元素 

        PrintList(list);    // 再次输出链表 

        DestroyList(list);  // 销毁单链表 

        

    }

    return 0;

输入

输入包括多组测试数据,每组测试数据占一行,第一个为大于等于0的整数n,表示该单链表的长度,后面跟着n个整数,表示链表的每一个元素。整数之间用空格隔开

输出

针对每组测试数据,输出包括两行,分别是删除前和删除后的链表元素,用空格隔开 如果链表为空,则只输出一行,list is empty

样例输入 复制

5 1 2 3 4 5
5 1 1 2 2 3
0

样例输出 复制

1 2 3 4 5 
1 2 3 4 5 
1 1 2 2 3 
1 2 3 
list is empty

提示

单链表的结构体定义和相应的操作函数如下图所示:

#include <iostream>

using namespace std;

struct Node{

    int data;

    Node* next;

};

typedef Node* List;

int InitList(List &list){

    return 0;

}

int DestroyList(List &list){

    return 0;

}

int AddNode(List &list, int data){

    

    return 0;

}

void RemoveDuplicate(List &list){

}

#include <bits/stdc++.h>
using namespace std;
 
struct Node{
    int data;
    Node* next;
};
 
typedef Node* List;
 
 
int InitList(List &list){
    list = (List)malloc(sizeof(List));
    list->next=NULL;
    return 0;
}
 
int DestroyList(List &list){
    List node;
    while(list->next)
    {
        node=list->next;
        list->next=node->next;
        free(node);
    }
    return 0;
}
 
int AddNode(List &list, int data){
    List node,p;
    InitList(node);
    p=list;
    node->data = data;
    node->next=NULL;
    while(p->next)
    {
        p = p->next;
    }
    p->next=node;
    return 0;
}
 
void RemoveDuplicate(List &list){
    List p = list->next;
    while(p->next)
    {
        if(p->data==p->next->data)
        {
            p->next=p->next->next;
        }
        else
        {
            p=p->next;
        }
         
    }
}
 

问题 T: 案例3-1.1:线性表元素的区间删除

内存限制:128 MB时间限制:1.000 S

评测方式:文本比较命题人:admin

提交:1712解决:764

返回比赛提交提交记录侧边提交

题目描述

 给定一个顺序存储的线性表,删除线性表中所有小于r且大于l的元素。删除后剩余元素保持顺序存储,相对位置不变。

输入

第一行元素个数n、区间l、r。1<=n<=1e6,0<=l<=r<=1e8.
第二行n个整数,对应顺序表的各元素。对于每个元素Ai满足0<=Ai<=1e8

输出

第一行输出删除后元素的个数
第二行输出删除后顺序表中的元素,没有元素直接换行

样例输入 复制

10 0 4
4 -8 2 12 1 5 9 3 3 10

样例输出 复制

6
4 -8 12 5 9 10
#include <bits/stdc++.h>
using namespace std;
int a[1000005];
 
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    int l,r;
    cin>>n>>l>>r;
    for(int i=0;i<n;i++)
    cin>>a[i];
    int count=0;
    for(int i=0;i<n;i++)
    {
        if(a[i]<=l||a[i]>=r)
        ;
        else
        count++;
    }
    cout<<n-count<<endl;
    for(int i=0;i<n;i++)
    {
        if(a[i]<=l||a[i]>=r)
        cout<<a[i]<<" ";
    }
     
}

问题 U: 案例3-1.2:最长连续递增子序列

内存限制:128 MB时间限制:1.000 S

评测方式:文本比较命题人:admin

提交:1045解决:548

返回比赛提交提交记录侧边提交

题目描述

给定一个顺序存储的线性表,设计算法查找该线性表中最长的连续递增子序列。例如(1,9,2,5,7,3,4,6,8,0)中最长的递增子序列为(3,4,6,8).

输入

序列长度 n (1<=n<=1e6)
序列元素(整数)a1,a2.....an (1<=ai<=1e9)

输出

最长连续递增子序列(若有多个等长最长连续递增子序列,输出位置靠前的)。

样例输入 复制

15
1 9 2 5 7 3 4 6 8 0 11 15 17 17 10

样例输出 复制

3 4 6 8
#include <bits/stdc++.h>
using namespace std;
int a[1000005];
int dp[1000005];
 
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        dp[i]=1;
    }
    for(int i=0;i<n;i++)
    {
            if(a[i]>a[i-1])
            dp[i]=max(dp[i],dp[i-1]+1);
    }
    int mymax=0;
    for(int i=0;i<n;i++)
    {
        if(mymax<dp[i])
        mymax=dp[i];
    }
    for(int i=0;i<n;i++)
    {
        if(dp[i]==mymax)
        {
            for(int j=i-mymax+1;j<=i;j++)
            cout<<a[j]<<" ";
            break;
        }
    }
     
}

问题 V: 案例3-1.3:求链表的倒数第m个元素(附加代码模式)

内存限制:128 MB时间限制:1.000 S

评测方式:文本比较命题人:admin

提交:868解决:585

返回比赛提交提交记录侧边提交

题目描述

给你一个链表,链表长度为n,链表内各元素为a1,a2....an,给你一个数m,求链表倒数第m个数是多少。

输入

第一行:链表长度n(1<=n<=1e4)
第二行:n个数字,为链表元素从头到尾依次排列
第三行:数字m

输出

输出倒数第m个元素的值并输出换行

样例输入 复制

5
1 2 3 4 5
5 

样例输出 复制

1
#include <bits/stdc++.h>
using namespace std; 
struct Node{
    int data;
    Node *next;
};
typedef Node* List;
 
int InitList(List& list){
    list = (List)malloc(sizeof(List));
    list->next=NULL;
    return 0;
    return 0;
}
int DestroyList(List& list){
    List node;
    while(list->next)
    {
        node=list->next;
        list->next=node->next;
        free(node);
    }
    return 0;
}
int AddNode(List& list,int v){
    List node,p;
    InitList(node);
    p=list;
    node->data = v;
    node->next=NULL;
    while(p->next)
    {
        p = p->next;
    }
    p->next=node;
    return 0;
}
void PrintList(const List& list){
}
// 查找倒数第m个节点
Node* FindMthNode(const List& list, int m){
     int n=1;
     int cur=0;
     List p = list->next;
     List q = list->next;
     List ans;
     InitList(ans);
     //ans=NULL;
     while(p->next)
     {
         n++;
         p=p->next;
     }
     //cout<<n<<endl;
     if(m<0||m>n)
     return NULL;
     while(q->next)
     {
         if(cur==n-m)
         {
             ans->data = q->data;
             return ans;
         }
         else
         {
             q=q->next;
         }
          
         cur++;
     }
}


int  main() 
{
    // freopen("/config/workspace/answer/in.txt","r",stdin);
    int n;
    cin >> n;
    List list;
    InitList(list);
    for(int i=0;i<n;i++){
        int v;
        cin >> v;
        int r = AddNode(list,v);
        if(r != 0) cout<<"add node failed"<<endl;
    }
    // PrintList(list);
    int m;
    cin >> m;
    Node* result = FindMthNode(list,m);
    if(result == NULL){
        cout << "failed to find the node" <<endl;
    }else{
        cout << result->data << endl;
    }
    DestroyList(list);
    return 0; 
}

问题 W: 案例2-1.6:两个有序链表序列的合并(附加代码模式)

内存限制:128 MB时间限制:1.000 S

评测方式:文本比较命题人:admin

提交:1115解决:620

返回比赛提交提交记录侧边提交

题目描述

将两个链表表示的递增整数序列合并为一个非递减的整数序列
本题是附加代码模式,以下代码会自动附加在同学们提交的代码后面,请同学们在提交的时候注释掉自己的main函数和链表输出函数。
附加代码如下:


 

void PrintList(const List &list)

{

    List p=list;

    while(p->next!=NULL){

        p=p->next;

        cout << p->data << " ";

    }

    cout << endl;

}

int main(){

    int m,n;

    cin >> m >> n;

    List listA, listB;      

    InitList(listA); // 初始化单链表 

    InitList(listB); // 初始化单链表 

    for(int i=0;i<m;i++){

        int v;cin >> v;

        AddNode(listA,v); // 在单链表的末尾增加节点 

    }

    for(int i=0;i<n;i++){

        int v; cin >> v;

        AddNode(listB, v);

    }

    List listC = MergeList(listA,listB);    //合并两个单链表 

    PrintList(listC);   // 输出单链表 

    

    DestroyList(listA);  // 销毁单链表 

    DestroyList(listB);  // 销毁单链表 

    DestroyList(listC);  // 销毁单链表 

    

    return 0;



 

输入

输入三行,第一行代表两个链表长度,第二三行分别表示两个链表表示的递增序列

输出

输出合并后的序列

样例输入 复制

3 5
1 3 5
2 4 6 8 10

样例输出 复制

1 2 3 4 5 6 8 10

提示

单链表的结构体定义和相应的操作函数如下所示:

#include <iostream>

using namespace std;

struct Node

{

    int  data;

    Node* next;

};

typedef Node* List;

void InitList(List &list)

{

}

void DestroyList(List &list)

{

}

void AddNode(List &list,int data)

{

}

List MergeList(List &listA,List &listB)

{

    List listC;    

    return listC;

}

#include <bits/stdc++.h>
using namespace std; 
struct Node{
    int data;
    Node *next;
};
typedef Node* List;
 
int InitList(List& list){
    list = (List)malloc(sizeof(List));
    list->next=NULL;
    return 0;
}
 
int DestroyList(List& list){
    List node;
    while(list->next)
    {
        node=list->next;
        list->next=node->next;
        free(node);
    }
    return 0;
}
 
 
 
int AddNode(List& list,int v){
    List node,p;
    InitList(node);
    p=list;
    while(p->next)
    {
        p = p->next;
    }
    p->next=node;
    node->data = v;
    node->next=NULL;
    return 0;
}
 
List MergeList(List &listA,List &listB)
{
    List listC;
    List p;
    InitList(listC);
    p=listC;
    while(listA->next&&listB->next)
    {
        if(listA->next->data<listB->next->data)
        {
            p->next=listA->next;
            listA ->next= listA->next->next;
            p = p->next;
            p->next = NULL;
        }
        else
        {
            p->next=listB->next;
            listB ->next= listB->next->next;
            p = p->next;
            p->next = NULL;
        }
        //cout<<1<<endl;
    }
    if(listA->next)
    {
        p->next = listA->next;
        listA->next=NULL;
    }
    if(listB->next)
    {
        p->next=listB->next;
        listB->next=NULL;
    }
    return listC;
}

问题 X: 案例3-1.4:一元多项式的乘法运算

内存限制:128 MB时间限制:1.000 S

评测方式:文本比较命题人:admin

提交:743解决:444

返回比赛提交提交记录侧边提交

题目描述

给你两个一元多项式,输出这两个一元多项式的乘积。

输入

输入包含两行,每行一个一元多项式。
每行开头一个数字n,表示该多项式非零项项数,后面有n组数字,每组数字包含两个数字,按顺序分别为该项的系数和指数。

输出

输出包含一行,按指数从大到小的顺序输出乘积的非0项的系数与指数,以空格分隔开。
如果最终结果为0,直接输出0 0。

样例输入 复制

4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1

样例输出 复制

15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
#include <bits/stdc++.h>
using namespace std; 
struct data{
    int num;
    int p;
}a[1005],b[1005],c[10000005];
 
bool mysort(struct data d,struct data e)
{
    return d.p>e.p;
}
 
 
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int m,n;
    cin>>m;
    for(int i=0;i<m;i++)
    {
        cin>>a[i].num;
        cin>>a[i].p;
    }
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>b[i].num;
        cin>>b[i].p;
    }
    int index=0;
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            c[index].num=a[i].num*b[j].num;
            c[index].p=a[i].p+b[j].p;
            index++;
        }
    }
    sort(c,c+index,mysort);
 
    for(int i=0;i<index;i++)
    {
        if(c[i].num==0)
        continue;
        for(int j=i+1;j<index;j++)
        {
            if(c[i].p==c[j].p)
            {
                c[i].num+=c[j].num;
                c[j].num=0;
            }
            else
                break;
        }
    }
    int flag=0;
    for(int i=0;i<index;i++)
    {
        if(c[i].num==0)
        continue;
        else
        {
            flag=1;
            cout<<c[i].num<<" "<<c[i].p<<" ";
        }
    }
    if(!flag)
    cout<<"0 0"<<endl;
}

问题 Y: 进阶实验2-3.3:两个有序链表序列的交集(附加代码模式)

内存限制:128 MB时间限制:1.000 S

评测方式:文本比较命题人:admin

提交:1148解决:581

返回比赛提交提交记录侧边提交

题目描述

已知两个非降序链表序列S1与S2,设计函数构造出S1与S2的交集新链表S3。
本题是附加代码模式,主函数main和打印链表的代码会自动附加在同学们提交的代码后面,请同学们在提交的时候注释掉附加代码。
附加代码如下:

void PrintList(const List &list){

    Node *p = list->next;

    if(p == NULL){

        cout<<"NULL"<<endl;

    }else{

        while(p != NULL){

            cout << p->data << " ";

            p = p->next;

        }

    }

    cout << endl;

}

int main(){

    List listA, listB;      

    InitList(listA); // 初始化单链表 

    InitList(listB); // 初始化单链表 

    int v;

    while(cin >> v && v != -1){

        AddNode(listA,v); // 在单链表的末尾增加节点 

    }

    while(cin >> v && v != -1){

        AddNode(listB,v); // 在单链表的末尾增加节点 

    }

    List listC = GetIntersection(listA,listB);  //合并两个单链表 

    PrintList(listC);   // 输出单链表 

     

    DestroyList(listA);  // 销毁单链表 

    DestroyList(listB);  // 销毁单链表 

    DestroyList(listC);  // 销毁单链表 

     

    return 0;

输入

输入分为两行,分别在每行给出由若干个正整数构成的非降序序列,用-1表示序列的结尾(-1不属于这个序列)。数字用空格间隔。

输出

在一行中输出两个输入序列的交集序列,数字间用空格分开,结尾不能有多余空格;若新链表为空,输出“NULL”(不带引号)。

样例输入 复制

1 2 5 -1
2 4 5 8 10 -1

样例输出 复制

2 5

提示

单链表的结构体定义和相应的操作函数如下图所示:

#include <iostream>

using namespace std;

struct Node{

    int data;

    Node* next;

};

typedef Node* List;

int InitList(List &list){

    return 0;

}

int DestroyList(List &list){

    return 0;

}

int AddNode(List &list, int data){

    return 0;

}

List GetIntersection(const List &listA, const List &listB){

    List listC;

     

    return listC;

}

#include <iostream>
using namespace std;
  
struct Node{
    int data;
    Node* next;
};
  
typedef Node* List;
  
int InitList(List &list){
     list = (List)malloc(sizeof(List));
    list->next=NULL;
    return 0;
}
 
 
  
int DestroyList(List &list){
    List node;
    while(list->next)
    {
        node=list->next;
        list->next=node->next;
        free(node);
    }
    return 0;
}
  
int AddNode(List &list, int data){
    List node,p;
    InitList(node);
    p=list;
    node->data = data;
    node->next=NULL;
    while(p->next)
    {
        p = p->next;
    }
    p->next=node;
    return 0;
}
  
  
List GetIntersection(const List &listA, const List &listB){
    List listC;
    List p;
    int flag=1;
    InitList(listC);
    p=listC;
    while(listA->next&&listB->next)
    {
        if(listA->next->data<listB->next->data)
        {
            listA ->next= listA->next->next;
        }
        else if(listA->next->data>listB->next->data)
        {
            listB ->next= listB->next->next;
        }
        else
        {
            flag=0;
            p-> next = listA->next;
            listA ->next= listA->next->next;
            listB ->next= listB->next->next;
            p=p->next;
            p->next=NULL;
        }
    }
    if(flag)
    {
        listC->next=NULL;
        return listC;
    }
   return listC;
}
 
 

问题 Z: 算法2-3~2-6:Big Bang

内存限制:512 MB时间限制:1.000 S

评测方式:文本比较命题人:2011014323

提交:2611解决:1470

返回比赛提交提交记录侧边提交

题目描述

复习考研累了的时候看看一集二十分钟左右的《生活大爆炸》也不失为一种乐趣。在剧中Sheldon可以说是一个极品,真不知Leonard是如何忍受这位极品室友成天的唠叨。

你知道么?Sheldon有一个神秘的小本本,记录了所有他从小开始讨厌的人名。Stuart这位漫画店老板就是小本本的一员哦,谁叫他常常毫不客气地挤兑Sheldon,曾多次赌赢过Sheldon呢。

Penny是一个漂亮的女孩,好奇心也很强。为了满足她的好奇心,我当回编剧让她意外知道了Sheldon的那个小本本放在了哪里。于是她几乎每天都去看,看看上面有哪些人。但是那个小本本上的人名实在太多。要知道她可是没上过大学在饭店里面当服务员啊。请聪明的你帮帮她处理处理那个小本本吧。

图1:《生活大爆炸》里的角色

Sheldon每天都会在小本本里记录些人名,当然有时也会与他们和好就会从小本本中将这个人名删除。我们假设Sheldon会在一个空的小本本上插入、删除、查询某个人。

要帮助Penny,你需要知道一个顺序表是怎么初始化、插入、删除以及查找的。下面我就将这些算法列举在下方。

图2:线性表的动态分配顺序存储结构以及初始化

图3:线性表的插入算法

图4:线性表的删除算法

图5:线性表的查找算法

输入

输入数据只有一组,有很多行。每行的格式可能是下列一种:
insert a name
delete name
show
search name
其中 a 是一个整数,代表在第a个名字前插入名字。name是一个姓名,只包含英文字母的大小写,每个名字不超过30个字符。
输入保证不会插入列表中已经存在的姓名,不会删除列表中不存在的姓名,也不会搜索列表中不存在的姓名。

输出

起始时,列表是空的。只输出show和search name 的结果。show将列表中的姓名全部输出,search只输出找到该名字的序号(从1开始)。每次输出占一行,姓名间用空格隔开。如果列表中没有名字了,show时也要输出一个空行。

样例输入 复制

insert 1 Stuart
insert 2 Bernadette
show
search Stuart
delete Stuart
show
insert 2 Stuart
show
insert 1 Amy
insert 2 Leslie
insert 3 Stephanie
show
delete Leslie
show
search Stuart

样例输出 复制

Stuart Bernadette
1
Bernadette
Bernadette Stuart
Amy Leslie Stephanie Bernadette Stuart
Amy Stephanie Bernadette Stuart
4

提示

提示: 
1、名字是不含空格的,指令也是一定的,所以可以用scanf("%s", str)来读取。 
2、上述代码有些函数头中变量类型与变量之间有个&,这个表示该变量是引用类型的,是C++特性。在C语言中存在值传递与指针传递,值传递中形参不可以改变实参的值,需要通过指针来修改。而引用变量实际上就是实参的另一个名字,这种类型的形参改变会影响实参的值。 
3、使用题目中的代码需要自己定义其中缺失的类型和变量,例如Status、OK以及Error等。 
4、ElemType类型中可以只有一个字符数组用来存储姓名。 
5、LocateElem_Sq函数在调用时需要传递一个函数指针,可以这样定义: Status cmp(ElemType e1, ElemType e2); 注意这个函数用来判断e1和e2是否相等,如果相等则返回非零值,否则返回0。因此可以在这个函数里直接返回 !strcmp(...)但最好需要改变返回类型。 
6、内存分配以及字符串操作需要的头文件分别是stdlib.h和string.h需要被包含进来。 
7、题目要求每个输出占一行,所以要注意换行。 


总结: 
1、实际上,题目中几乎将主要代码都写出来了。解决这道题使用上面的代码是可能复杂了点,但将各个功能独立出来是个不错的思路。以后修改就方便了,特别适用于代码量较大的程序。 
2、C语言中参数的传递分为值传递和指针传递,而C++中多了一个引用传递。值传递和指针传递都不可以改变传递进来的值,但指针可以改变其所指向的值。在C语言中,调用函数时传入的参数叫做“实参”,而在函数声明或定义中在函数头中的参数叫做“形参”。值传递与指针传递中,形参的改变是不影响实参的。C++中,引用传递,形参与实参实际上是同一个内容的不同名字,因而形参的变化会改变实参。引用传递是C++中一个很重要也很方便的特性,比如在可能会产生不必要的复制时,采用引用传递是一个很不错的解决方案。 

#include<bits/stdc++.h>
using namespace std;
 
 
int main()
{
    string data[10005];
    string s;
    int length=0;
    while (cin>>s)
    {
       if(s=="insert")
       {
           int index;
           string name;
           cin>>index>>name;
           for(int i=length;i>=index;i--)
           {
               data[i+1]=data[i];
           }
           data[index]=name;
            length++;
       }
       if(s=="show")
       {
           for(int i=1;i<=length;i++)
           cout<<data[i]<<" ";
           cout<<endl;
       }
       if(s=="delete")
       {
           string name;
           cin>>name;
           int temp=length;
           for(int i=1;i<=temp;i++)
           {
               if(data[i]==name)
               {
                   for(int j=i;j<=temp;j++)
                   data[j]=data[j+1];
                   length--;
               }
           }
       }
       if(s=="search")
       {
           string name;
           cin>>name;
           for(int i=1;i<=length;i++)
           {
               if(data[i]==name)
               {
                   cout<<i<<endl;
                   break;
               }
           }
       }
    }
     
}

问题 AA: 算法2-24 单链表反转(附加代码模式)

内存限制:128 MB时间限制:1.000 S

评测方式:文本比较命题人:2011014323

提交:3397解决:1680

返回比赛提交提交记录侧边提交

题目描述

根据一个整数序列构造一个单链表,然后将其反转。

例如:原单链表为 2 3 4 5 ,反转之后为5 4 3 2
本题是附加代码模式,主函数main和打印链表的代码会自动附加在同学们提交的代码后面,请同学们在提交的时候注释掉附加代码。
附加代码如下:
 
 

void PrintList(const List &list){

    Node *p = list->next;

    if(p == NULL){

        cout<<"list is empty"<<endl;

    }else{

        while(p != NULL){

            cout << p->data << " ";

            p = p->next;

        }

    }

    cout << endl;

}

int main(){

    int n;

    while(cin >> n){

        if( n ==0) {

            cout<<"list is empty"<<endl;

            continue;

        }

        List list;      

        InitList(list); // 初始化单链表 

        for(int i=0;i<n;i++){

            int v;

            cin >> v;

            AddNode(list,v); // 在单链表的末尾增加节点 

        }

        PrintList(list);  // 输出链表

        ReverseList(list);  // 反转链表

        PrintList(list);    // 再次输出链表 

        DestroyList(list);  // 销毁单链表 

        

    }

    return 0;

输入

输入包括多组测试数据,每组测试数据占一行,第一个为大于等于0的整数n,表示该单链表的长度,后面跟着n个整数,表示链表的每一个元素。整数之间用空格隔开

输出

针对每组测试数据,输出包括两行,分别是反转前和反转后的链表元素,用空格隔开 如果链表为空,则只输出一行,list is empty

样例输入 复制

5 1 2 3 4 5 
0

样例输出 复制

1 2 3 4 5 
5 4 3 2 1 
list is empty

提示

单链表的结构体定义和相应的操作函数如下图所示:

#include <iostream>

using namespace std;

struct Node{

    int data;

    Node* next;

};

typedef Node* List;

int InitList(List &list){

    return 0;

}

int DestroyList(List &list){

    return 0;

}

int AddNode(List &list, int data){

    

    return 0;

}


 

void ReverseList(List &list){

}

#include <bits/stdc++.h>
using namespace std;
 
struct Node{
    int data;
    Node* next;
};
 
typedef Node* List;
 
int InitList(List &list){
     list = (List)malloc(sizeof(List));
    list->next=NULL;
    return 0;
 
}
 
 
 
int DestroyList(List &list){
     List node;
    while(list->next)
    {
        node=list->next;
        list->next=node->next;
        free(node);
    }
    return 0;
}
 
int AddNode(List &list, int data){
     List node,p;
    InitList(node);
    p=list;
    node->data = data;
    node->next=NULL;
    while(p->next)
    {
        p = p->next;
    }
    p->next=node;
    return 0;
}
 
 
void ReverseList(List &list){
    List pre,p,temp;
    p=list->next;
    InitList(pre);
    pre=NULL;
    while(p!=NULL)
    {
        temp=p->next;
        p->next=pre;
        pre=p;
        p=temp;
    }
    list->next=pre;
}
 
 

问题 AB: 约瑟夫问题

内存限制:128 MB时间限制:1.000 S

评测方式:文本比较命题人:外部导入

提交:1475解决:642

返回比赛提交提交记录侧边提交

题目描述

N个人围成一圈,从第一个人开始报数,数到M的人出圈;再由下一个人开始报数,数到M的人出圈;……输出依次出圈的人的编号。N,M由键盘输入。

输入

一行,包含两个正整数N,M。1<=N,M<=100。

输出

先输出一个空行,再依次出圈的人的编号。每个编号后都有一个空格。

样例输入 复制

8 5

样例输出 复制


5 2 8 7 1 4 6 3 

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int n,m;
    cin>>n>>m;
    int a[105];
    for(int i=0;i<n;i++)
    a[i]=1;
    int ans=0;
    int temp=0;
    int i=0;
    cout<<endl;
    while(1)
    {
        if(a[i]==1)
        {
            temp++;
            //cout<<1<<endl;
        }
        if(temp==m)
        {
            ans++;
            a[i]=0;
            cout<<i+1<<" ";
            temp=0;
        }
        i+=1;
        if(i>=n)
        {
            i=0;
            //cout<<2<<endl;
        }
        if(ans==n)
        break;
    }
}

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嗯嗯你说的对

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值