BJFU data structure sequence table partial problem solving and integration

204. Creation and output of book information table based on sequential storage structure

describe

Define a sequential table containing book information (book number, book name and price), read in the corresponding book data to complete the creation of the book information table, then count the number of books in the book table, and output the information of each book line by line.

input

Enter n+1 line, where the first n lines are the information of n books (book number, book name and price). Each book information occupies one line. The book number, book name and price are separated by spaces, and there is no space after the price. Finally, line n+1 is the input end flag: 0 (three zeros separated by spaces). The book number and title are string type, and the price is floating point type.

output

Total n+1 lines. The first line is the number of books in the created book table. The last n lines are the information of n books (book number, book name and price). Each book information occupies one line, and the book number, book name and price are separated by spaces. Two decimal places are reserved for price output.

Input sample 1
9787302257646 Data-Structure 35.00
9787302164340 Operating-System 50.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9787822234110 The-C-Programming-Language 38.00
0 0 0
Output sample 1
8
9787302257646 Data-Structure 35.00
9787302164340 Operating-System 50.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9787822234110 The-C-Programming-Language 38.00
AC code (C + +)
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

typedef struct  // Define a Book Structure
{
    string ibsn;
    string name;
    double price;
}Book;

const int MaxSize = 10010; // Defines the maximum length of the sequence table

// 1. Create a sequence table
Book books[MaxSize];

int main()
{
    // 2. Initialization sequence table, this type does not need to be initialized
    
    // 3. Establish the sequence table
    int n = 0; // Traversal times
    while (1)
    {
        cin >> books[n].ibsn >> books[n].name >> books[n].price;
        
        if (books[n].price == 0) break; // When 3 zeros are output, the output ends

        n ++;
    }

    // Overload sorting
    // sort(SqList, SqList + n);

    // 4. Output
    cout << n << endl;
    for (int i = 0; i < n; i ++)
    {
        cout << books[i].ibsn << " " << books[i].name << " ";
        printf("%.2lf\n", books[i].price); // Title Requirements
    }

    return 0;
}

205. Sorting of book information table based on sequential storage structure

describe

Define a sequence table containing book information (book number, book name and price), read in the corresponding book data, complete the creation of the book information table, then sort the books in descending order according to the price, and output the information of each book row by row.

input

Enter n+1 line. The first n lines are the information of n books (book number, book name and price). Each book information occupies one line. The book number, book name and price are separated by spaces, and there is no space after the price. Finally, line n+1 is the input end flag: 0 (three zeros separated by spaces). The book number and title are string type, and the price is floating point type.

output

There are n lines in total. Each line is the information of a Book (book number, title and price). The book number, title and price are separated by spaces. Two decimal places are reserved for price output.

Input sample 1
9787302257646 Data-Structure 35.00
9787302164340 Operating-System 50.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9787822234110 The-C-Programming-Language 38.00
0 0 0
Output sample 1
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9787302164340 Operating-System 50.00
9787822234110 The-C-Programming-Language 38.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257646 Data-Structure 35.00
9787302219972 Software-Engineer 32.00
AC code
#include <iostream>
#include <cstring>
#Include < algorithm > / / the sort function is in this library

using namespace std;

typedef struct // Create book structure
{
    string ibsn;
    string name;
    double price;
}Book;

const int MaxSize = 10010;
Book books[MaxSize]; // Defines the order table for the Book type

bool operator < (const Book &a, const Book &b) // Overload less than sign
{
    return a.price > b.price; // Because the title requires descending price output
}

int main()
{
    int n = 0;
    
    while (1) // 1. Create sequence table
    {
        cin >> books[n].ibsn >> books[n].name >> books[n].price;
        
        if (books[n].price == 0) break; // The input is terminated when three zeros are entered
        
        n ++; // Note that this n + + operation needs to be followed by the break statement
    }
    
    sort(books, books + n); // 2. Using overload characteristics to sort
    
    for (int i = 0; i < n; i ++) // 3. Output
    {
        cout << books[i].ibsn << " " << books[i].name << " ";
        printf("%.2lf\n", books[i].price);
    }
    
    return 0;
}

