团体程序设计天梯赛-练习集(一)(string.find()、string.erase()、string.insert()、map容器、L1-002 (*)、L1-005、L1-063、L1-058、L1-054 (*)、L1-003 )
L1-002 打印沙漏 (20分)
https://pintia.cn/problem-sets/994805046380707840/problems/994805145370476544
#include <cstdio> #include <vector> #include <cstring> #include <string> #include <cstdlib> #include <iostream> using namespace std; int main() { vector<int> a; a.push_back(1); int n,s=1,c,p; int i,j,k; char chr; cin>>n; cin>>chr; for(i=3;s+i*2<=n;i+=2) { s+=i*2; a.push_back(i); } c=n-s; p=i-2; for(i=a.size()-1;i>=0;i--) { j=p-a[i]; for(k=0;k<j/2;k++) cout<<" "; for(k=0;k<a[i];k++) cout<<chr; cout<<endl; } for(i=1;i<a.size();i++) { j=p-a[i]; for(k=0;k<j/2;k++) cout<<" "; for(k=0;k<a[i];k++) cout<<chr; cout<<endl; } cout<<c<<endl; return 0; }
L1-005 考试座位号 (15分)
https://pintia.cn/problem-sets/994805046380707840/problems/994805140211482624
法一(结构体):
#include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <string> #include <iostream> using namespace std; struct node{ char admin[20]; int seat1; int seat2; }student[1010]; int main() { int n,m; int findseat; cin>>n; for(int i=0;i<n;i++) { cin>>student[i].admin; cin>>student[i].seat1>>student[i].seat2; } cin>>m; while(m--) { cin>>findseat; for(int i=0;i<n;i++) { if(student[i].seat1==findseat) { cout<<student[i].admin<<" "; cout<<student[i].seat2<<endl; break; } } } return 0; }
法二(map):
#include<iostream> #include<cstdio> #include<algorithm> #include<map> using namespace std; map<int ,pair<long long ,int> > m;//定义一个int(试机座位号)为键,pair<long long(准考证号),int(考试座位号)>作为值的map int main(){ int n; cin>>n; for(int i = 0;i < n; i++){ long long zkz; int sj,ks; cin>>zkz>>sj>>ks; //1.把准考证号和考试座位号赋值给对应的试机座位号 m[sj].first = zkz; m[sj].second = ks; } int xw; cin>>xw; for(int i = 0; i < xw; i++){ int sj; cin>>sj; //2.通过map查找对应试机座位号的准考证号和考试座位号 cout<<m[sj].first<<" "<<m[sj].second<<endl; } return 0; }
L1-063 吃鱼还是吃肉 (10分)
https://pintia.cn/problem-sets/994805046380707840/problems/1111914599412858884
复杂
#include <cstdio> #include <cstring> #include <string> #include <cmath> #include <algorithm> #include <iostream> using namespace std; struct node{ int flag; //性别 int height; //身高 int weight; //体重 }baby[12]; int main() { int n; cin>>n; for(int i=0;i<n;i++) cin>>baby[i].flag>>baby[i].height>>baby[i].weight; for(int i=0;i<n;i++) { if(baby[i].flag==1) //boy { if(baby[i].height<130) { if(baby[i].weight<27) cout<<"duo chi yu! duo chi rou!\n"; else if(baby[i].weight>27) cout<<"duo chi yu! shao chi rou!\n"; else cout<<"duo chi yu! wan mei!\n"; } else if(baby[i].height>130) { if(baby[i].weight<27) cout<<"ni li hai! duo chi rou!\n"; else if(baby[i].weight>27) cout<<"ni li hai! shao chi rou!\n"; else cout<<"ni li hai! wan mei!\n"; } else if(baby[i].height==130) { if(baby[i].weight<27) cout<<"wan mei! duo chi rou!\n"; else if(baby[i].weight>27) cout<<"wan mei! shao chi rou!\n"; else cout<<"wan mei! wan mei!\n"; } } else if(baby[i].flag==0) //girl { if(baby[i].height<129){ if(baby[i].weight<25) cout<<"duo chi yu! duo chi rou!\n"; else if(baby[i].weight>25) cout<<"duo chi yu! shao chi rou!\n"; else cout<<"duo chi yu! wan mei!\n"; } else if(baby[i].height>129){ if(baby[i].weight<25) cout<<"ni li hai! duo chi rou!\n"; else if(baby[i].weight>25) cout<<"ni li hai! shao chi rou!\n"; else cout<<"ni li hai! wan mei!\n"; } else if(baby[i].height==129){ if(baby[i].weight<25) cout<<"wan mei! duo chi rou!\n"; else if(baby[i].weight>25) cout<<"wan mei! shao chi rou!\n"; else cout<<"wan mei! wan mei!\n"; } } } return 0; }
简易
#include <cstdio> #include <iostream> using namespace std; int main() { int n,heigh,weigh,sex; cin>>n; while(n--) { int flag=0; cin>>sex>>heigh>>weigh; if(sex==0) flag=1; if(heigh<130-flag) cout<<"duo chi yu! "; if(heigh==130-flag) cout<<"wan mei! "; if(heigh>130-flag) cout<<"ni li hai! "; if(weigh<27-flag*2) puts("duo chi rou!"); if(weigh==27-flag*2) puts("wan mei!"); if(weigh>27-flag*2) puts("shao chi rou!"); } return 0; }
L1-058 6翻了 (15分)
https://pintia.cn/problem-sets/994805046380707840/problems/1111914599408664577
string类find函数,erase函数,insert函数
find函数 https://blog.csdn.net/shujh_sysu/article/details/52026108 https://blog.csdn.net/OpenStack_/article/details/88429962
erase函数,insert函数 https://blog.csdn.net/zxy131072/article/details/94548629
#include <cstdio> #include <cstring> #include <string> #include <cmath> #include <algorithm> #include <iostream> using namespace std; int main() { string str; int count,pos=0; getline(cin,str); int len=str.length(); string str1="6666"; while(str.find(str1,pos)!=string::npos) { count=0; int first=str.find(str1); for(int i=str.find(str1);;i++) { if(str[i]=='6') count++; else break; } if(count>9) { str.erase(first,count); str.insert(first,"27"); } else if(count>3){ str.erase(first,count); str.insert(first,"9"); } } cout<<str<<endl; return 0; }
L1-054 福到了 (15分)
https://pintia.cn/problem-sets/994805046380707840/problems/994805076512587776
#include <cstdio> #include <cstring> #include <string> #include <cmath> #include <algorithm> #include <iostream> using namespace std; int main() { int n; int flag=1; char chr; char chr1[101][101]; cin>>chr>>n; getchar(); //这个没写的话也是错误的 for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { chr1[i][j]=getchar(); //开始写成cin>>chr1[i][j]死活过不了。不过这里也可以写成scanf("%c",&chr1[i][j]); if(chr1[i][j]!=' ') chr1[i][j]=chr; } getchar(); //开始没写这个,不写就是错误的 } for(int i=1;i<=n&&flag;i++) //flag放这挺好 { for(int j=1;j<=n;j++) { if(chr1[i][j]!=chr1[n+1-i][n+1-j]) { flag=0; break; } } } if(flag==1) cout<<"bu yong dao le\n"; for(int k=n;k>=1;k--) { for(int s=n;s>=1;s--) { cout<<chr1[k][s]; } cout<<endl; } return 0; }
L1-003 个位数统计 (15分)
https://pintia.cn/problem-sets/994805046380707840/problems/994805143738892288
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
char chr[1010];
int a[10]={0};
cin>>chr;
for(int i=0;i<strlen(chr);i++)
{
int temp=chr[i]-'0';
a[temp]++;
}
for(int j=0;j<10;j++)
{
if(a[j]!=0) cout<<j<<":"<<a[j]<<endl;
}
return 0;
}