团体程序设计天梯赛-练习集 04

天梯赛题解合集
团体程序设计天梯赛-练习集 (L1-001 - L1-012)
团体程序设计天梯赛-练习集 (L1-013 - L1-024)
团体程序设计天梯赛-练习集 (L1-025 - L1-036)
团体程序设计天梯赛-练习集 (L1-037 - L1-048)

L1-037 A除以B 模拟

image-20240402144029588

样例

样例1:

输入

-1 2

输出

-1/2=-0.50

样例2

输入

1 -3

输出

1/(-3)=-0.33

样例13

输入

5 0

输出

5/0=Error

思路

按照题面模拟即可

Ac代码

#include<bits/stdc++.h>
using namespace std;

int a,b;
double ans;

int main(){
	cin>>a>>b;
	ans=a*1.0/b;
	if(b!=0){
		if(b<0) printf("%d/(%d)=%.2lf\n",a,b,ans);
		else printf("%d/%d=%.2lf\n",a,b,ans);
	}
	else printf("%d/%d=Error\n",a,b);
	return 0;
} 

L1-038 标题 模拟

样例

输入


输出

Hello World!

思路

按照题面模拟即可

Ac代码


L1-039 古风排版 模拟

bjqYYq.png

样例

输入

4
This is a test case

输出

asa T
st ih
e tsi
 ce s

思路

按照题面模拟即可

Ac代码

#include<bits/stdc++.h>
using namespace std;

const int N=1e5+5;

int n,k,i,j,len;
string str; 

int main(){
	scanf("%d",&n); // 每一列的字符数
	getchar(); // 吸收空格
	getline(cin,str); // 非空字符串
	for(i=0;i<n;i++){
		string tmp;
		for (int j = str.length() - 1; j >= 0; j--)
			if ((j - i) % n == 0) // 从后往前每隔n-1个取一个字母构成一行
				tmp += str[j];
		if(tmp.size()<str.size()/(double)n) // 不足N个用空格补上
			tmp = ' ' + tmp;
		cout << tmp << endl;
	}
	return 0;
}

L1-040 最佳情侣身高差 模拟

image-20240402144155464

样例

输入

2
M 1.75
F 1.8

输出

1.61
1.96

思路

按照题面模拟即可

Ac代码

#include<bits/stdc++.h>
using namespace std;

int n;
double x;
char c;

int main(){
	scanf("%d",&n);
	getchar();
	while(n--){
		cin>>c>>x;
		if(c=='M'){
			printf("%.2lf\n",x/1.09);
		}
		else
			printf("%.2lf\n",x*1.09);
	}
	return 0;
}

L1-041 寻找250 模拟

image-20240402144300902

样例

输入

888 666 123 -233 250 13 250 -222

输出

5

思路

按照题面模拟即可

Ac代码

#include<bits/stdc++.h>
using namespace std;

int x,k;

int main(){
	k=0;
	while(~scanf("%d",&x)){
		k++;
		if(x==250){
			cout<<k<<endl;
			break;
		}
	}
	return 0;
}

L1-042 日期格式化 模拟

image-20240402144339584

样例

输入

03-15-2017

输出

2017-03-15

思路

按照题面模拟即可

Ac代码

#include<bits/stdc++.h>
using namespace std;

string str;
int i;

int main(){
	getline(cin,str);
	for(i=6;i<10;i++) cout<<str[i];cout<<"-";
	for(i=0;i<2;i++) cout<<str[i];cout<<"-";
	for(i=3;i<5;i++) cout<<str[i];
	
	return 0;
}

L1-043 阅览室 STL-set&map

bxgNh4.png

样例

输入

3
1 S 08:10
2 S 08:35
1 E 10:00
2 E 13:16
0 S 17:00
0 S 17:00
3 E 08:10
1 S 08:20
2 S 09:00
1 E 09:20
0 E 17:00

输出

2 196
0 0
1 60

思路

用set和map确保同一本书在任何时间区间内只可能被一位读者借阅

还书后才能将借书次数+1

平均阅读时间 没特殊说明就是四舍五入

Ac代码

#include<bits/stdc++.h>
using namespace std;

struct Node{
    int h,m;
};
set<int>se;
map<int,Node>mp;

int main(){
	int n; scanf("%d",&n); //天数
	int cnt=0,ans=0,num;
	int hh,mm;
	while(~scanf("%d",&num)){ // 输入书号
		char c; cin>>c; // 输入键值
		if(num==0){ // 0作为书号输入时,表示一天工作结束
			scanf("%02d:%02d",&hh,&mm); // 输入发生时间
            int f=round(ans*1.0/cnt);	// 平均阅读时间 没特殊说明就是四舍五入
            if(ans==0) f=0;
            printf("%d %d\n",cnt,f);
            se.clear(); // 为新的一天初始化
            ans=0,cnt=0;
            continue; // 不然会继续执行下面的语句。
		}
		if(c=='S'){ // 借书 
            // 用set和map确保同一本书在任何时间区间内只可能被一位读者借阅
            scanf("%02d:%02d",&mp[num].h,&mp[num].m);
            se.insert(num);
        }
        if(c=='E'){ // 还书
	        scanf("%02d:%02d",&hh,&mm);
	        if(se.count(num)==1){ // 只还借过的书
	            cnt++; // 还书后借书次数+1
	            se.erase(num); //还已经还过的书。
	            ans+=(hh*60+mm)-(mp[num].h*60+mp[num].m); // 计算总阅读时间
	        }
    	}
	}
	return 0;
}