206. Modification of book information table based on sequential storage structure

describe

Define a sequence table containing book information (book number, book name and price), read in the corresponding book data, complete the creation of the book information table, then calculate the average price of all books, increase the price of all books lower than the average price by 20%, increase the price of all books higher than or equal to the average price by 10%, and finally output the book information with the price modified line by line.

input

Enter n+1 line. The first n lines are the information of n books (book number, book name and price). Each book information occupies one line. The book number, book name and price are separated by spaces, and there is no space after the price. Finally, line n+1 is the input end flag: 0 (three zeros separated by spaces). The book number and title are string type, and the price is floating point type.

output

Total n+1 lines. The first line is the average price of all books before modification, and the last n lines are the information (book number, book name and price) of n books after price modification. Each book information occupies one line, and the book number, book name and price are separated by spaces. Two decimal places are reserved for price output.

Input sample 1
9787302257646 Data-Structure 35.00
9787302164340 Operating-System 50.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9787822234110 The-C-Programming-Language 38.00
0 0 0
Output sample 1
43.88
9787302257646 Data-Structure 42.00
9787302164340 Operating-System 55.00
9787302219972 Software-Engineer 38.40
9787302203513 Database-Principles 43.20
9787810827430 Discrete-Mathematics 43.20
9787302257800 Data-Structure 68.20
9787811234923 Compiler-Principles 68.20
9787822234110 The-C-Programming-Language 45.60
AC code
#include <iostream>
#include <cstring>
#Include < algorithm > / / the sort function is in this library

using namespace std;

typedef struct // Create book structure
{
    string ibsn;
    string name;
    double price;
}Book;

const int MaxSize = 10010;
Book books[MaxSize]; // Defines the order table for the Book type


int main()
{
    int n = 0;
    
    double sum = 0;
    while (1) // 1. Create sequence table and sum
    {
        cin >> books[n].ibsn >> books[n].name >> books[n].price;
        
        if (books[n].price == 0) break; // The input is terminated when three zeros are entered
        
        sum += books[n].price; // Price plus
        
        n ++; // Note that this n + + operation needs to be followed by the break statement
    }
    
    // 2. Average
    double ave = sum / n; // Here, because the right value contains the double type, you can divide it slightly to get the average value
    printf("%.2lf\n", ave);
    
    for (int i = 0; i < n; i ++) // 3. Output
    {
        if (books[i].price < ave) books[i].price *= 1.2; // Operate according to the requirements of the topic
        else books[i].price *= 1.1;
        
        cout << books[i].ibsn << " " << books[i].name << " ";
        printf("%.2lf\n", books[i].price);
    }
    
    return 0;
}

207. Reverse order storage of book information table based on sequential storage structure

describe

Define a sequence table containing book information (book number, book name and price), read in the corresponding book data to complete the creation of the book information table, then store the read book information in reverse order, and output the information of each book in reverse order line by line.

input

Enter n+1 line. The first line is the number of books n, and the next N lines are the information of n books (book number, book name and price). Each book information occupies one line. The book number, book name and price are separated by spaces, and there is no space after the price. The book number and title are string type, and the price is floating point type.

output

There are n rows in total. Row I is the information (book number, book name and price) of the books in row n-i+1 in the original book list. Each book information occupies one row, and the book number, book name and price are separated by spaces. Two decimal places are reserved for price output.

Input sample 1
8
9787302257646 Data-Structure 35.00
9787302164340 Operating-System 50.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9787822234110 The-C-Programming-Language 38.00
Output sample 1
9787822234110 The-C-Programming-Language 38.00
9787811234923 Compiler-Principles 62.00
9787302257800 Data-Structure 62.00
9787810827430 Discrete-Mathematics 36.00
9787302203513 Database-Principles 36.00
9787302219972 Software-Engineer 32.00
9787302164340 Operating-System 50.00
9787302257646 Data-Structure 35.00
AC code
#include <iostream>

