第一题
笔记📒:
- 我觉得按照条件顺序输出是这道题的难点,我自己也没有想出怎么解决这个问题。
- 看了别人的代码之后我发现,可以在Student这个结构体里再加一个flag标签,根据题目的不同条件,对每一个考生赋予不同的标签,并且标签的数据类型为整形,更重要的是,标签的大小将影响最后考生的排序,比如标签的数字越小,则优先排在前面。这样就可以很好地解决按序输出的问题。
3.比较函数
bool cmp(node a , node b)
{
if(a.flag != b.flag)
return a.flag < b.flag;
else if(a.sum != b.sum)
return a.sum > b.sum;
else if(a.Dscore != b.Dscore)
return a.Dscore > b.Dscore;
else
return a.id < b.id;
}
大佬的代码里的这个cmp函数是关键,依次判断标签、总分、德分、学号是否相同返回相应的学生,我要好好学习👍
4.整理排序
sort(stu,stu+N,cmp);
通过这行代码,可以直接将stu数组按照cmp的比较规则排序,快速便捷!
代码:
#include <bits/stdc++.h>
using namespace std;
struct node
{
int id;
int Dscore;
int Cscore;
int sum;
int flag;
};
bool cmp(node a , node b)
{
if(a.flag != b.flag)
return a.flag < b.flag;
else if(a.sum != b.sum)
return a.sum > b.sum;
else if(a.Dscore != b.Dscore)
return a.Dscore > b.Dscore;
else
return a.id < b.id;
}
int main()
{
int N,L,H;
scanf("%d %d %d",&N,&L,&H);
node stu[N];
int cnt = 0;
for(int i = 0 ; i < N ; i++)
{
scanf("%d %d %d",&stu[i].id,&stu[i].Dscore,&stu[i].Cscore);
stu[i].sum = stu[i].Dscore + stu[i].Cscore;
}
for(int i = 0 ; i < N ; i++)
{
if(stu[i].Cscore >= L && stu[i].Dscore >= L)
{
cnt++;
if(stu[i].Cscore >= H && stu[i].Dscore >= H)
stu[i].flag = 0;
else if(stu[i].Dscore >= H && stu[i].Cscore < H)
stu[i].flag = 1;
else if(stu[i].Dscore < H && stu[i].Cscore < H && stu[i].Dscore >= stu[i].Cscore)
stu[i].flag = 2;
else
stu[i].flag = 3;
}
else
stu[i].flag = 4;
}
sort(stu,stu+N,cmp);
cout << cnt << endl;
for(int i = 0 ; i < cnt ; i++)
{
printf("%d %d %d\n",stu[i].id,stu[i].Dscore,stu[i].Cscore);
}
return 0;
}
第二题
笔记📒:
- 这道题的关键在于将输入的A和B输入为字符数组,然后通过将
a[i] - '0'
来判断该数字是否是是Da,如果是则cnt++,从而计算Pa,最后输出Pa和Pb的和即可。
我的代码:
#include <iostream>
using namespace std;
int main( ){
char a[1000];
char b[1000];
int n1,n2;
int pa = 0;
int pb = 0;
int cnt = 0;
cin>>a>>n1>>b>>n2;
for (int i = 0; i < strlen(a); ++i) {
if (a[i] - '0' == n1){
pa += n1 * pow(10,cnt);
cnt++;
}
}
cnt = 0;
for (int i = 0; i < strlen(b); ++i) {
if (b[i] - '0' == n2){
pb += n2 * pow(10,cnt);
cnt++;
}
}
cout<<pa + pb;
}
第三题
笔记:
模拟手动除法的过程,每次用第一位去除以B,如果得到的商不是0就输出,否则就*10+下一位,直到最后的数为余数~
代码:
#include <iostream>
using namespace std;
int main() {
string s;
int a, t = 0, temp = 0;
cin >> s >> a;
int len = s.length();
t = (s[0] - '0') / a;
if ((t != 0 && len > 1) || len == 1)
cout << t;
temp = (s[0] - '0') % a;
for (int i = 1; i < len; i++) {
t = (temp * 10 + s[i] - '0') / a;
cout << t;
temp = (temp * 10 + s[i] - '0') % a;
}
cout << " " << temp;
return 0;
}