天梯赛L1 题解

2023-04-20,,

L1-001 Hello World (5 分)

这道超级简单的题目没有任何输入。

你只需要在一行中输出著名短句“Hello World!”就可以了。

AC代码:(直接输出记性)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=;
int main()
{
printf("Hello World!\n");
return ;
}

L1-002 打印沙漏 (20 分)

本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印

*****
***
*
***
*****

所谓“沙漏形状”,是指每行输出奇数个符号;各行符号中心对齐;相邻两行符号数差2;符号数先从大到小顺序递减到1,再从小到大顺序递增;首尾符号数相等。

给定任意N个符号,不一定能正好组成一个沙漏。要求打印出的沙漏能用掉尽可能多的符号。

输入样例:

19 *

输出样例:

*****
***
*
***
*****
2 解法:我们先求出共有多少层,然后根据层数输出。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=;
int n;
char c;
int main()
{
scanf("%d %c",&n,&c);
int sum=,i=;//i代表几层,sum代表沙漏需要的字符个数
while(sum<=n)
{
sum+=*(*(i+)-);
if(sum<=n)
i++;
}
// cout<<i<<endl;
for(int j=;j<i;j++)
{
for(int k=;k<j;k++)
printf(" ");
for(int k=;k<(*(i-j)-);k++)
printf("%c",c);
printf("\n");
}
for(int j=;j<=i;j++)
{
for(int k=;k<i-j;k++)
printf(" ");
for(int k=;k<(*j-);k++)
printf("%c",c);
printf("\n");
}
printf("%d",n-(sum-*(*(i+)-)));
return ;
}

L1-003 个位数统计 (15 分)

给定一个 k 位整数 1 (0, ,, d​k−1​​>0),请编写程序统计每种不同的个位数字出现的次数。例如:给定 0,则有 2 个 0,3 个 1,和 1 个 3。

输入样例:

100311

输出样例:

0:2
1:3
3:1
解法:用数组模拟,a[i] 表示数字 i 出现几次
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=;
string s;
int a[];
int main()
{
memset(a,,sizeof(a));
cin>>s;
int len=s.length();
for(int i=;i<len;i++)
{
a[s[i]-'']++;
}
for(int i=;i<;i++)
{
if(a[i]!=)
printf("%d:%d\n",i,a[i]);
}
return ;
}

L1-004 计算摄氏温度 (5 分)

给定一个华氏温度F,本题要求编写程序,计算对应的摄氏温度C。计算公式:2。题目保证输入与输出均在整型范围内。

输入样例:

150

输出样例:

Celsius = 65
解法:简单数学计算
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=;
int F;
int main()
{
cin>>F;
cout<<"Celsius = "<<*(F-)/<<endl;
return ;
}

L1-005 考试座位号 (15 分)

每个 PAT 考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位。正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座位就座。但有些考生迟到了,试机已经结束,他们只能拿着领到的试机座位号码求助于你,从后台查出他们的考试座位号码。

输入样例:

4
3310120150912233 2 4
3310120150912119 4 1
3310120150912126 1 3
3310120150912002 3 2
2
3 4

输出样例:

3310120150912002 2
3310120150912119 1
解法:用map 存储,然后在map中对应输出就行了
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int maxn=;
int n,m;
map<int,string>ma1;
map<int,int>ma2;
int main()
{
string s;int a,b;
cin>>n;
for(int i=;i<=n;i++)
{
cin>>s>>a>>b;
ma1[a]=s;
ma2[a]=b;
}
cin>>m;
int x;
for(int i=;i<=m;i++)
{
cin>>x;
cout<<ma1[x]<<" "<<ma2[x]<<endl;
}
return ;
}

L1-006 连续因子 (20 分)

一个正整数 N 的因子中可能存在若干连续的数字。例如 630 可以分解为 3×5×6×7,其中 5、6、7 就是 3 个连续的数字。给定任一正整数 N,要求编写程序求出最长连续因子的个数,并输出最小的连续因子序列。