using namespace std;

typedef struct // Create book structure
{
    string ibsn;
    string name;
    double price;
}Book;

const int MaxSize = 10010;
Book books[MaxSize]; // Create book structure type array

int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i ++) // 1. Input
        cin >> books[i].ibsn >> books[i].name >> books[i].price;
    
    for (int i = n - 1; i >= 0; i --)
    {
        cout << books[i].ibsn << " " << books[i].name << " ";
        printf("%.2lf\n", books[i].price);
    }
    
    return 0;
}

208. Search for the most expensive books in the book information table based on the sequential storage structure

describe

Define a sequence table containing book information (book number, book name and price), read in the corresponding book data to complete the creation of the book information table, then find the book with the highest price and output the information of the corresponding book.

input

Enter a total of n+1 lines. The first line is the number of books n, and the next N lines are the information of n books (book number, book name and price). Each book information occupies one line. The book number, book name and price are separated by spaces, and there is no space after the price. The book number and title are string type, and the price is floating point type.

output

The total output is m+1 lines. The first line is the number of the most expensive books (there may be multiple books with the highest price), and the last m line is the information of the most expensive books. Each book information accounts for one line. The book number, title and price are separated by spaces, and the price output retains two decimal places.

Input sample 1
8
9787302257646 Data-Structure 35.00
9787302164340 Operating-System 50.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9787822234110 The-C-Programming-Language 38.00
Output sample 1
2
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
AC code
// The general idea is to output the first one directly after shooting the sequence (in descending order)

#include <iostream>
#include <cstring>
#Include < algorithm > / / the sort function is in this library

using namespace std;

typedef struct // Create book structure
{
    string ibsn;
    string name;
    double price;
}Book;

const int MaxSize = 10010;
Book books[MaxSize]; // Defines the order table for the Book type

bool operator < (const Book &a, const Book &b) // Overload less than sign
{
    return a.price > b.price; // Because the title requires descending price output
}

int main()
{
    int n;
    cin >> n;
    
    for (int i = 0; i < n; i ++) // 1. Create sequence table
        cin >> books[i].ibsn >> books[i].name >> books[i].price;
        
    sort(books, books + n); // 2. Using overload characteristics to sort
    
    // For (int i = 0; I < n; I + +) / / 3. Output
    // {
    //     cout << books[i].ibsn << " " << books[i].name << " ";
    //     printf("%.2lf\n", books[i].price);
    // }
    
    int ans = 0;
    while (books[ans].price == books[0].price)
        ans ++; // Record the number of books with the same price as the most expensive
    
    cout << ans << endl;
    for (int i = 0; i < ans; i ++)
    {
        cout << books[i].ibsn << " " << books[i].name << " ";
        printf("%.2lf\n", books[0].price);
    }
    
    return 0;
}

209. Search of favorite books in book information table based on sequential storage structure

describe

Define a sequence table containing book information (book number, book name and price), read in the corresponding book data to complete the creation of the book information table, and then find the favorite book according to the specified name of the favorite book, and output the information of the corresponding book.

input

Total n+m+2 lines. First, enter n+1 line. The first line is the number of books n, and the next N lines are the information of n books (book number, book name and price). Each book information occupies one line. The book number, book name and price are separated by spaces, and there is no space after the price. The book number and title are string type, and the price is floating point type. Then enter line m+1, where the first line is an integer m, representing m times of searching, and the last line m is the name of the favorite book to be searched each time.

output

If the search is successful: a total of m * (k+1) lines are output. For each search, the first line is the number of favorite books (there may be multiple books with the same title), and the last k lines are the information of favorite books (book number, book name and price). Each book information occupies one line. The book number, book name and price are separated by spaces, and the price output retains two decimal places. If the search fails: only the following prompt is output: sorry, there is no your favorite!