L1-044 稳赢 模拟

image-20240402144450147

样例

输入

2
ChuiZi
JianDao
Bu
JianDao
Bu
ChuiZi
ChuiZi
End

输出

Bu
ChuiZi
Bu
ChuiZi
JianDao
ChuiZi
Bu

思路

按照题面模拟即可

Ac代码

#include<bits/stdc++.h>
using namespace std;

int main(){
    int k, tmp=0; cin>>k; k++;
    string str;
    while(cin>>str && str!="End"){
        tmp++;
        if(tmp%k==0){ // 平局
            if(str=="ChuiZi") cout<<"ChuiZi"<<endl;
            if(str=="JianDao") cout<<"JianDao"<<endl;
            if(str=="Bu") cout<<"Bu"<<endl;
        }
        else{ // 稳赢
            if(str=="ChuiZi") cout<<"Bu"<<endl;
            if(str=="JianDao") cout<<"ChuiZi"<<endl;
            if(str=="Bu") cout<<"JianDao"<<endl;
        }
    }
    return 0;
}

L1-045 宇宙无敌大招呼 模拟

image-20240402144602216

样例

输入

Mars

输出

Hello Mars

思路

按照题面模拟即可

Ac代码

#include<bits/stdc++.h>
using namespace std;

int main(){
    string str; cin>>str;
    cout<<"Hello "<<str<<endl;
    return 0;
}

L1-046 整除光棍 思维

bxf4JS.png

样例

输入

31

输出

3584229390681 15

思路

首先 s对应光棍数 的位数必定大于等于 s 的位数,s=31 时假设光棍数 n=111

  1. 111 ÷ 31 = 3 余 18 111 \div 31=3余18 111÷31=318, n不能整除s
  2. 继续寻找下一个n, 类似于手算除法将余数18*10+1=181
  3. 处理后的余数作为下一个n进行第一步, 直到余数为0, 即找到了能整除sn

Ac代码

#include<bits/stdc++.h>
using namespace std;

const int N=1e5+5;

int main(){
	int n; cin >> n;
	int base=1,k=1;
	while(base<n){ // 找到小于n的最大光棍数
		base=base*10+1;
		k++; //记录x的位数
	} 
	while(base!=0){ // 模拟手算除法
		cout<<(base/n); // 输出当前的商
		if(base%n==0) break; // 余数为0 -- 除尽了
		else base=base%n*10+1; // 没除尽 加一位1继续除
		k++; //记录x的位数
	}
	cout<<" "<<k<<endl;
	return 0;
}

L1-047 装睡 模拟

image-20240402144638910

样例

输入

4
Amy 15 70
Tom 14 60
Joe 18 50
Zoe 21 71

输出

Tom
Zoe

思路

按照题面模拟即可

Ac代码

#include<bits/stdc++.h>
using namespace std;

void check(string name,int a,int b){
    if(a<15||a>20){
        cout<<name<<endl;
        return;
    }
    if(b<50||b>70){
        cout<<name<<endl;
        return;
    }
}

int main(){
    int N;cin>>N;
    string name;
    int a,b;
    for(int i=1;i<=N;i++){
        cin>>name>>a>>b;
        check(name,a,b);
    }
    return 0;
}

L1-048 矩阵A乘以B 模拟

image-20240402144856273

样例

样例1:

输入

2 3
1 2 3
4 5 6
3 4
7 8 9 0
-1 -2 -3 -4
5 6 7 8

输出

2 4
20 22 24 16
53 58 63 28

样例2:

输入

3 2
38 26
43 -5
0 17
3 2
-11 57
99 68
81 72

输出

Error: 2 != 3

思路

按照题面模拟即可

可以重新排列循环以提高空间局部性,这样的优化不会改变矩阵乘法的时间复杂度,但是会在得到常数级别的提升。

image-20240402145036199

Ac代码

#include<bits/stdc++.h>
using namespace std;

const int N = 105;

int main(){
    int Ra,Ca,Rb,Cb;
    int A[N][N],B[N][N];
    cin>>Ra>>Ca; // 输入矩阵A
    for(int i = 0 ; i < Ra ; i ++)
        for(int j = 0 ; j < Ca ; j ++)
            cin >> A[i][j];
    cin>>Rb>>Cb; // 输入矩阵B
    for(int i = 0 ; i < Rb ; i ++)
        for(int j = 0 ; j < Cb ; j ++)
            cin >> B[i][j];
    if(Ca!=Rb) printf("Error: %d != %d\n",Ca,Rb); // 无法相乘
    else{ // 矩阵乘法
        cout<<Ra<<" "<<Cb<<endl;
        for(int i = 0 ; i <Ra ; i ++){
            for(int j = 0 ; j < Cb ; j ++){
                int sum = 0;
                for(int k = 0 ; k < Ca ; k ++){
                    sum+=A[i][k]*B[k][j];
                }
                cout<<sum;
                if(j!=Cb-1) cout<<" ";
            }
            cout<<endl;
        }
    }
    return 0;
}
  • 21
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

温柔说给风

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

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

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

打赏作者

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

抵扣说明:

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

余额充值