输入样例:

630

输出样例:

3
5*6*7
解法:
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int maxn=;
int n,N;
map<int,string>ma;
int main()
{
scanf("%d",&N);
int n=sqrt(N);
LL sum;
int i,j;
for(int len=;len>=;len--)
{
for(i=;i<=n;i++)
{
sum=;
for(j=i;j<=len-+i;j++)
{
sum*=j;
if(sum>N)
break;
}
if(N%sum==)
{
printf("%d\n%d",len,i);
for(int k=i+;k<j;k++)
printf("*%d",k);
printf("\n");
return ;
}
}
}
printf("1\n%d\n",N);
return ;
}

L1-007 念数字 (10 分)

输入一个整数,输出每个数字对应的拼音。当整数为负数时,先输出fu字。十个数字对应的拼音如下:

0: ling
1: yi
2: er
3: san
4: si
5: wu
6: liu
7: qi
8: ba
9: jiu

输入样例:

-600

输出样例:

fu liu ling ling
解法:利用map存储再输出就可以了。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int maxn=;
int n,m;
map<int,string>ma;
int main()
{
ma[]="ling";
ma[]="yi";
ma[]="er";
ma[]="san";
ma[]="si";
ma[]="wu";
ma[]="liu";
ma[]="qi";
ma[]="ba";
ma[]="jiu";
string s;
cin>>s;
int len=s.length();
int i=;
if(s[]=='-')
{
cout<<"fu ";
i++;
}
for(;i<len-;i++)
{
cout<<ma[s[i]-'']<<" ";
}
cout<<ma[s[i]-''];
cout<<endl;
return ;
}

L1-008 求整数段和 (10 分)

给定两个整数A和B,输出从A到B的所有整数以及这些数的和。

输入样例:

-3 8

输出样例:

   -3   -2   -1    0    1
2 3 4 5 6
7 8
Sum = 30
解法:模拟即可
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int maxn=;
int n,m;
int main()
{
int a,b;
scanf("%d %d",&a,&b);
int sum=,t=;
for(int i=a;i<=b;i++)
{
sum+=i;
printf("%5d",i);
t++;
if(t%==&&i!=b)
{
printf("\n");
t=;
}
}
printf("\n");
printf("Sum = %d\n",sum);
return ;
}

L1-010 比较大小 (10 分)

本题要求将输入的任意3个整数从小到大输出。

输入样例:

4 2 8

输出样例:

2->4->8
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int maxn=;
int n,m;
int a[];
int main()
{
for(int i=;i<;i++)
scanf("%d",&a[i]);
sort(a,a+);
for(int i=;i<;i++)
printf("%d->",a[i]);
printf("%d\n",a[]);
return ;
}

L1-011 A-B (20 分)

本题要求你计算A−B。不过麻烦的是,A和B都是字符串 —— 即从字符串A中把字符串B所包含的字符全删掉,剩下的字符组成的就是字符串A−B。

输入样例:

I love GPLT!  It's a fun game!
aeiou

输出样例:

I lv GPLT!  It's  fn gm!
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int maxn=;
int n,m;
string s1,s2;
set<char>s;
int main()
{
getline(cin,s1);
getline(cin,s2);
int len=s2.length();
for(int i=;i<len;i++)
s.insert(s2[i]);
len=s1.length();
for(int i=;i<len;i++)
{
if(s.count(s1[i])==)
cout<<s1[i];
}
cout<<endl;
return ;
}

L1-012 计算指数 (5 分)

真的没骗你,这道才是简单题 —— 对任意给定的不超过 10 的正整数 n,要求你输出 2​n​​。不难吧?

输入样例:

5

输出样例:

2^5 = 32
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int maxn=;
int n,m;
int main()
{
scanf("%d",&n);
int t=pow(,n);
printf("2^%d = %d",n,t);
return ;
}

L1-013 计算阶乘和 (10 分)

