您的位置:首页 > 游戏 > 游戏 > 成都品牌包装设计_网上商城运营推广方案_做百度推广怎么做才能有电话_网站开发流程图

成都品牌包装设计_网上商城运营推广方案_做百度推广怎么做才能有电话_网站开发流程图

2025/1/4 14:12:24 来源:https://blog.csdn.net/2401_88085478/article/details/144462489  浏览:    关键词:成都品牌包装设计_网上商城运营推广方案_做百度推广怎么做才能有电话_网站开发流程图
成都品牌包装设计_网上商城运营推广方案_做百度推广怎么做才能有电话_网站开发流程图

Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated absolute values of the keys. That is, for each value K, only the first node of which the value or absolute value of its key equals K will be kept. At the mean time, all the removed nodes must be kept in a separate list. For example, given L being 21→-15→-15→-7→15, you must output 21→-15→-7, and the removed list -15→15.

Input Specification:

Each input file contains one test case. For each case, the first line contains the address of the first node, and a positive N (≤10^5) which is the total number of nodes. The address of a node is a 5-digit nonnegative integer, and NULL is represented by −1.

Then N lines follow, each describes a node in the format:

Address Key Next

where Address is the position of the node, Key is an integer of which absolute value is no more than 10^4, and Next is the position of the next node.

Output Specification:

For each case, output the resulting linked list first, then the removed list. Each node occupies a line, and is printed in the same format as in the input.

Sample Input:

00100 5
99999 -7 87654
23854 -15 00000
87654 15 -1
00000 -15 99999
00100 21 23854

Sample Output:

00100 21 23854
23854 -15 99999
99999 -7 -1
00000 -15 87654
87654 15 -1

题目大意:给出一个静态链表,将绝对值出现过的节点按照出现顺序组成另一个链表,从原链表中删除。输出删除后剩余的链表,再输出删除的链表。

分析:从根节点开始遍历链表,并记录出现过的节点的val值。每次遍历到一个节点,先检查它的值是否出现过,没出现过则记录,出现过则删除。最后分别输出。

#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include   <string>
#include   <vector>
#include   <cstdio>
#include    <queue>
#include    <stack>
#include    <ctime>
#include    <cmath>
#include      <map>
#include      <set>
#define INF 0xffffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
using namespace std;typedef struct node
{int id,val,next;
}node;
node num[100005],news[100005],del[100005];int main(void)
{#ifdef testfreopen("in.txt","r",stdin);//freopen("in.txt","w",stdout);clock_t start=clock();#endif //testint root,m;scanf("%d%d",&root,&m);int cnt[100005]={0};for(int i=0;i<m;++i){int id,val,next;scanf("%d%d%d",&id,&val,&next);num[id].id=id,num[id].val=val,num[id].next=next;}int ends=num[root].next,ln=1,ld=0;news[0]=num[root];int sum=num[root].val;if(sum<0)sum*=-1;cnt[sum]=1;while(ends!=-1){int temp=num[ends].val;if(temp<0)temp=temp*-1;if(cnt[temp]==0)news[ln++]=num[ends],cnt[temp]++;else del[ld++]=num[ends];ends=num[ends].next;}for(int i=0;i<ln;++i){printf("%05d %d ",news[i].id,news[i].val);if(i+1<ln)printf("%05d\n",news[i+1].id);else printf("-1\n");}for(int i=0;i<ld;++i){printf("%05d %d ",del[i].id,del[i].val);if(i+1<ld)printf("%05d\n",del[i+1].id);else printf("-1\n");}#ifdef testclockid_t end=clock();double endtime=(double)(end-start)/CLOCKS_PER_SEC;printf("\n\n\n\n\n");cout<<"Total time:"<<endtime<<"s"<<endl;        //s为单位cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位#endif //testreturn 0;
}

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com