經過上一步已經將2個分隊得到的秘密情報合并到一起,并進行了信息去重。接下來,經過情報的分析,發現情報進行加密的方式,將鏈表從正中間斷開,然后后面的鏈表全部接到前面,輸出來的次序就是敵方的武器發射次序。
函數接口定義:
ptr encrypt(ptr head,ptr last,ptr s[]);
帶頭結點,head是頭指針,last是尾指針,s是ptr類型的數組。為了對鏈表方便的取正中央位置,加了一個輔助數組(以空間換時間)。每遍歷一個結點,把當前結點的地址存到數組s中,s的元素個數就是結點個數,s的元素值就是鏈表結點的地址
裁判測試程序樣例:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef struct snode {
char name[10]; int age; int num;
struct snode *next; }node,*ptr;
ptr creat();//系統默認實現(上一步代碼)
ptr add(ptr ha,ptr hb);//系統默認實現(上一步代碼)
ptr output(ptr h);//系統默認實現,有返回值,返回尾結點的地址
ptr encrypt(ptr head,ptr last,ptr s[]);//答題者實現
ptr createnode(ptr p,ptr last);//系統實現(上一步代碼)
void addremaining(ptr p,ptr q,ptr last);//系統實現(上一步代碼)
int main() {
ptr h1,h2,h3,s[100]; h1=creat(); //
output(h1); h2=creat(); //
output(h2); h3=add(h1,h2);
ptr last3=output(h3);
last3=encrypt(h3,last3,s);
output(h3); return 0; }
ptr output(ptr h)//返回尾結點地址 {
ptr q,p; p=h->next;
while(p!=NULL) {
printf("%s %d %d; ",p->name,p->age,p->num);
q=p; p=p->next; }
printf("\n"); return q; }
ptr creat() { ptr head,tail,q;
char tname[10]; int tnum; int tage;
head=tail=(ptr)malloc(sizeof(node));
head->next=NULL;
scanf("%d%s%d",&tnum,tname,&tage);
while(tnum!=0) {
q=(ptr)malloc(sizeof(node));
strcpy(q->name,tname);
q->num=tnum;
q->age=tage;
tail->next=q;
q->next=NULL;
tail=tail->next;
scanf("%d%s%d",&tnum,tname,&tage); }
return head; }
/* 請在這里填寫答案 */
輸入樣例1:
2 tom 22
4 jim 23
6 ho 21
0 0 0
1 lili 20
4 jim 23
7 bobo 19
9 fei 20
0 0 0
輸出樣例1:
lili 20 1; tom 22 2; jim 23 4; ho 21 6; bobo 19 7; fei 20 9;
ho 21 6; bobo 19 7; fei 20 9; lili 20 1; tom 22 2; jim 23 4;
6個結點,后三個結點斷開,重串到鏈表的首部,畫出示意圖,再理清串接關系
代碼實現:
?
ptr encrypt(ptr head, ptr last, ptr s[]) {int blacksheep=0;ptr p = head->next;while (p!= last) {s[blacksheep] = p;p = p->next;blacksheep++;}int mid = (blacksheep-1) / 2;p->next=NULL;head->next=s[mid+1];last->next=s[0];s[mid]->next=NULL;return head;
}?