zoukankan      html  css  js  c++  java
  • 环状链表与它的数组模拟方案

    蓝桥杯题目:能量项链

    题目链接:https://www.dotcpp.com/oj/problem1255.html(这是一个蓝桥杯的题目)

    下面是我的两个代码:

    1、链表模拟:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<malloc.h>
     4 #include<iostream>
     5 #include<algorithm>
     6 using namespace std;
     7 struct point
     8 {
     9     int q,h;
    10     point *last;
    11     point *next;
    12     bool ishere;
    13 }; 
    14 int n;
    15 int nums[200];
    16 int cur=0;
    17 int energy=0;
    18 point t;
    19 
    20 int main()
    21 {
    22     t.ishere=true;
    23     cin>>n;
    24     int a,b;
    25     cin>>a;
    26     nums[cur++]=a;
    27     //
    28     cin>>b;
    29     nums[cur++]=b;
    30     t.q=a;//输入的第一个数t.q 
    31     t.h=b;
    32     point *lastpoint=&t;
    33     a=b;
    34     //设置第一个元素 
    35     for(int i=0;i<n-1;i++)
    36     {
    37         if(i!=n-2){cin>>b;nums[cur++]=b;}
    38         else b=t.q;
    39         point *temp = (point*)malloc(sizeof(point));
    40         temp->ishere=true;
    41         temp->q=a;
    42         temp->h=b;
    43         temp->last=lastpoint;
    44         lastpoint->next=temp;
    45         a=b;
    46         lastpoint=temp;
    47     }
    48     t.last=lastpoint;
    49     lastpoint->next=&t;
    50     //完成闭环操作 
    51     sort(nums,nums+n);
    52     //
    53     //
    54     for(int i=0;i<n-1;i++)
    55     {
    56         int aimnum=nums[i];
    57         //if(aimnum==10)break;
    58         //printf("aim->%d
    ",aimnum);
    59         point *temp=&t;
    60         while(!temp->ishere)temp=temp->next;
    61         while(1)
    62         {
    63             if(temp->h==aimnum)
    64             {
    65                 point *s = temp->next;
    66                 energy+=(aimnum*(temp->q)*(s->h));
    67                 //printf("%d->%d<-%d
    ",temp->q,aimnum,s->h);
    68                 temp->h=s->h;
    69                 point *third = s->next;
    70                 third->last=temp;
    71                 temp->next=third;
    72                 s->ishere=false;
    73                 break;
    74             }
    75             temp=temp->next;
    76         }
    77         //完成一个两个珠子的融合 
    78         //pout();
    79     }
    80     printf("%d
    ",energy);
    81     return 0;
    82 }

    2、数组模拟环状链表

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<malloc.h>
     4 #include<iostream>
     5 #include<algorithm>
     6 using namespace std;
     7 struct point
     8 {
     9     int p;
    10     int value;
    11     bool operator<(const point& rhs)const
    12     {
    13         return this->value<rhs.value;
    14     }
    15 };
    16 const int maxn = 103;
    17 int n;
    18 int q[maxn];
    19 int h[maxn];
    20 bool ishere[maxn];
    21 int energy=0;
    22 
    23 int main()
    24 {
    25     cin>>n;
    26     for(int i=0;i<n;i++)
    27     {
    28         cin>>q[i];
    29         h[(i-1+n)%n]=q[i];
    30     }
    31     h[n-1]=q[0];
    32     point *aim = new point[n+2]; 
    33     for(int i=0;i<n;i++)
    34     {
    35         ishere[i]=true;
    36         aim[i].value=h[i];
    37         aim[i].p=i;
    38     }
    39     point *ender = aim+n;
    40     sort(aim,ender);
    41     for(int i=0;i<n-1;i++)
    42     {
    43         int aimi = aim[i].p;
    44         int k=(aimi+1)%n;
    45         while(!ishere[k])k=(k+1)%n;
    46         energy+=(q[aimi]*h[aimi]*h[k]);
    47         q[k]=q[aimi];
    48         ishere[aimi]=false;
    49     }
    50     printf("%d
    ",energy);
    51     return 0;
    52 }

    虽然我尽量提高算法质量,但是上述的两个代码在测试时仍然保持着内存使用量不变,唯一不同的是代码长度[哭]。

    第一个代码写起来并没有那么麻烦,而第二个算法的细节上仍然需要一些注意。

    总之上述的方案提供了一种对 数组模拟环状链表 的方案。

  • 相关阅读:
    关于Ubuntu中passwd、shadow、group等文件
    Android colors.xml 颜色列表
    【设计】线框图、原型和视觉稿的区别
    【设计】24款线框图相关工具及资源大放送
    【辅助工具】20款优秀的移动产品原型和线框图设计工具(二)
    【辅助工具】20款优秀的移动产品原型和线框图设计工具(一)
    GET RESTful With Python
    VRRP、Track与NQA联动配置举例(Master监视上行链路)
    静态路由、Track与NQA联动配置举例
    ROS-MikroTik-RouterOS-培训认证各种证书
  • 原文地址:https://www.cnblogs.com/savennist/p/12422093.html
Copyright © 2011-2022 走看看