From 4733376e5db8ed3315a04aa7e4e3050556430b23 Mon Sep 17 00:00:00 2001 From: msksbr Date: Thu, 26 Sep 2024 01:44:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=8C=E6=88=90=E7=AC=AC?= =?UTF-8?q?=E5=9B=9B=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- homework1/CMakeLists.txt | 1 + homework1/test4.cpp | 68 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 homework1/test4.cpp diff --git a/homework1/CMakeLists.txt b/homework1/CMakeLists.txt index 6c67530..2aa2370 100644 --- a/homework1/CMakeLists.txt +++ b/homework1/CMakeLists.txt @@ -7,3 +7,4 @@ add_executable(test1 test1.cpp) add_executable(test2 test2.cpp) add_executable(test3 test3.cpp) +add_executable(test4 test4.cpp) diff --git a/homework1/test4.cpp b/homework1/test4.cpp new file mode 100644 index 0000000..43ffd0a --- /dev/null +++ b/homework1/test4.cpp @@ -0,0 +1,68 @@ +#include +#include + +using namespace std; + +typedef struct { + string id; + string name; + double price; +} book; + +typedef struct { + book books[]; + int number_of_books; +} book_and_number; + +double max_price(book *books, int n) { + double max_price = 0; + for (int i = 0; i < n; i++) { + if (books[i].price > max_price) { + max_price = books[i].price; + } + } + return max_price; +} + +book_and_number max_price_books(book *books, int n) { + double max_price = max_price(books, n); + + book_and_number books_and_number; + book max_price_books[]; + + books_and_number.number_of_books = 0; + for (int i = 0; i < n; i++) { + if (books[i].price == max_price) { + max_price_books[i] = books[i]; + books_and_number.number_of_books++; + } + } + books_and_number.books = &max_price_books; + return books_and_number; +} + +void input(book *books, int n) { + for (int i = 0; i < n; i++) { + cin >> books[i].id >> books[i].name >> books[i].price; + } +} + +void output(book_and_number books_and_number) { + cout << books_and_number.number_of_books << endl; + for (int i = 0; i < books_and_number.number_of_books; i++) { + cout << books_and_number.books[i].id << "\t" << books_and_number.books[i].name << "\t" << fixed << + setprecision(2) << + books_and_number.books[i].price << endl; + } +} + +int main() { + book books[100]; + int n; + cin >> n; + input(books, n); + max_price(books, n); + book_and_number books_and_number = max_price_books(books, n); + output(books_and_number); + return 0; +}