对于给定的正整数N,需要你计算 S=1!+2!+3!+...+N!

输入样例:

3

输出样例:

9
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int maxn=;
int n,m;
int a[];
int main()
{
a[]=;
a[]=;
for(int i=;i<=;i++)
a[i]=i*a[i-];
scanf("%d",&n);
int ans=;
for(int i=;i<=n;i++)
ans+=a[i];
printf("%d\n",ans);
return ;
}

L1-014 简单题 (5 分)

这次真的没骗你 —— 这道超级简单的题目没有任何输入。

输入样例:


输出样例:

This is a simple problem.

你只需要在一行中输出事实:This is a simple problem. 就可以了。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int maxn=;
int n,m;
int a[];
int main()
{
printf("This is a simple problem.\n");
return ;
}

L1-015 跟奥巴马一起画方块 (15 分)

美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统。2014年底,为庆祝“计算机科学教育周”正式启动,奥巴马编写了很简单的计算机代码:

在屏幕上画一个正方形。现在你也跟他一起画吧!

输入样例:

10 a

输出样例:

aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
aaaaaaaaaa
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int maxn=;
int n,m;
char s;
int main()
{
scanf("%d %c",&n,&s);
for(int i=;i<=(int)(n/2.0+0.5);i++)
{
for(int j=;j<=n;j++)
printf("%c",s);
printf("\n");
}
return ;
}

L1-016 查验身份证 (15 分)

一个合法的身份证号码由17位地区、日期编号和顺序编号加1位校验码组成。校验码的计算规则如下:

首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};然后将计算的和对11取模得到值Z;最后按照以下关系对应Z值与校验码M的值:

Z:0 1 2 3 4 5 6 7 8 9 10
M:1 0 X 9 8 7 6 5 4 3 2

现在给定一些身份证号码,请你验证校验码的有效性,并输出有问题的号码。

输入样例1:

4
320124198808240056
12010X198901011234
110108196711301866
37070419881216001X

输出样例1:

12010X198901011234
110108196711301866
37070419881216001X
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int maxn=;
int n,m;
char s[maxn];
int h[]={,,,,,,,,,,,,,,,,};
char c[]={'','','X','','','','','','','',''};
int main()
{
bool flag1=false;
scanf("%d",&n);
while(n--)
{
scanf("%s",s);
int sum=;
bool flag2=false;
for(int i=;i<;i++)
{
if(s[i]>=''&&s[i]<='')
sum+=((s[i]-'')*h[i]);
else
{
flag2=true;
break;
}
}
// cout<<sum%11<<endl;
if(flag2 || (s[]!=c[sum%]))
{
printf("%s\n",s);
flag1=true;
}
}
if(flag1==false)
printf("All passed\n");
return ;
}

L1-017 到底有多二 (15 分)

一个整数“犯二的程度”定义为该数字中包含2的个数与其位数的比值。如果这个数是负数,则程度增加0.5倍;如果还是个偶数,则再增加1倍。例如数字-13142223336是个11位数,

其中有3个2,并且是负数,也是偶数,则它的犯二程度计算为:3,约为81.82%。本题就请你计算一个给定整数到底有多二。

输入样例:

-13142223336

输出样例:

81.82%
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int maxn=;
int n,m;
string s;
int main()
{
double ans=1.0;
cin>>s;
int len=s.length();
int i=;int cnt2=;
if((s[len-]-'')%==)
ans*=;
if(s[i]=='-')
{
ans*=1.5;
i++;
len--;
}
for(;i<len;i++)
{
if(s[i]=='')
cnt2++;
}
// cout<<cnt2<<" "<<len<<endl;
// double temp=cnt2*1.0/len;
// cout<<temp<<endl;
ans=ans*(cnt2*1.0/len);
printf("%.2f",ans*);
cout<<"%"<<endl;
return ;
}

L1-018 大笨钟 (10 分)

