|
发表于 2022-12-10 16:12:47
|
显示全部楼层
本楼为最佳答案
回帖奖励 +2 鱼币
本帖最后由 jhq999 于 2022-12-10 16:53 编辑
第一题,没样例不知道有没有bug
- struct child
- {
- int *candys;
- int candycount;
- };
- int main()
- {
- int m,n,r,x,y,a;
- scanf("%d%d%d",&m,&n,&r);
- struct child *childs=(struct child *)calloc(n,sizeof(struct child));
- for(int i=0; i<n; i+=1)
- {
- childs[i].candys=(int*)calloc(m,sizeof(int));
- }
- for(int i=0; i<m; i+=1)
- {
- scanf("%d",&a);
- childs[a].candys[childs[a].candycount]=i;
- childs[a].candycount+=1;
- }
- for(int i=0; i<r; i+=1)
- {
- scanf("%d%d",&x,&y);
- int *tmp=childs[x].candys;
- childs[x].candys=childs[y].candys;
- childs[y].candys=tmp;
- a=childs[x].candycount;
- childs[x].candycount=childs[y].candycount;
- childs[y].candycount=a;
- }
- for(int i=0; i<n; i+=1)
- {
- printf("%d ",childs[i].candycount);
- for(int j=0;j<childs[i].candycount;j+=1)printf("%6d ",childs[i].candys[j]);
- printf("\n");
- }
- for(int i=0; i<n; i+=1)
- {
- free(childs[i].candys);
- }
- free(childs);
- return 0;
- }
复制代码- #include <stdio.h>
- #include<stdlib.h>
- struct candy
- {
- int id;
- struct candy* next;
- };
- struct child
- {
- struct candy candys;
- int candycount;
- };
- int main()
- {
- int m,n,r,x,y,a;
- scanf("%d%d%d",&m,&n,&r);
- struct child childs[n];
- for(int i=0; i<n; i+=1)childs[i].candys.next=NULL,childs[i].candycount=0;
- for(int i=0; i<m; i+=1)
- {
- scanf("%d",&a);
- childs[a].candycount+=1;
- struct candy* c=(struct candy* )malloc(sizeof(struct candy));
- struct candy* p=&childs[a].candys;
- while(p->next)p=p->next;
- p->next=c;
- c->id=i,c->next=NULL;
- }
- for(int i=0; i<r; i+=1)
- {
- scanf("%d%d",&x,&y);
- struct candy* p=childs[x].candys.next;
- childs[x].candys.next=childs[y].candys.next;
- childs[y].candys.next=p;
- a=childs[x].candycount;
- childs[x].candycount=childs[y].candycount;
- childs[y].candycount=a;
- }
- for(int i=0; i<n; i+=1)
- {
- printf("%d ",childs[i].candycount);
- struct candy* p=childs[i].candys.next;
- while(p)
- {
- printf("%6d ",p->id);
- p=p->next;
- }
- }
- for(int i=0; i<n; i+=1)
- {
- while(childs[a].candys.next)
- {
- struct candy* p=childs[a].candys.next;
- childs[a].candys.next=p->next;
- free(p);
- }
- }
- return 0;
- }
复制代码
|
|