From 39e4029b9c9f1752205b4c5e032a2fdac81ce2cc Mon Sep 17 00:00:00 2001 From: msksbr Date: Thu, 26 Sep 2024 00:45:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E9=AA=8C=E4=BA=8C=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- homework1/test2.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/homework1/test2.cpp b/homework1/test2.cpp index f86d9e6..4d46f15 100644 --- a/homework1/test2.cpp +++ b/homework1/test2.cpp @@ -1,3 +1,42 @@ -// -// Created by 31416 on 24-9-26. -// +#include +#include + +using namespace std; + +typedef struct { + string id; + string name; + double price; +} book; + +void swap(book &a, book &b) { + book temp = a; + a = b; + b = temp; +} + +void sort(book *arr, int size) { + for (int i = 0; i < size - 1; i++) { + for (int j = 0; j < size - 1 - i; j++) { + if (arr[j].price < arr[j + 1].price) { + swap(arr[j], arr[j + 1]); + } + } + } +} + +int main() { + book books[100]; + int size = 0; + for (size = 0; 1; size++) { + cin >> books[size].id >> books[size].name >> books[size].price; + if (books[size].id == "0" && books[size].name == "0" && books[size].price == 0) { + break; + } + } + sort(books, size); + for (int i = 0; i < size; i++) { + cout << books[i].id << " " << books[i].name << " " << fixed << setprecision(2) << books[i].price << endl; + } + return 0; +}