微博上有个自称“大笨钟V”的家伙,每天敲钟催促码农们爱惜身体早点睡觉。不过由于笨钟自己作息也不是很规律,所以敲钟并不定时。一般敲钟的点数是根据敲钟时间而定的,如果正好在某个整点敲,那么“当”数就等于那个整点数;如果过了整点,就敲下一个整点数。另外,虽然一天有24小时,钟却是只在后半天敲1~12下。例如在23:00敲钟,就是“当当当当当当当当当当当”,而到了23:01就会是“当当当当当当当当当当当当”。在午夜00:00到中午12:00期间(端点时间包括在内),笨钟是不敲的。

下面就请你写个程序,根据当前时间替大笨钟敲钟。

输入样例1:

19:05

输出样例1:

DangDangDangDangDangDangDangDang
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int maxn=;
int n,m;
string s;
int main()
{
int h,m;
scanf("%d:%d",&h,&m);
if((h==&&m>)||(h>&&h<))
{
h-=;
if(m>)
h++;
for(int i=;i<h;i++)
printf("Dang");
printf("\n");
}
else
{
if(h==)
h=;
printf("Only %02d:%02d. Too early to Dang.\n",h,m);
}
return ;
}

L1-019 谁先倒 (15 分)

划拳是古老中国酒文化的一个有趣的组成部分。酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字。如果谁比划出的数字正好等于两人喊出的数字之和,谁就输了,输家罚一杯酒。两人同赢或两人同输则继续下一轮,直到唯一的赢家出现。

下面给出甲、乙两人的酒量(最多能喝多少杯不倒)和划拳记录,请你判断两个人谁先倒。

输入样例:

1 1
6
8 10 9 12
5 10 5 10
3 8 5 12
12 18 1 13
4 16 12 15
15 1 1 16

输出样例:

A
1
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int maxn=;
int x,y;
int n;
int main()
{
scanf("%d %d",&x,&y);
scanf("%d",&n);
int a[maxn];
int b[maxn];
int c[maxn];
int d[maxn];
int cnt1=,cnt2=;
for(int i=;i<=n;i++)
scanf("%d %d %d %d",&a[i],&b[i],&c[i],&d[i]);
bool flag1=false,flag2=false;
for(int i=;i<=n;i++)
{
if(b[i]==a[i]+c[i]&&d[i]!=a[i]+c[i])
cnt1++;
if(d[i]==a[i]+c[i]&&b[i]!=a[i]+c[i])
cnt2++;
if(cnt1>x)
{
flag1=true;
break;
}
if(cnt2>y)
{
flag2=true;
break;
}
}
if(flag1)
printf("A\n%d\n",cnt2);
if(flag2)
printf("B\n%d\n",cnt1);
return ;
}

L1-021 重要的话说三遍 (5 分)

这道超级简单的题目没有任何输入。

你只需要把这句很重要的话 —— “I'm gonna WIN!”——连续输出三遍就可以了。

注意每遍占一行,除了每行的回车不能有任何多余字符。

输入样例:


输出样例:

I'm gonna WIN!
I'm gonna WIN!
I'm gonna WIN!
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int maxn=;
int n,m;
int main()
{
printf("I'm gonna WIN!\n");
printf("I'm gonna WIN!\n");
printf("I'm gonna WIN!\n");
return ;
}

L1-022 奇偶分家 (10 分)

给定N个正整数,请统计奇数和偶数各有多少个?

输入样例:

9
88 74 101 26 15 0 34 22 77

输出样例:

3 6
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int maxn=;
int n,m;
int main()
{
scanf("%d",&n);
int cnt1=,cnt2=;
while(n--)
{
int x;
scanf("%d",&x);
if(x%)
cnt1++;
else
cnt2++;
}
printf("%d %d\n",cnt1,cnt2);
return ;
}

L1-023 输出GPLT (20 分)

给定一个长度不超过10000的、仅由英文字母构成的字符串。请将字符重新调整顺序,按GPLTGPLT....这样的顺序输出,并忽略其它字符。当然,四种字符(不区分大小写)的个数不一定是一样多的,

