题目
答案
#include<stdio.h>
#include<string.h>
struct Book{
char name[40];
double price;
};
int main()
{
int n,i;
scanf("%d",&n);
struct Book book[n];
for(i=0;i<n;i++)
{
getchar();
gets(book[i].name);
scanf("%lf",&book[i].price);
}
int max=book[0].price,min=book[0].price,flag1=0,flag2=0;
for(i=1;i<n;i++)
{
if(book[i].price>max)
{
max=book[i].price;
flag1=i;
}
if(book[i].price<min)
{
min=book[i].price;
flag2=i;
}
}
printf("%.2lf, %s\n",book[flag1].price,book[flag1].name);
printf("%.2lf, %s\n",book[flag2].price,book[flag2].name);
}
PTA测试点
总结
- 要使用getchar()吸收换行符,且要放在输入n之后输入第一个name之前,其它位置会出错
- price要设为double类型,否则如果设为float类型会在第三个测试点报错