CSDN 编程竞赛二十四期题解

1、计数问题

试计算在区间 1 到 n 的所有整数中,数字 x(0 ≤ x ≤ 9) 共出现了多少次?例如,在 1 到 11 中,即在 1,2,3,4,5,6,7,8,9,10,11 中,数字 1 出现了 4 次。

#include<bits/stdc++.h>
using namespace std;
int main(){
int ans=0,n,x;
scanf("%d%d",&n,&x);
while(n){
int m=n;
while(m){
if(m%10==x)++ans;
m/=10;
}
--n;
}
printf("%d",ans);
}

2、小艺的英文名

小艺酱想给自己起一个英文名字。 小艺酱想要装的自己学识渊博。 所以她想要自己英文名字必须满足: 1.只有字母表中 前k个小写字母。 2.必须是回文串。 3.前k个小写字母每个字母至少出现一次。 小艺酱已经自己完成了部分空余的字母部 分用’?’代替。 请你帮她完成她的英文名字。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int k,n;
char s[maxn];
int main(){
scanf("%d%s",&k,s+1);
n=strlen(s+1);
set<char,greater<char>>c;
for(int i=0;i<k;++i)c.insert(i+'a');
for(int i=1;i<=n;++i)if(s[i]!='?')c.erase(s[i]);
for(int i=n+1>>1;i;--i){
if(s[i]=='?'&&s[n-i+1]!='?')s[i]=s[n-i+1];
if(s[i]!='?'&&s[n-i+1]=='?')s[n-i+1]=s[i];
if(s[i]!=s[n-i+1])return puts("QAQ"),0;
if(s[i]!='?')c.erase(s[i]);
else{
if(c.empty())s[i]=s[n-i+1]='a';
else s[i]=s[n-i+1]=*c.begin(),c.erase(c.begin());
}
}
puts(c.empty()?s+1:"QAQ");
}

3、蛇形矩阵

给你一个整数n,输出n∗n的蛇形矩阵。

#include<bits/stdc++.h>
using namespace std;
const int maxn=110;
int a[maxn][maxn],cnt,dx=-1,dy=1,n,x=1,y=1;
int main(){
scanf("%d",&n);
a[1][1]=cnt=1;
if(n>1)
while(1){
int mx=x+dx,my=y+dy;
if(mx>0&&mx<=n&&my>0&&my<=n)a[x=mx][y=my]=++cnt;
else{
if(x==1&&y==n)++x;
else if(x==n&&y==1)++y;
else if(x==1||x==n)++y;
else if(y==1||y==n)++x;
dx=-dx,dy=-dy;
a[x][y]=++cnt;
}
if(x==n&&y==n)break;
}
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
printf("%d%c",a[i][j]," \n"[j==n]);
}

4、放货物

小明是一名快递员,他现在手上一共有N个快件需要运送。但是货车有限,所以我们希望用最少的货车来进行工作。现在 已知,一辆车的限定额度为最多放置K件货物。此外,小明很不喜欢13这个数字,所以他不希望任何一辆货车中的货物数 量为13。 现在小明想要知道,最少使用多少辆货车能够将这N个快件都放置到货车上。

#include<bits/stdc++.h>
using namespace std;
int main(){
int k,n;
while(~scanf("%d%d",&n,&k))k==13&&--k,printf("%d\n",(n+k-1)/k+(n%k==13));
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

aery_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值