Input sample 1
8
9787302257646 Data-Structure 35.00
9787302164340 Operating-System 50.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9787822234110 The-C-Programming-Language 38.00
2
Java-Programming-Language
Data-Structure
Output sample 1
Sorry,there is no your favourite!
2
9787302257646 Data-Structure 35.00
9787302257800 Data-Structure 62.00
AC code
// The general idea is to output the first one directly after shooting the sequence (in descending order)

#include <iostream>
#include <cstring>
#Include < algorithm > / / the sort function is in this library

using namespace std;

typedef struct // Create book structure
{
    string ibsn;
    string name;
    double price;
}Book;

const int MaxSize = 1010;
Book books[MaxSize]; // Defines the order table for the Book type

bool operator < (const Book &a, const Book &b) // Overload less than sign
{
    return a.price > b.price; // Because the title requires descending price output
}

int main()
{
    int n;
    cin >> n;
    
    for (int i = 0; i < n; i ++) // 1. Create sequence table
        cin >> books[i].ibsn >> books[i].name >> books[i].price;
        
    int m;
    cin >> m;
    while (m --)
    {
        string name;
        cin >> name;
        
        int cnt = 0; // Number of favorite books
        for (int i = 0; i < n; i ++)
            if (books[i].name == name)
                cnt ++;
        
        if (cnt == 0) cout << "Sorry,there is no your favourite!" << endl;
        else 
        {
            cout << cnt << endl;
            for (int i = 0; i < n; i ++)
                if (books[i].name == name)
                {
                    cout << books[i].ibsn << " " << books[i].name << " ";
                    printf("%.2lf\n", books[i].price);
                }
        }
    }
    
    return 0;
}

210. Search for the best position book of the book information table based on the sequential storage structure

describe

Define a sequence table containing book information (book number, book name and price), read in the corresponding book data to complete the creation of the book information table, and then find the books in the position according to the serial number of the specified best position, and output the information of the corresponding books.

input

Total n+m+2 lines. First, enter n+1 line. The first line is the number of books n, and the next N lines are the information of n books (book number, book name and price). Each book information occupies one line. The book number, book name and price are separated by spaces, and there is no space after the price. The book number and title are string type, and the price is floating point type. Then enter line m+1, where the first line is an integer m, representing m times of searching, and the content of each line of the last m lines is an integer, representing the position serial number of the book to be searched.

output

If the search of the output m line is successful: the output content is the information (book number, book name and price) of a book at the specified position of the i-th query. The book number, book name and price are separated by spaces, and the price output retains two decimal places. If the search fails: only the following prompt is output: sorry, the book in the best position does not exist!

Input sample 1
8
9787302257646 Data-Structure 35.00
9787302164340 Operating-System 50.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9787822234110 The-C-Programming-Language 38.00
2
2
0
Output sample 1
9787302164340 Operating-System 50.00
Sorry,the book on the best position doesn't exist!
AC code
#include <iostream>
#include <cstring>

using namespace std;

typedef struct // Create book structure
{
    string ibsn;
    string name;
    double price;
}Book;

const int MaxSize = 1010;
Book books[MaxSize]; // Defines the order table for the Book type

int main()
{
    int n;
    cin >> n;
    
    for (int i = 1; i <= n; i ++) // 1. Establish a sequence table. Note that here, in order to be opposite to the bit order, it starts from 1
        cin >> books[i].ibsn >> books[i].name >> books[i].price;
        
    int m;
    cin >> m;
    while (m --)
    {
        int site; // 2. Input location
        cin >> site;

        if (site < 1 || site > n) cout << "Sorry,the book on the best position doesn't exist!" << endl;
        else 
        {
            cout << books[site].ibsn << " " << books[site].name << " ";
            printf("%.2lf\n", books[site].price);            
        }
    }
    
    return 0;
}

211. Warehousing of new books in book information table based on sequential storage structure

describe

Define a sequence table containing book information (book number, book name and price), read in the corresponding book data to complete the creation of the book information table, then insert the new book into the specified position in the book table according to the specified position and information of the new book to be warehoused, and finally output the information of all books after the new book is warehoused.