若某种字符已经输出完,则余下的字符仍按GPLT的顺序打印,直到所有字符都被输出。

输入样例:

pcTclnGloRgLrtLhgljkLhGFauPewSKgt

输出样例:

GPLTGPLTGLTGLGLL
AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int maxn=10005;
int n,m;
int main()
{
string s;
cin>>s;
int len=s.length();
int cnt1=0,cnt2=0,cnt3=0,cnt4=0;
for(int i=0;i<len;i++)
{
if(s[i]=='G'||s[i]=='g')
cnt1++;
if(s[i]=='P'||s[i]=='p')
cnt2++;
if(s[i]=='L'||s[i]=='l')
cnt3++;
if(s[i]=='T'||s[i]=='t')
cnt4++;
}
while(cnt1>0 || cnt2>0 || cnt3>0 ||cnt4>0)
{
if(cnt1>0)
{
printf("G");
cnt1--;
}
if(cnt2>0)
{
printf("P");
cnt2--;
}
if(cnt3>0)
{
printf("L");
cnt3--;
}
if(cnt4>0)
{
printf("T");
cnt4--;
}
}
printf("\n");
return 0;
}

L1-024 后天 (5 分)

如果今天是星期三,后天就是星期五;如果今天是星期六,后天就是星期一。我们用数字1到7对应星期一到星期日。给定某一天,请你输出那天的“后天”是星期几

输入样例:

3

输出样例:

5
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int maxn=;
int n,m;
int main()
{
int n;
scanf("%d",&n);
n=n+;
if(n<=)
printf("%d\n",n);
else
printf("%d\n",n-);
return ;
}

L1-026 I Love GPLT (5 分)

这道超级简单的题目没有任何输入。

你只需要把这句很重要的话 —— “I Love GPLT”——竖着输出就可以了。

所谓“竖着输出”,是指每个字符占一行(包括空格),即每行只能有1个字符和回车。

输入样例:


输出样例:

I

L
o
v
e G
P
L
T
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int maxn=;
int n,m;
int main()
{
printf("I\n");
printf(" \n");
printf("L\n");
printf("o\n");
printf("v\n");
printf("e\n");
printf(" \n");
printf("G\n");
printf("P\n");
printf("L\n");
printf("T");
return ;
}

L1-027 出租 (20 分)

下面是新浪微博上曾经很火的一张图:

一时间网上一片求救声,急问这个怎么破。其实这段代码很简单,index数组就是arr数组的下标,index[0]=2 对应 arr[2]=1index[1]=0 对应 arr[0]=8index[2]=3 对应 arr[3]=0,以此类推…… 很容易得到电话号码是18013820100

本题要求你编写一个程序,为任何一个电话号码生成这段代码 —— 事实上,只要生成最前面两行就可以了,后面内容是不变的。

输入样例:

18013820100

输出样例:

int[] arr = new int[]{8,3,2,1,0};
int[] index = new int[]{3,0,4,3,1,0,2,4,3,4,4};

AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=;
int n,m;
char a[maxn];
int arr[maxn];
int indexx[maxn];
int main()
{
scanf("%s",a);
int k=;
for(int i=;i>=;i--)
{
bool flag=false;
for(int j=;j<;j++)
{
if(i==a[j]-'')
{
arr[k]=i;
indexx[j]=k;
flag=true;
}
}
if(flag)
k++;
}
// cout<<1<<endl;
printf("int[] arr = new int[]{");
for(int i=;i<k;i++)
{
printf("%d",arr[i]);
if(i!=k-)
printf(",");
}
printf("};\n"); printf("int[] index = new int[]{");
for(int i=;i<;i++)
{
printf("%d",indexx[i]);
if(i!=)
printf(",");
}
printf("};\n");
return ;
}

天梯赛L1 题解的相关教程结束。

《天梯赛L1 题解.doc》

下载本文的Word格式文档,以方便收藏与打印。