团体程序设计天梯赛--15分题
Posted 星河欲转。
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了团体程序设计天梯赛--15分题相关的知识,希望对你有一定的参考价值。
L1-003 个位数统计
#include<bits/stdc++.h>
using namespace std;
int main()
string s;
int i;
map<int,int>a;
cin>>s;
for(i=0;i<s.size();i++)
a[s[i]-'0']++;
for(i=0;i<10;i++)if(a[i])cout<<i<<":"<<a[i]<<endl;
return 0;
L1-005 考试座位号
#include<bits/stdc++.h>
using namespace std;
struct xx
string s;
int a,b;
p[1005];
int main()
int n,m,x,i;
cin>>n;
for(i=0;i<n;i++)cin>>p[i].s>>p[i].a>>p[i].b;
cin>>m;
while(m--)
cin>>x;
for(i=0;i<n;i++)
if(x==p[i].a)cout<<p[i].s<<' '<<p[i].b<<endl;
return 0;
L1-015 跟奥巴马一起画方块
#include<bits/stdc++.h>
using namespace std;
int main()
int n,i,x;char c;
cin>>n>>c;
if(n%2==0)x=n/2;
else x=n/2+1;
for(i=0;i<x;i++)
for(int j=0;j<n;j++)
cout<<c;
cout<<endl;
return 0;
L1-016 查验身份证
#include<bits/stdc++.h>
using namespace std;
int main()
int n,sum,i,x=0;
string s;
int a[20]=7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2;
char b[15]='1','0','X','9','8','7','6','5','4','3','2';
cin>>n;
while(n--)
sum=0;
cin>>s;
for(i=0;i<s.size()-1;i++)
sum+=(s[i]-'0')*a[i];
sum%=11;
if(b[sum]!=s[s.size()-1])cout<<s<<endl;x=1;
if(x==0)cout<<"All passed";
return 0;
L1-017 到底有多二
#include<bits/stdc++.h>
using namespace std;
int main()
string s;
int i,a=0,b=0,c=0,d=0;
cin>>s;
if(s[0]=='-')a=1;
if((s[s.size()-1]-'0')%2==0)b=1;
for(i=0;i<s.size();i++)if(s[i]=='2')c++;
d=s.size();
if(a)d-=1;
double x=0;
x=c*1.0/d;
if(a==1&&b==0)x*=1.5;
if(a==0&&b==1)x*=2.0;
if(a==1&&b==1)x*=3.0;
printf("%.2f%%",x*100);
return 0;
L1-019 谁先倒
#include<bits/stdc++.h>
using namespace std;
int main()
int x,y,n,a,b,c,d,x1=0,y1=0;
cin>>x>>y;
cin>>n;
while(n--)
cin>>a>>b>>c>>d;
if(b==(a+c)&&b!=d)x1++;
if(d==(a+c)&&d!=b)y1++;
if(x1>x)cout<<"A\\n"<<y1;break;
if(y1>y)cout<<"B\\n"<<x1;break;
return 0;
L1-025 正整数A+B
#include<bits/stdc++.h>
using namespace std;
int main()
string a,b,c;
int x=0,y=0,s1=0,s2=0;
cin>>a>>b;
if(cin>>c)y=1;
for(int i=0;i<a.size();i++)
if(!isdigit(a[i]))x=1;
else s1=s1*10+(a[i]-'0');
for(int i=0;i<b.size();i++)
if(!isdigit(b[i]))y=1;
else s2=s2*10+(b[i]-'0');
if(s1<1||s1>1000)x=1;
if(s2<1||s2>1000)y=1;
if(x==1)cout<<"?";
else cout<<a;
cout<<" + ";
if(y==1)cout<<"?";
else cout<<b;
cout<<" = ";
if(x||y)cout<<"?";
else cout<<s1+s2;
return 0;
L1-030 一帮一
#include<bits/stdc++.h>
using namespace std;
int main()
int n,i,a[55],f[55]=0;string b[55];
cin>>n;
for(i=1;i<=n;i++)cin>>a[i]>>b[i];
int x=n/2;
for(i=1;i<=x;i++)
for(int j=n;j>x;j--)
if(((a[i]==0&&a[j]==1)||(a[i]==1&&a[j]==0))&&!f[i]&&!f[j])
cout<<b[i]<<' '<<b[j]<<endl;
f[i]=1;f[j]=1;
return 0;
L1-033 出生年
#include<bits/stdc++.h>
using namespace std;
int main()
int y,n,x=0,b=0,c,d,i,j;map<int,int>a;
cin>>y>>n;
for(i=y;;i++)
c=4;b=0;d=i;
while(c--)
a[d%10]++;
d/=10;
for(j=0;j<10;j++)
if(a[j])b++;
if(b==n)printf("%d %04d",x,x+y);break;
else x++;a.clear();
return 0;
L1-035 情人节
#include<bits/stdc++.h>
using namespace std;
int main()
string s,a,b;int x=0;
while(cin>>s&&s!=".")
x++;
if(x==2)a=s;
if(x==14)b=s;
if(x>=14)cout<<a<<" and "<<b<<" are inviting you to dinner...";
else if(x>=2&&x<14)cout<<a<<" is the only one for you...";
else cout<<"Momo... No one is for you ...";
return 0;
L1-044 稳赢
#include<bits/stdc++.h>
using namespace std;
int main()
int k,x=0;
string s;
cin>>k;
while(cin>>s)
if(s=="End")break;
x++;
if(x%(k+1)==0)cout<<s<<endl;
else if(s=="JianDao")cout<<"ChuiZi\\n";
else if(s=="ChuiZi")cout<<"Bu\\n";
else cout<<"JianDao\\n";
return 0;
L1-048 矩阵A乘以B
#include<bits/stdc++.h>
using namespace std;
int main()
int a,b,x,y,i,j,k,m[105][105],n[105][105],c[105][105],d;
cin>>a>>b;
for(i=1;i<=a;i++)
for(j=1;j<=b;j++)
cin>>m[i][j];
cin>>x>>y;
for(i=1;i<=x;i++)
for(j=1;j<=y;j++)
cin>>n[i][j];
if(b==x)
cout<<a<<' '<<y<<endl;
for(i=1;i<=a;i++)
for(j=1;j<=y;j++)
d=0;
for(k=1;k<=x;k++)
d+=m[i][k]*n[k][j];
c[i][j]=d;
for(i=1;i<=a;i++)
for(j=1;j<=y;j++)
if(j!=1)cout<<' ';
cout<<c[i][j];
cout<<endl;
else printf("Error: %d != %d",b,x);
return 0;
L1-050 倒数第N个字符串
#include<bits/stdc++.h>
using namespace std;
int main()
int l,n,a[10],i,m;
cin>>l>>n;
m=pow(26,l)-n;
for(i=1;i<=l;i++)a[i]=m%26;m/=26;
for(i=l;i>=1;i--)
cout<<char(a[i]+97);
return 0;
L1-054 福到了
#include<bits/stdc++.h>
using namespace std;
int main()
int n,i,j,k=0;char c;string a[105];char b[105][105];
cin>>c>>n;
getchar();
for(i=0;i<n;i++)getline(cin,a[i]);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(a[n-i-1][n-j-1]!=' ')a[n-i-1][n-j-1]=c;b[i][j]=c;
else
b[i][j]=' ';
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(a[i][j]!=b[i][j])k=1;
if(k==0)cout<<"bu yong dao le\\n";
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cout<<b[i][j];
cout<<endl;
return 0;
L1-058 6翻了
#include<bits/stdc++.h>
using namespace std;
int main()
string s;
int i,j,x;
getline(cin,s);
for(i=0;i<s.size();i++)
x=0;
if(s[i]!='6')cout<<s[i];
else
for(j=i;j<s.size();j++)
x++;
if(s[j+1]!='6'||s[j+1]=='\\0')i=i+x-1;break;
if(x<=3)while(x--)cout<<"6";
else if(x>3&&x<10)cout<<"9";
else cout<<"27";
return 0;
L1-062 幸运彩票
#include<bits/stdc++.h>
using namespace std;
int main()
int n,i,x,y;string s;
cin>>n;
while(n--)
x=0,y=0;
cin>>s;
for(i=0;i<6;i++)
if(i<3)
x+=s[i]-'0';
else y+=s[i]-'0';
if(x==y)cout<<"You are lucky!\\n";
else cout<<"Wish you good luck.\\n";
return 0;
L1-069 胎压监测
#include<bits/stdc++.h>
using namespace std;
int main()
int a[5],b[5],c=0,max=0,i,x,y,z;
for(i=1;i<=4;i++)cin>>a[i];if(a[i]>max)max=a[i];
cin>>x>>y;
for(i=1;i<=4;i++)
b[i]=max-a[i];
if(b[i]>y||a[i]<x)c++;z=i;
if(c==0)cout<<"Normal";
else if(c==1)printf("Warning: please check #%d!",z);
else cout<<"Warning: please check all the tires!";
return 0;
L1-070 吃火锅
#include<bits/stdc++.h>
using namespace std;
int main()
int x=0,y=0,z=0;string s;
while(getline(cin,s)&&s!=".")
x++;
if(s.find("chi1 huo3 guo1")!=-1&&y==0)y=x;
if(s.find("chi1 huo3 guo1")!=-1)z++;
cout<<x<<endl;
if(y) cout<<y<<' '<<z;
else cout<<"-_-#";
return 0;
L1-077 大笨钟的心情
#include<bits/stdc++.h>
using namespace std;
int main()
int a[30],i,x;
for(i=0;i<24;i++)cin>>a[i];
while(cin>>x)
if(x<0||x>23)break;
if(a[x]>50)cout<<a[x]<<' '<<"Yes\\n";
else cout<<a[x]<<' '<<"No\\n";
return 0;
L1-078 吉老师的回归
#include<bits/stdc++.h>
using namespace std;
int main()
int n,m;string s;
cin>>n>>m;
getchar();
while(n--)
getline(cin,s);
if(s.find("qiandao")!=-1||s.find("easy")!=-1);
else m--;
if(m==-1)cout<<s;break;
if(m>=0)cout<<"Wo AK le";
return 0;
L1-085 试试手气
#include<bits/stdc++.h>
using namespace std;
int main()
int a[10],i,n;
for(i=0;i<6;i++)cin>>a[i];
cin>>n;
for(i=0;i<6;i++)
if(a[i]<7-n)a[i]=7-n;
else a[i]=6-n;
if(i)cout<<' ';
cout<<a[i];
return 0;
L1-086 斯德哥尔摩火车上的题
#include<bits/stdc++.h>
using namespace std;
int main()
string s1,s2,a,b;int i;
cin>>a>>b;
for(i=1;i<a.size();i++)
if(a[i]%2==a[i-1]%2)
s1+=max(a[i],a[i-1]);
for(i=1;i<b.size();i++)
if(b[i]%2==b[i-1]%2)
s2+=max(b[i],b[i-1]);
if(s1==s2)cout<<s1;
else cout<<s1<<endl<<s2;
return 0;
L4-103 就不告诉你
#include<bits/stdc++.h>
using namespace std;
int main()
int a,b,k=0;
cin>>a>>b;
string s=to_string(a*b);
for(int i=s.size()-1;i>=0;i--)
if(s[i]!='0')k=1;
if(s[i]!='0'||k)cout<<s[i];
return 0;
L4-104 Wifi密码
#include<bits/stdc++.h>
using namespace std;
int main()
int n,i;string s;
cin>>n;
getchar();
while(n--)
getline(cin,s);
for(i=0;i<s.size();i++)
if(s[i]=='T')
if(s[i-2]=='A')cout<<1;
if(s[i-2]=='B')cout<<2;
if(s[i-2]=='C')cout<<3;
if(s[i-2]=='D')cout<<4;
break;
return 0;
L4-106 判断题
#include<bits/stdc++.h>
using namespace std;
int main()
int n,m,s,a[105],b[105],x;
cin>>n>>m;
for(int i=0;i<m;i++)cin>>a[i];
for(int i=0;i<m;i++)cin>>b[i];
while(n--)
s=0;
for(int i=0;i<m;i++)
cin>>x;
if(x==b[i])s+=a[i];
cout<<s<<endl;
return 0;
L4-107 检查密码
#include<bits/stdc++.h>
using namespace std;
int main()
int i,n;string s;
cin>>n;
getchar();
while(n--)
int a=0,b=0,c=0,d=0,e=0;
getline(cin,s);
if(s.size()>=6)a=1;
for(i=0;i<s.size();i++)
if(s[i]>='0'&&s[i]<='9')b=1;
else if((s[i]>='A'&&s[i]<='Z')||(s[i]>='a'&&s[i]<='z'))e=1;
else if(s[i]=='.')c=1;
else d=1;
if(a==0)cout<<"Your password is tai duan le.\\n";
if(a==1&&d==1)cout<<"Your password is tai luan le.\\n";
if(a==1&&d==0&&e==1&&b==0)cout<<"Your password needs shu zi.\\n";
if(a==1&&d==0&&e==0&&b==1)cout<<"Your password needs zi mu.\\n";
if(a==1&&d==0&&e==1&&b==1)cout<<"Your password is wan mei.\\n";
return 0;
L4-112 不变初心数
#include<bits/stdc++.h>
using namespace std;
int main()
int n,i,k,y,a,x,z;string s,s1;
cin>>n;
while(n--)
a=0;y=0;x=0;
cin>>s;
for(i=0;i<s.size();i++)
y+=(s[i]-'0');x=x*10+s[i]-'0';
for(i=2;i<=9;i++)
k=0;
z=x*i;
s1=to_string(z);
for(int j=0;j<s1.size();j++)
k+=(s1[j]-'0');
if(y!=k)a=1;break;
if(a==1)cout<<"NO\\n";
else cout<<y<<endl;
return 0;
L4-116 字母串
#include<bits/stdc++.h>
using namespace std;
int main()
int n,i,x;string s;
cin>>n;
while(n--)
cin>>s;
x=0;
for(i=0;i<s.size()-1;i++)
if(s[i]>='A'&&s[i]<='Z'&&(s[i+1]==s[i]+32||(s[i+1]==s[i]+1&&s[i]!='Z')))continue;
else if(s[i]>='a'&&s[i]<='z'&&(s[i+1]==s[i]-32||(s[i+1]==s[i]-1&&s[i]!='a')))continue;
else x=1;break;
if(x)cout<<"N\\n";
else cout<<"Y\\n";
return 0;
团体程序设计天梯赛-练习集
L2-001 紧急救援 (25 分)
知识点:最短路dij
#include<bits/stdc++.h>
#define debug(x,y) printf("%s = %d\\n",x,y);
typedef long long ll;
using namespace std;
inline int read()
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9')if(ch=='-')w=-1;ch=getchar();
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();//s=(s<<3)+(s<<1)+(ch^48);
return s*w;
const int INF=0x3f3f3f;
const int maxn=1000;
int a[maxn];
int edge[maxn][maxn];
int n,m,s,d;
int dis[maxn];
int vis[maxn];
int cnt[maxn];
int num_a[maxn];//最短路上的最大救援队
int pathnum[maxn];//最短路的数量
int path[maxn];
void dij()
pathnum[s]=1;
dis[s]=0;
vis[s]=1;
num_a[s]=a[s];
for(int i=0;i<n;i++)
dis[i]=edge[s][i];
if(edge[s][i]<=INF&&i!=s)
path[i]=s;
num_a[i]=num_a[s]+a[i];
pathnum[i]=1;
for(int i=1;i<n;i++)
int now=0;
int minn=INF;
for(int j=0;j<n;j++)
if(vis[j]==0&&dis[j]<minn)
now=j;
minn=dis[j];
vis[now]=1;
for(int j=0;j<n;j++)
if(!vis[j])
if(dis[j]==edge[now][j]+minn)
pathnum[j]+=pathnum[now];
if(num_a[j]<num_a[now]+a[j])
num_a[j]=num_a[now]+a[j];
path[j]=now;
else if(dis[j]>edge[now][j]+minn)
dis[j]=edge[now][j]+minn;
num_a[j]=num_a[now]+a[j];
pathnum[j]=pathnum[now];
path[j]=now;
void print(int now)
if(now==s)
return ;
print(path[now]);
cout<<" "<<now;
int main()
// memset(edge,0x3f3f3f,sizeof(edge));
cin>>n>>m>>s>>d;
for(int i=0;i<n;i++)
dis[i]=INF;
for(int j=0;j<n;j++)
edge[i][j]=edge[j][i]=INF;
for(int i=0;i<n;i++)cin>>a[i];
for(int i=1;i<=m;i++)
int u,v,w;
cin>>u>>v>>w;
edge[u][v]=w;
edge[v][u]=w;
dij();
cout<<pathnum[d]<<" "<<num_a[d]<<endl;
cout<<s;
print(d);
L2-002 链表去重 (25 分)
知识点:模拟链表
#include<bits/stdc++.h>
#define debug(x,y) printf("%s = %d\\n",x,y);
typedef long long ll;
using namespace std;
inline int read()
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9')if(ch=='-')w=-1;ch=getchar();
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();//s=(s<<3)+(s<<1)+(ch^48);
return s*w;
const int maxn=2e5+9;
struct node
int add;
int val,next;
link[maxn],b[maxn];
int vis[maxn];
int tot=0;
int main()
int beg,n;
cin>>beg>>n;
for(int i=1;i<=n;i++)
int add,val,nex;
cin>>add>>val>>nex;
link[add].val=val;
link[add].next=nex;
int now=beg;
// printf("----\\n");
while(1)
// printf("now=%d\\n",now);
if(now==-1)
break;
if(vis[abs(link[now].val)]==0)
if(now!=beg)
printf("%05d\\n",now);
vis[abs(link[now].val)]=1;
printf("%05d %d ",now,link[now].val);
// cout<<now<<" "<<link[now].val<<" "<<link[now].next<<endl;
now=link[now].next;
else
b[++tot].next=link[now].next;
b[tot].add=now;
b[tot].val=link[now].val;
now=link[now].next;
printf("-1\\n");
if(tot)
printf("%05d %d ",b[1].add,b[1].val);
for(int i=2;i<=tot;i++)
printf("%05d\\n%05d %d ",b[i].add,b[i].add,b[i].val);
printf("-1\\n");
L2-003 月饼 (25 分)
知识点:贪心
注意要将种类数和需求数都开成double,不然会被卡一个点
#include<bits/stdc++.h>
#define debug(x,y) printf("%s = %d\\n",x,y);
typedef long long ll;
using namespace std;
inline int read()
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9')if(ch=='-')w=-1;ch=getchar();
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();//s=(s<<3)+(s<<1)+(ch^48);
return s*w;
const int maxn=2000;
//int num[maxn];
//int val[maxn];
struct node
double num,val;
double ave;
a[maxn];
bool cmp(node a,node b)
return a.val*b.num>b.val*a.num;
int main()
int n,d;
cin>>n>>d;
for(int i=1;i<=n;i++)cin>>a[i].num;
for(int i=1;i<=n;i++)cin>>a[i].val;
for(int i=1;i<=n;i++)a[i].ave=1.0*a[i].val/a[i].num;
sort(a+1,a+1+n,cmp);
double sum=0;
for(int i=1;i<=n;i++)
// cout<<a[i].ave<<endl;
if(d>=a[i].num)
d-=a[i].num;
sum+=a[i].val;
else
sum+=d*a[i].ave;
break;
printf("%.2f\\n",sum);
L2-004 这是二叉搜索树吗? (25 分)
知识点:考察了前序遍历和后序遍历的应用
#include<bits/stdc++.h>
#define debug(x,y) printf("%s = %d\\n",x,y);
typedef long long ll;
using namespace std;
inline int read()
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9')if(ch=='-')w=-1;ch=getchar();
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();//s=(s<<3)+(s<<1)+(ch^48);
return s*w;
const int maxn=2000;
int pos[maxn];
int iff=0;
vector<int>post;
void getpos(int l,int r)
if(l>r)return ;
int tl=l+1,tr=r;
if(!iff)//正常的二叉搜索树
while(tl<=r&&pos[tl]<pos[l])tl++;//找到第一个大的
while(tr>l&&pos[tr]>=pos[l])tr--; //找到第一个小的
else //镜像的
while(tl<=r&&pos[tl]>=pos[l])tl++;
while(tr>l&&pos[tr]<pos[l])tr--;
if(tl!=tr+1)return ;
getpos(l+1,tr);
getpos(tl,r);
// cout<<pos[l]<<endl;
post.push_back(pos[l]);
int main()
int n;
cin>>n;
for(int i=1;i<=n;i++)cin>>pos[i];
getpos(1,n);
if(post.size()!=n)
iff=1;
post.clear();
getpos(1,n);
if(post.size()!=n)
cout<<"NO"<<endl;
return 0;
cout<<"YES"<<endl;
for(int i=0;i<n;i++)
if(i==0)
printf("%d",post[i]);
else printf(" %d",post[i]);
return 0;
L2-005 集合相似度 (25 分)
知识点:考察对set的应用情况
#include<bits/stdc++.h>
#define debug(x,y) printf("%s = %d\\n",x,y);
typedef long long ll;
using namespace std;
inline int read()
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9')if(ch=='-')w=-1;ch=getchar()以上是关于团体程序设计天梯赛--15分题的主要内容,如果未能解决你的问题,请参考以下文章
团体程序设计天梯赛L1-025 正整数A+B (15分)(易错点和测试点分析)
题解PTA团体程序设计天梯赛L1-033 出生年 (15 分) Go语言|Golang
题解PTA团体程序设计天梯赛L1-030 一帮一 (15 分) Go语言|Golang
题解PTA团体程序设计天梯赛L1-019 谁先倒 (15 分) Go语言 Golang