第四次双周赛/天梯赛选拔赛(Week 15)

发布于 2023-05-13  229 次阅读


T1 重要的话说三遍(签到)

题目:

代码:

#include <iostream>
int main(){
    int t=3;
    while(t--) std::cout<<"I'm gonna Win!"<<'\n';
    return 0;
}

T2 日期格式化

题目:

#include <bits/stdc++.h>
using namespace std;
int main(){
	int m,d,y;
	cin>>m;
	getchar();//读入时跳过 ‘-’ 符号
	cin>>d;
	getchar();
	cin>>y;
	getchar();
	cout<<y<<"-";
	if(m<10) cout<<"0"<<m<<"-";
	else cout<<m<<"-";
	if(d<10)cout<<"0"<<d;
	else cout<<d;
	return 0;
}

T3 大笨钟

题目:

代码:

#include <bits/stdc++.h>
using namespace std;
stack <int > z;
int main(){
	int h,m;
	cin>>h; getchar(); cin>>m;
	if((h<12)||(h==12&&m==0)){
		cout<<"Only ";
		if(h<10) {
			cout<<"0"<<h<<":";
		} else cout<<h<<":";
		if(m<10){
			cout<<"0"<<m;
		} else cout<<m;
		cout<<".  Too early to Dang.";
	}
	else{
		h-=12;
		if(m!=0) h++;
		while(h--) cout<<"Dang";
	}
	return 0;
}

T4 拯救外星人

题目:

代码:

#include <bits/stdc++.h>
using namespace std;
int main(){
	long long int a,b,ans=1;
	cin>>a>>b;
	a+=b;
	for(int i=1;i<=a;i++){
		ans*=i;
	}
	cout<<ans;
	return 0;
}

T5 个位数统计

题目:

代码:

#include <bits/stdc++.h>
using namespace std;
int m[11];
int main(){
	char n;
	while(cin>>n){
		m[n-'0']++;
	}
	for(int i=0;i<=9;i++){
			m[i]? (cout<<i<<":"<<m[i]<<endl) : continue;
	}
	return 0;
}

T6 正整数

题目:

这是孬的模拟,数据读入上会造成很大的麻烦,因为L部分和R部分可能存在非数字的字符所以不能够直接读入整型变量里。为了好定位第一个空格的位置,方便判断L部分的合法性,我用了单字符逐个读入的方法,遇到空格时停止。然后再将输入数据的剩余部分全部读入到R部分中,再逐个判断。代码当然没有必要写的那么复杂,不过我也懒得改了就是; ;

#include <bits/stdc++.h>
using namespace std;
bool rno,lno;
int main(){
	string a="",b="";
	char temp;
	while(temp=getchar()){
		if(isspace(temp)){
			break;
		}
		a = a+temp;
	}
	getline(cin,b);

	if(a.length()>4) lno=true;
	if(b.length()>4) rno=true;
	int lnum=0,rnum=0;
	
	for(int i=0;i<a.length();i++){
		if(a[i]>='0'&&a[i]<='9'){
			if(lnum==0&&((a[i]-'0')==0)){
				lno=true;
				break;
			}
			lnum*=10;
			lnum+=a[i]-'0';
		}
		else{
			lno=true;
			break;
		}
	}
	for(int i=0;i<b.length();i++){
		if(b[i]>='0'&&b[i]<='9'){
			if(rnum==0&&(b[i]-'0'==0)){
				rno=true;
				break;
			}
			rnum*=10;
			rnum+=b[i]-'0';
		}
		else{
			rno=true;
			break;
		}
	}
	if(lnum>1000||lnum<1) lno=true;
	if(rnum>1000||rnum<1) rno=true;
	if(lno==false) cout<<lnum;
	else cout<<"?";
	
	cout<<" + ";
	
	if(rno==false) cout<<rnum;
	else cout<<"?";
	cout<<" = ";
	if(lno==true||rno==true)
		cout<<"?";
	else cout<<lnum+rnum;
	
//	cout<<endl<<lno<<" "<<rno;
}

T7 打印沙漏

题目:

要先算出能够打印出的层数,然后用层数变量控制符号与空格的数量(注意到第i层有 2i 个空格,2(max-i)-1 个符号),由于上下层是对称的,所以我的做法是直接打一半,另一半以字符串的形式存在栈里,很方便。

代码:

