B3952 [GESP202403 一级] 小杨买书

题目描述

小杨同学积攒了一部分零用钱想要用来购买书籍,已知一本书的单价是 1313 元,请根据小杨零用钱的金额,编写程序计算可以购买多少本书,还剩多少零用钱。

输入格式

输入一个正整数 mm,表示小杨拥有的零用钱数。

输出格式

输出包含两行,第一行,购买图书的本数;第二行,剩余的零用钱数。

输入输出样例

输入 

100

输出 

7
9

输入 

199

输出

15
4

说明/提示

数据规模与约定

对全部的测试数据,保证 0 < m < 2000<m<200。

代码如下:

#include<bits/stdc++.h>
using namespace std;
int main(){
	int m;
	cin>>m;
	cout<<m/13<<endl<<m%13;
	return 0;
}

这个程序使用了C++语言,实现了以下功能:

  1. 首先,程序从标准输入中读取一个整数m。
  2. 然后,程序输出m除以13的结果,并换行。
  3. 最后,程序输出m除以13的余数,并换行。

在整个过程中,程序使用了基本的算术运算符来计算和输出结果。

 

在解决成绩排序问题时,尤其是针对 **GESP202403 五级** 的题目要求,需要实现多级排序规则,并且保留原始输入顺序以处理并列排名的情况。可以使用结构体(或类)来封装学生的成绩信息,并通过自定义比较函数实现多级排序逻辑。 ### 数据结构设计 定义一个 `Student` 结构体,用于存储每位学生的成绩信息以及计算出的总分: ```cpp struct Student { int math; // 数学成绩 int chinese; // 语文成绩 int english; // 英语成绩 int total; // 总分 int index; // 输入顺序(用于保留原始索引) // 构造函数 Student(int m, int c, int e, int idx) : math(m), chinese(c), english(e), index(idx) { total = m + c + e; } }; ``` ### 多级排序规则 排序的优先级如下: 1. **总分降序**(总分高的排在前面) 2. **语文与数学成绩之和降序** (总分相同时,语文+数学成绩高的优先) 3. **语文与数学中的最高分降序** (若语数之和也相同,则取语文和数学中的最高分进行比较) 4. **原始输入顺序升序** (若以上条件都相同,则按照输入顺序排序) 对应的比较函数如下: ```cpp bool cmp(const Student& a, const Student& b) { if (a.total != b.total) { return a.total > b.total; } int a_cm = a.chinese + a.math; int b_cm = b.chinese + b.math; if (a_cm != b_cm) { return a_cm > b_cm; } int a_max = max(a.chinese, a.math); int b_max = max(b.chinese, b.math); if (a_max != b_max) { return a_max > b_max; } return a.index < b.index; } ``` ### 完整程序示例 ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Student { int math; int chinese; int english; int total; int index; Student(int m, int c, int e, int idx) : math(m), chinese(c), english(e), index(idx) { total = m + c + e; } }; bool cmp(const Student& a, const Student& b) { if (a.total != b.total) { return a.total > b.total; } int a_cm = a.chinese + a.math; int b_cm = b.chinese + b.math; if (a_cm != b_cm) { return a_cm > b_cm; } int a_max = max(a.chinese, a.math); int b_max = max(b.chinese, b.math); if (a_max != b_max) { return a_max > b_max; } return a.index < b.index; } int main() { int n; cin >> n; vector<Student> students; for (int i = 0; i < n; ++i) { int m, c, e; cin >> m >> c >> e; students.emplace_back(m, c, e, i); } sort(students.begin(), students.end(), cmp); for (const auto& s : students) { cout << s.math << " " << s.chinese << " " << s.english << endl; } return 0; } ``` ### 程序说明 - 使用 `vector<Student>` 存储所有学生对象。 - 在排序前,每个学生对象都记录了原始输入顺序 `index`。 - 使用 `sort` 函数配合自定义比较函数 `cmp` 完成多级排序。 - 输出按排序后的顺序输出各科成绩。 该算法的时间复杂度为 **O(n log n)**,适用于中等规模的数据排序需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值