input

Total n+3 lines. First, enter n+1 line. The first line is the number of books n, and the next N lines are the information of n books (book number, book name and price). Each book information occupies one line. The book number, book name and price are separated by spaces, and there is no space after the price. The book number and title are string type, and the price is floating point type. Then enter line n+2, the content is only an integer, representing the position serial number of the new book to be warehoused. Finally, enter line n+3. The content is the information of the new book. The book number, title and price are separated by spaces.

output

If the insertion is successful: output the information (book number, book name and price) of all books after the new book is warehoused, totaling n+1 lines. Each line is the information of a book, and the book number, book name and price are separated by spaces. Two decimal places are reserved for price output. If the insertion fails: only the following prompt is output: sorry, the warehousing location is illegal!

Input sample 1
7
9787302257646 Data-Structure 35.00
9787302164340 Operating-System 50.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
2
9787822234110 The-C-Programming-Language 38.00
Output sample 1
9787302257646 Data-Structure 35.00
9787822234110 The-C-Programming-Language 38.00
9787302164340 Operating-System 50.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
Input sample 2
7
9787302257646 Data-Structure 35.00
9787302164340 Operating-System 50.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9
9787822234110 The-C-Programming-Language 38.00
Output sample 2
Sorry,the position to be inserted is invalid!
AC code
#include <iostream>
#include <cstring>

using namespace std;

typedef struct // Create book structure
{
    string ibsn;
    string name;
    double price;
}Book;

const int MaxSize = 1010;
Book books[MaxSize]; // Defines the order table for the Book type

int main()
{
    int n;
    cin >> n;
    
    for (int i = 1; i <= n; i ++) // 1. Establish a sequence table. Note that here, in order to be opposite to the bit order, it starts from 1
        cin >> books[i].ibsn >> books[i].name >> books[i].price;
        
    int site; // 2. Enter insertion location
    cin >> site;

    if (site < 1 || site > n)
    {
        cout << "Sorry,the position to be inserted is invalid!" << endl;
        return 0; // Note that you can no longer run the program here
    }
    else  
    {
        for (int i = n; i >= site; i --) // Start traversal in reverse order and move the sequence table back
        {
            books[i + 1].ibsn = books[i].ibsn;
            books[i + 1].name = books[i].name;
            books[i + 1].price = books[i].price;
        }
        
        cin >> books[site].ibsn >> books[site].name >> books[site].price;
    }
    
    for (int i = 1; i <= n + 1; i ++)
    {
        cout << books[i].ibsn << " " << books[i].name << " ";
        printf("%.2lf\n", books[i].price);
    }
    
    return 0;
}

212. Ex warehouse of old books based on book information table with sequential storage structure

describe

Define a sequence table containing book information (book number, book name and price), read in the corresponding book data to complete the creation of the book information table, then delete the book from the book table according to the specified location of the old book to be delivered, and finally output the information of all books after the book is delivered.

input

Total n+2 lines. First, enter n+1 line. The first line is the number of books n, and the next N lines are the information of n books (book number, book name and price). Each book information occupies one line. The book number, book name and price are separated by spaces, and there is no space after the price. The book number and title are string type, and the price is floating point type. Then enter line n+2, the content is only an integer, representing the position serial number of the old book to be deleted.

output

If the deletion is successful: output the information (book number, book name and price) of all the books after the old books are delivered, totaling n-1 lines. Each line is the information of a book, and the book number, book name and price are separated by spaces. Two decimal places are reserved for price output. If deletion fails: only the following prompt will be output: sorry, the outbound location is illegal!

Input sample 1
7
9787302257646 Data-Structure 35.00
9787302164340 Operating-System 50.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
2
9787822234110 The-C-Programming-Language 38.00
Output sample 1
9787302257646 Data-Structure 35.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9787822234110 The-C-Programming-Language 38.00
Input sample 2
8
9787302257646 Data-Structure 35.00
9787302164340 Operating-System 50.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9787822234110 The-C-Programming-Language 38.00
9
Output sample 2
Sorry,the position to be deleted is invalid!
AC code
#include <iostream>
#include <cstring>

