简练版
#include<stdio.h>
int main()
{
char str[100];
scanf("%s", str);
int i,x,j=0,k=0,num[100],list[5][5],flag=0;
scanf("%d", &x);
for (i = 0; str[i] != '\0'; i++)
{
if (str[i] >= '0' && str[i] <= '9')
{
num[j++] = str[i] - '0';
}
}
for(i=0;i<5;i++)
{
for(k=0;k<5;k++)
{
list[i][k]=-1;
}
}
for(i=0;i<j;i=i+2)
{
list[num[i]][num[i+1]]=1;
}
for(i=0;i<5;i++)
{
k=0;
for(j=0;j<5;j++)
{
if(list[i][j]!=-1)
{
k++;
if(k%2!=0)
list[i][j]=5;
}
}
}
for(i=0;i<5;i++)
{
if(list[i][x]==5)
{
printf("%d,",i);
flag=1;
}
}
if(flag==0) printf("-1");
return 0;
}
图的链式存储
#include <stdio.h>
#include <stdlib.h>
typedef struct people
{
struct people *next;
int number;
int count;
} People;
int main()
{
char a[100];
gets(a);
int i = 0;
People *head[5];
for (int i = 0; i < 5; i++)
{
head[i] = (People *)malloc(sizeof(People));
head[i]->next = NULL;
}
People *New = NULL;
int fly[2] = {-1, -1};
while (a[i])
{
if (a[i] != ',')
{
if (fly[0] == -1)
{
fly[0] = a[i] - '0';
}
else
{
fly[1] = a[i] - '0';
People *p = head[fly[0]]->next;
New = (People *)malloc(sizeof(People));
New->next = NULL;
New->count = 0;
New->number = fly[1];
if (p)
{
if (New->number < p->number)
{
People *temp = head[fly[0]]->next;
head[fly[0]]->next = New;
New->next = temp;
}
else
{
for (p = head[fly[0]]->next; p->next && New->number > p->number; p = p->next)
;
People *temp = p->next;
p->next = New;
New->next = temp;
}
}
else
{
head[fly[0]]->next = New;
}
fly[0] = fly[1] = -1;
}
}
i++;
}
for (int i = 0; i < 5; i++) //打印函数,限于调试
{
People *p = head[i]->next;
int cnt=1;
while (p)
{
p->count=cnt++;
p = p->next;
}
printf("\n");
}
int print;
int flag = 0;
scanf("%d", &print);
for (int i = 0; i < 5; i++)
{
People *p = head[i]->next;
while (p)
{
if (p->number == print)
{
if (p->count % 2 == 1)
{
flag = 1;
printf("%d,", i);
}
}
p = p->next;
}
}
if (flag == 0)
printf("-1");
return 0;
}
// 0,1,0,2,0,3,1,4,2,1,2,3,3,2,4,0,4,1,4,3,
// 1
// 4,3,4,1,4,0,2,3,2,1,1,4,0,3,0,2,0,1,
// 2
十字链表
#include<stdio.h>
#include<stdlib.h>
typedef struct arc{
int head,tail,like;
struct arc* hlink;
struct arc* tlink;
}arc,*parc;
typedef struct vertex{
int data;
parc firstin;
parc firstout;
}Vnode,*Vertex;
int main()
{
char str[100];
Vertex head[5];
parc temp,mid;
scanf("%s",str);
int x,i,j=0,num[100],flag=0;
scanf("%d",&x);
for(i=0;str[i]!='\0';i++)
{
if(str[i]>='0'&&str[i]<='9')
{
num[j++] = str[i] - '0';
}
}
for(i=0;i<5;i++)
{
head[i] = (Vertex)malloc(sizeof(Vnode));
head[i]->data = i;
head[i]->firstin = head[i]->firstout = NULL;
}
for(i=0;i<j;i=i+2)
{
parc p = (parc)malloc(sizeof(arc));
p->head = num[i];
p->tail = num[i+1];
p->like = 0;
p->hlink = p->tlink = NULL;
if(!head[num[i]]->firstout){
head[num[i]]->firstout = p;
}
else{
if(p->tail<head[num[i]]->firstout->tail){
mid = head[num[i]]->firstout;
head[num[i]]->firstout = p;
p->tlink = mid;
}
else{
for(temp=head[num[i]]->firstout;temp->tlink&&p->tail>temp->tail;temp=temp->tlink);
mid = temp->tlink;
temp->tlink = p;
p->tlink = mid;
}
}
if(!head[num[i+1]]->firstin)
{
head[num[i+1]]->firstin = p;
}
else{
for(temp=head[num[i+1]]->firstin;temp->hlink;temp=temp->hlink);
temp->hlink = p;
}
}
for(i=0;i<5;i++)
{
if(head[i]->firstout)
{
for(j=1,temp=head[i]->firstout;temp;temp=temp->tlink,j++)
{
if((j%2)!=0) temp->like = 1;
}
}
}
i=0;
if(head[x]->firstin)
{
for(temp=head[x]->firstin;temp;temp=temp->hlink)
{
if(temp->like==1){
flag=1;
num[i++] = temp->head;
}
}
}
if(flag==0) printf("-1");
else{
for(j=i-1;j>0;j--)
{
for(int k=0;k<j;k++)
{
if(num[k]>num[k+1])
{
x = num[k];
num[k] = num[k+1];
num[k+1] = x;
}
}
}
for(j=0;j<i;j++)
{
printf("%d,",num[j]);
}
}
return 0;
}