1032 Sharing (25 分)

版本2

静态链表

#include 
#include 
#include 
#include 
#include 
#include 
#include  
#include 
#include 
#include 
#include 
using namespace std;
const int N = 1e5+5;
struct node{
	char data;
	int address, next, idx;
}E[N];
int main(){
	int stA, stB, n, add, next;
	char c;
	cin>>stA>>stB>>n;
	for(int i = 0; i < n; i++){
		cin>>add>>c>>next;
		E[add]= {c, add, next, 0};
	}
	while(stA != -1){
		E[stA].idx = 1;
		stA = E[stA].next;
	}
	bool flag = false;
	while(stB != -1){
		if(E[stB].idx == 1){
			printf("%05d", E[stB].address);
			flag = true;
			break;
		}
		stB = E[stB].next;
	}
	if(!flag) printf("-1");
	return 0;
}

版本1

思路:使用静态链表,相当于哈希操作。
注意:地址需要输出5位数

#include
struct node{
	char c;
	int  next;
	int index;
}Node[100006];
int main(){
	int x,y,n,cur,next;
	char c;
	scanf("%d%d%d",&x,&y,&n);
	for(int i=0;i<n;i++){
		scanf("%d %c %d",&cur,&c,&next);
		Node[cur].c=c;
		Node[cur].next=next;
		Node[cur].index=0;
	}
	
	while(x!=-1){
		Node[x].index=1;
		x=Node[x].next;
	}
	int flag=0;
	while(y!=-1){
		if(Node[y].index==1){
			flag=y;
			break;
		}
		y=Node[y].next;
	}
	if(flag==0) 
		printf("-1\n");
	else 
		printf("%05d\n",flag);
	return 0;
}

你可能感兴趣的:(链表,PAT甲级真题题解,数组,数组)