using namespace std;

typedef struct // Create book structure
{
    string ibsn;
    string name;
    double price;
}Book;

const int MaxSize = 1010;
Book books[MaxSize]; // Defines the order table for the Book type

int main()
{
    int n;
    cin >> n;
    
    for (int i = 1; i <= n; i ++) // 1. Establish a sequence table. Note that here, in order to be opposite to the bit order, it starts from 1
        cin >> books[i].ibsn >> books[i].name >> books[i].price;
        
    int site; // 2. Enter insertion location
    cin >> site;

    if (site < 1 || site > n)
    {
        cout << "Sorry,the position to be deleted is invalid!" << endl;
        return 0;
    }
    else 
        for (int i = site; i <= n - 1; i ++) // Traverse to replace
        {
            books[i].ibsn = books[i + 1].ibsn;
            books[i].name = books[i + 1].name;
            books[i].price = books[i + 1].price;
        }
    
    for (int i = 1; i <= n - 1; i ++)
    {
        cout << books[i].ibsn << " " << books[i].name << " ";
        printf("%.2lf\n", books[i].price);
    }
    
    return 0;
}

213. Book de duplication of book information table based on sequential storage structure

describe

The book number (ISBN) of any book published by the publishing house is unique, that is, books with duplicate book numbers are not allowed in the book list. Define a sequence table containing book information (book number, book name and price), read in the corresponding book data to complete the creation of book information table (book number may be repeated), then de duplicate the book, that is, delete the book with duplicate book number (only the first book is retained), and finally output the information of all books after de duplication.

input

Enter a total of n+1 lines. The first line is the number of books n, and the next N lines are the information of n books (book number, book name and price). Each book information occupies one line. The book number, book name and price are separated by spaces, and there is no space after the price (the book number may be repeated). The book number and title are string type, and the price is floating point type.

output

The total output is m+1 line (m ≤ n), where the first line is the number of books after de duplication, and the last m line is the information of books after de duplication (book number, book name and price). Each book information occupies one line. The book number, book name and price are separated by spaces, and the price output retains two decimal places.

Input sample 1
9
9787302257646 Data-Structure 35.00
9787302164340 Operating-System 50.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9787811234923 Compiler-Principles 62.00
9787822234110 The-C-Programming-Language 38.00
Output sample 1
8
9787302257646 Data-Structure 35.00
9787302164340 Operating-System 50.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9787822234110 The-C-Programming-Language 38.00
AC code
#include <iostream>
#include <cstring>

using namespace std;

typedef struct // Create book structure
{
    string ibsn;
    string name;
    double price;
}Book;

const int MaxSize = 1010;
Book books[MaxSize]; // Defines the order table for the Book type

// Double pointer algorithm is used for de duplication
void Unique(Book books[], int &n) // Note that the former parameter passes an address, and the latter parameter passes a reference, so they all belong to address passing
{
    int length = 0;
    int j = 0;
    for (int i = 0; i < n; i ++)
        if (!i || books[i].ibsn != books[i - 1].ibsn) // If it is not the first word and different from the previous one, we will add it to the new sequence table
        {
            books[j ++] = books[i];
            length ++;
        }
    
    n = length;

}

int main()
{
    int n;
    cin >> n;
    
    for (int i = 0; i < n; i ++) // 1. Create sequence table
        cin >> books[i].ibsn >> books[i].name >> books[i].price;

    // 2. Weight removal
    Unique(books, n); // Pass in the sequence table to be deleted and the length of the sequence table
    
    cout << n << endl;
    for (int i = 0; i < n; i ++)
    {
        cout << books[i].ibsn << " " << books[i].name << " ";
        printf("%.2lf\n", books[i].price);
    }
    
    return 0;
}

Tags: data structure

Posted on Sun, 31 Oct 2021 05:05:33 -0400 by renegade888