查找书籍
https://pintia.cn/problem-sets/12/problems/346
这个程序的算法不难,因为没有学透标准输入和输出,特别是gets()函数。如果不用getchar()读取多余的'\n',程序就会运行错误。
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 struct book 5 { 6 char title[31]; 7 double price; 8 }; 9 int main(void) 10 { 11 int i, n, min, max; 12 struct book *p; 13 14 scanf("%d", &n); 15 p = (struct book *)calloc(n, sizeof(struct book)); 16 17 for (i = 0; i < n; i++) 18 { 19 getchar(); 20 gets((p+i)->title); 21 scanf("%lf", &((p+i)->price)); 22 } 23 min = max = 0; 24 for (i = 1; i < n; i++) 25 { 26 if ((p + i)->price > (p + max)->price) 27 { 28 max = i; 29 } 30 if ((p + i)->price < (p + min)->price) 31 { 32 min = i; 33 } 34 } 35 printf("%.2f, %s\n", (p + max)->price, (p + max)->title); 36 printf("%.2f, %s\n", (p + min)->price, (p + min)->title); 37 38 free(p); 39 return 0; 40 }