如果編譯不通過,可以將C該為C++
#include<stdio.h> #include<stdlib.h> #define MAX 100 //創建節點 typedef struct Book{ double no; char name[MAX]; double price; struct Book * next; }Book,*BList; //創建鏈表 void CreatList(BList &B,int n) { B = (BList)malloc(sizeof(Book)); B->next = NULL; BList rear = B; for(int i=1;i<=n;i++) { //這里用的是尾插法 BList p = (BList)malloc(sizeof(Book)); scanf("%lf",&p->no); scanf("%s",p->name); scanf("%lf",&p->price); if(p->no==0&&p->name[0]=='0'&&p->price==0) break; rear->next = p; p->next = NULL; rear = p; } } //獲得最貴圖書的價格,并回傳其值 double getCost(BList &B) { BList p = B->next; double cost = p->price; int cout = 0; while(p) { if(p->price > cost) { cost = p->price; } p = p->next; } return cost; } void traverse(BList B,int n,double cost) { BList p = B->next; int cout = 0; for(int i=1;i<=n;i++) { if(p->price==cost) cout++; //計算最貴圖書的個數 p = p->next; } printf("%d\n",cout); BList q = B->next; for(int i=1;i<=n;i++) { if(q->price==cost) //如果圖書的價格是cost,就輸出 { printf("%.0f ",q->no); printf("%s ",q->name); printf("%.2f",q->price); printf("\n"); } q = q->next; } } int main() { BList B; int n; double cost; scanf("%d",&n); CreatList(B,n); cost = getCost(B); traverse(B,n,cost); return 0; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/137163.html
標籤:其他
上一篇:圖論篇3——最短路徑 Dijkstra演算法、Floyd演算法
下一篇:淺談K-means聚類演算法
