2025年6月15日测试总结
tangruixuan · · 个人记录
第一题 T226001【排序】桶排序(主包得了100分)
虽然题目写的是桶排序,但不会真有人这么老实吧!不会吧!不会吧!(我记得某个姓陈的老师好像就这么老实)我说一遍:题目是死的,人是活的,要懂随机应变。这道题又没说要去重,用桶排序只会增加难度,所以主包直接sort排序。代码是个人都会就直接贴了。
#include<bits/stdc++.h>
using namespace std;
int a[1005];
int main()
{
int n;
cin>>n;
for (int i=1;i<=n;++i) cin>>a[i];
sort(a+1,a+n+1);
for (int i=1;i<=n;++i) cout<<a[i]<<' ';
return 0;
}
第二题 T619879 整理抽屉(主包得了100分)
这道题我看很多人用了map,但真没必要,主包没用map照样满分。(绝对不是主包不会用map)想必用map的思路你们都知道了,那我就来讲讲不用map的思路。
其实只要建立一个string数组,把值按字典排序。再看连起来一样的数的个数最多的就是答案,下面是主包的答案:
#include<bits/stdc++.h>
using namespace std;
string a[1005];
int main()
{
int n,t=0,sum=0;
string ans;
cin>>n;
for (int i=1;i<=n;++i) cin>>a[i];
sort(a+1,a+n+1);
for (int i=2;i<=n;++i)
{
if (a[i]==a[i-1]){
++t;
if (t>sum){
sum=t;
ans=a[i];
}
}
}
cout<<ans;
return 0;
}
第三题 T619884 成绩排序(主包得了100分)
这道题是真没必要讲,一个结构体排序就解决了。就按题目要求,先排分数,如果分数一样,把名字按字典排序排一下就解决了。下面是主包的代码:
#include<bits/stdc++.h>
using namespace std;
struct student{
int score;
string name;
}a[25];
bool comp(student a,student b)
{
if (a.score!=b.score) return a.score>b.score;
return a.name<b.name;
}
int main()
{
int n;
cin>>n;
for (int i=1;i<=n;++i) cin>>a[i].name>>a[i].score;
sort(a+1,a+n+1,comp);
for (int i=1;i<=n;++i) cout<<a[i].name<<' '<<a[i].score<<endl;
return 0;
}
第四题 T619421 陶陶摘苹果(主包得了100分)
先让主包吐槽两句:你个**陶陶没事打游戏去啊!摘什么苹果啊,连苹果都不知道怎样摘得多,真是个出生。
好首先这道题肯定是先按力气排,然后再判断能否摘到,代码直接贴上:
#include<bits/stdc++.h>
using namespace std;
struct apple{
int x;//高度
int y;//力气
}p[5005];
bool comp(apple a,apple b)
{
return a.y<b.y;
}
int main()
{
int n,s,a,b,ans=0;
cin>>n>>s>>a>>b;
int q=a+b;
for (int i=1;i<=n;++i) cin>>p[i].x>>p[i].y;
sort(p+1,p+n+1,comp);
for (int i=1;i<=n;++i)
{
if (q>=p[i].x&&s>=p[i].y)
{
++ans;
s-=p[i].y;
}
}
cout<<ans;
return 0;
}
第五题 T619883 正整数N转换成一个二进制数(主包拿了75分)
主要主包没有加特判啊
#include<bits/stdc++.h>
using namespace std;
int a[1005];
int main()
{
if(n==0) //就是这个特判没写,你们不能学主包啊
cout<<0;
return 0;
}
int n,i=0;
cin>>n;
while(n!=0)
{
a[++i]=n%2;
n/=2;
}
for (int j=i;j>=1;--j) cout<<a[j];
return 0;
}
第六题 T619885【模板】栈(主包拿了10分)
主要还是想给“小孩”点机会懂吗(不是忘记怎么写模版了),这是模版题就直接贴代码了:
#include<bits/stdc++.h>
using namespace std;
string st[2000005];
int main()
{
int t,n,top=0;
cin>>t;
for (int i=1;i<=t;++i)
{
cin>>n;
top=0;
for (int j=1;j<=n;++j)
{
string c;
cin>>c;
if (c=="push"){
string x;
cin>>x;
st[++top]=x;
}
if (c=="pop"){
if (top==0) cout<<"Empty"<<endl;
else --top;
}
if (c=="query"){
if (top==0) cout<<"Anguei!"<<endl;
else cout<<st[top]<<endl;
}
if (c=="size") cout<<top<<endl;
}
}
return 0;
}
第七题 T619886【模板】队列(主包拿了100分)
一道模版题就直接贴代码了:
#include<bits/stdc++.h>
using namespace std;
int que[10005];
int main()
{
int n,head=0,tail=0;
cin>>n;
for (int i=1;i<=n;++i)
{
int q;
cin>>q;
if (q==1){
int x;
cin>>x;
que[++tail]=x;
}
if (q==2){
if (head==tail) cout<<"ERR_CANNOT_POP"<<endl;
else ++head;
}
if (q==3){
if (head==tail) cout<<"ERR_CANNOT_QUERY"<<endl;
else cout<<que[head+1]<<endl;
}
if (q==4) cout<<tail-head<<endl;
}
return 0;
}
第八题 T619887 最大公约数(主包拿了100分)
一道经典的递归题,我们需要知道一个简便计算最大公约数的算法。就拿35和50举例。
35/50=0余35
50/35=1余15
35/15=2余5
15/5=3余0
所以5就是最大公约数
那我们知道这个算法就能很轻松写出下面代码:
#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b){
if (a%b==0) return b;
return gcd(b,a%b);
}
int main()
{
int x,y,z;
cin>>x>>y>>z;
cout<<gcd(gcd(x,y),z);
return 0;
}
第九题 T619888 宇宙总统(主包得了100分)
由于数据太大直接用string,先看长短,然后直按字典排,下面是代码:
#include<bits/stdc++.h>
using namespace std;
struct zt{
int id;
string score;
int s;
}a[25];
bool comp(zt a,zt b)
{
if (a.s!=b.s) return a.s>b.s;
return a.score>b.score;
}
int main()
{
int n;
cin>>n;
for (int i=1;i<=n;++i)
{
cin>>a[i].score;
a[i].s=a[i].score.size();
a[i].id=i;
}
sort(a+1,a+n+1,comp);
cout<<a[1].id<<endl<<a[1].score;
return 0;
}
第十题 T619481 进制转换
由于这道题太简单了,主包懒得写了。另外也是想给我们班的“小孩”一点机会,想让他得个第一,不然我就885分(虽然他还是万年老二)。
好!咱废话不多说,直接上思路。
其实他就是典型的x转10进制,只不过多了个n组数据而已。不会有人连x转10的代码都不知道吧?由于时间问题我就直接把x转10的代码贴上,剩下的部分还不会,我劝你别学编程了,回家种地吧!(绝对不是主包懒,只是想多给你们更多的发挥空间)
#include<bits/stdc++.h>
using namespace std;
int to_num(char c){
if (c<='9') return c-'0';
else return c-'A'+10;
}
int main()
{
int x,n;
string s;
cin>>n>>s;
reverse(s.begin(),s.end());
int len=s.size();
int quan=1;
int ans=0;
for (int i=0;i<len;++i)
{
int x=to_num(s[i]);
ans+=x*quan;
quan*=n;
}
cout<<ans;
return 0;
}
555 遭老师紫菜了,只能重新写了。
那这次我就好好讲了。
首先我们要理解一个东西叫位权
什么是位权呢?我们拿一个十进制数 781391 举例:
| 类型 | 十万位 | 万位 | 千位 | 百位 | 十位 | 个位 |
|---|---|---|---|---|---|---|
| 数字 | 7 | 8 | 1 | 3 | 9 | 1 |
| 位权 | 10^5 | 10^4 | 10^3 | 10^2 | 10^1 | 10^0(也就是1) |
| 计算过程 | 10^5*7 | 10^4*8 | 10^3*1 | 10^2*3 | 10^1*9 | 10^5*1 |
| 实际数值 | 700000 | 80000 | 1000 | 300 | 90 | 1 |
而 781391 就是每一位代表的实际数值之和。
相信大家有思路了吧!第 n 位的位权,就代表这一位上每一个 1 代表的数量。
下面是代码:
#include<bits/stdc++.h>
using namespace std;
long long num[15];//十年OI一场空不开long long见祖宗!
string s;
int main() {
long long n,k;
cin>>n;
while (n--)
{
cin>>k>>s;
long long ans=0;
for (long long i=1;i<=s.size();++i)
{
if (!(s[i]>='0'&&s[i]<='9'))
num[i]=s[i]-'A'+10;
else
num[i]=s[i]-'0';
ans=ans*k+num[i];
}
cout<<ans<<endl;
}
return 0;
}