Run ID | 作者 | 问题 | 语言 | 测评结果 | Time | Memory | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|
3246 | 邓小龙 | 单价最高的书 | C++ | Accepted | 0 MS | 272 KB | 982 | 2024-04-26 20:37:33 |
# include<iostream> using namespace std; struct Book{//定义结构体,通常结构体的名字首字母大写 int id;//书的编号 string name;//书的名字 int qty;//书的数量 int price_tot;//书的总价格 int unit_price(){//结构体函数,返回值是总价/数量 return this->price_tot/this->qty; } }; int n;//书的数量 Book book[101];//定义一个书的数组,存储最多101本书的信息 int main(){ cin>>n; struct Book maxn;//定义一个Book类型的结构体maxn,用于存储最高单价的书 //maxn.unit_price()=0; for(int i=1;i<=n;i++){ cin>>book[i].id>>book[i].name>>book[i].qty>>book[i].price_tot; if(i==1)maxn=book[i]; if(maxn.unit_price()<book[i].unit_price()) maxn=book[i];//结构体是可以整体赋值的 } cout<<maxn.id<<" "<<maxn.name<<" "<<maxn.qty <<" "<<maxn.price_tot<<" "<<maxn.unit_price()<<endl; return 0; }