Find books (20 points)

Given the titles and prices of n books, this question requires writing a program to find and output the titles and prices of the books with the highest and lowest prices.

Input format:

Enter the first line to give a positive integer n (<10), and then give the information of n books. Each book is given the title in one line, which is a string of length no more than 30, followed by a positive real price in a line. The title guarantees that there is no book with the same price.

Output format:

Output the books with the highest price and the lowest price one by one in the format of "price, title". Prices retain 2 decimal places.

Input sample:
3
Programming in C
21.5
Programming in VB
18.5
Programming in Delphi
25.0
Output sample:
25.00, Programming in Delphi
18.50, Programming in VB

#include <stdio.h>

struct books
{
    char name[30];
    double price;
}b[10],t;

int main()
{
    int n,i,j;
    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
        scanf("%s%f",&b[i].name,&b[i].price);  //题目要求定价和书名要换行输入,我如果写两个scanf语句的话
        //scanf("%f",&b[i].price);             //输出后的结果就只有书名,而定价全部显示的是0.0000  
    }                                          //希望看到你们的回复帮我一下
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-i-1;j++)
        {
            if( (b[j].price - b[j+1].price) < 0)
            {
                t = b[j];
                b[j] = b[j+1];
                b[j+1] = t;
            }
        }
    }

    printf("%.2lf,%s\n",b[0].price,b[0].name);
    printf("%.2lf,%s\n",b[n-1].price,b[n-1].name);
    return 0;
}

Guess you like

Origin blog.csdn.net/xiaoning9299/article/details/82467208