#include <iostream>
#include <stack>
using namespace std;
stack <string > a;
int n;
char f;
int main(){
	cin>>n; cin>>f;
	int h;
	for(int i=1;i<=99999;i++){
		if(i*i*2-1>n){
			h = i-1;
			break;
		}
	}
	int remain = n - h*h*2 +1;
	for(int i=1;i<=h;i++){
		string temp="";
		for(int j=1;j<i;j++) temp = " "+temp;
		for(int j=1;j<=(h-i+1)*2-1;j++) temp = temp + f;
		cout<<temp<<endl;
		a.push(temp);
	}
	a.pop();
	while(!a.empty()){
		cout<<a.top()<<endl;
		a.pop();
	}
	cout<<remain;
	return 0;
}

T8 机工士姆斯塔迪奥

题目:

代码:

#include <iostream>
using namespace std;
long long int row[100005],line[100005],tot,n,m,q,t,eaten,stat,r,l;
void prework(){
	cin>>n>>m>>q;
	tot = n*m;
	for(int i=1;i<=q;i++){
		cin>>stat>>t;
		if(stat==0&&row[t]==0)
			r++ , row[t]=1;
		else if(stat==1&&line[t]==0) l++, line[t]=1;
	}
}
void solve(){
	tot = tot + l*r - l*n - r*m;
	cout<<tot;
}
int main(){
	prework();
	solve();
	return 0;
}

L2-1

题目:

样例:

上来写了个领接矩阵,一交WA了。再读一遍题发现漏掉朋友的朋友也是朋友这个条件,敲出并查集提交过了。

#include <iostream>
using namespace std;
bool check;
int n,t,q;
int a[105][105];
int f[105];
void init(){
	for(int i=1;i<=103;i++) f[i]=i;
}
int getf(int x){
	if(f[x]==x) return x;
	else{
		f[x]=getf(f[x]);
		return f[x];
	}
}
void merge(int a,int b){
	a=getf(a);
	b=getf(b);
	if(a!=b){
		f[a]=b;
	}
}
int main(){
	init();
	cin>>n>>t>>q;
	for(int i=1;i<=t;i++){
		int x1,x2,x3;
		cin>>x1>>x2>>x3;
		a[x1][x2]=x3;
		a[x2][x1]=x3;
		if(x3==1) merge(x1,x2);
	}
	for(int i=1;i<=q;i++){
		check = false;
		int x1,x2;
		cin>>x1>>x2;
		if(a[x1][x2]==1) cout<<"No problem"<<endl;
		else if(a[x1][x2]==0) cout<<"OK"<<endl;
		else if(a[x1][x2]==-1){
			for(int i=1;i<=n;i++){
				if(i==x1||i==x2) continue;
				if(getf(x1)==getf(x2)){
					check=true;
				}
			}
			if(check) cout<<"OK but..."<<endl;
			else cout<<"No way"<<endl;
		}
	}
	return 0;
}

L2-2 名人堂与代金券(数据结构)

题目:

样例:

代码:

#include <bits/stdc++.h>
using namespace std;
int n,g,k,ans,cnt;
struct student{
	string email;
	int score;
}stu[100005];
bool cmp(student a,student b){
	if(a.score>b.score)
		return true;
	else if(a.score==b.score){
		if(a.email>b.email){
			return false;
		} 
		else return true;
	}
	else return false;
}

void prework(){
	cin>>n>>g>>k;
	for(int i=1;i<=n;i++){
		cin>>stu[i].email;
		int x2;
		cin>>x2;
		if(x2>=g) ans+=50;
		else if(x2<g&&x2>=60) ans+=20;
		stu[i].score=x2;
	}
	sort(stu+1,stu+n+1,cmp);
}
int main(){
	prework();
	cout<<ans<<endl;
	int rank=1;
	if(k>=1) cout<<rank<<' '<<stu[1].email<<' '<<stu[1].score<<endl;
	cnt++;
	for(int i=2;i<=n;i++){
		if(stu[i].score==stu[i-1].score){
			cnt++;
		}
		else{
			cnt++;
			rank = cnt;
		}
		if(rank<=k)
		cout<<rank<<' '<<stu[i].email<<' '<<stu[i].score<<endl;
		else break;
	}
	return 0;
}

比较函数写的混乱不堪,重写一遍应该是这个样子:

bool cmp(student a,student b){
	if(a.score!=b.score) 
		return a.score>b.score;
	else{
		return a.email<b.email;
	}
}