PTA天梯赛L1 051-060题目解析

1.L1-051 打折

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
#define endl "\n"
int a[1000010];
int main() {
    double s;
    double count;
    cin>>s>>count;
    cout<<fixed<<setprecision(2)<<s*count/10;
    return 0;
}

2.L1-052 2018我们要赢

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
#define endl "\n"
int a[1000010];
int main() {
    cout<<"2018\nwo3 men2 yao4 ying2 !";
    return 0;
}

3.L1-053 电子汪

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
#define endl "\n"
int a[1000010];
int main() {
    int a,b;
    cin>>a>>b;
    for(int i=1;i<=a+b;i++) cout<<"Wang!";
    return 0;
}

4.L1-054 福到了

解析:

注意是使用指定字符进行翻转打印,将翻转的功能封装出来即可

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
#define endl "\n"
#define maxLine 110
int n;
char s;
char tt[maxLine][maxLine];
bool checkArray(char signArray[][maxLine]){
    int len=n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if (signArray[i][j]!=signArray[n+1-i][j]){
                return false;
            }
        }
    }
    return true;
} 

void print(char ss[][maxLine]){
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cout<<ss[i][j];
        }
        cout<<endl;
    }
}

int main() {
    cin>>s>>n;
    getchar();
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n+1;j++){
            char temp=getchar();
            if (j<=n) tt[i][j]=temp;
        }
    }
    // print(tt);
    if (checkArray(tt)){
        cout<<"bu yong dao le"<<endl;
    }
    for(int i=n;i>=1;i--){
        for(int j=n;j>=1;j--){
            if (tt[i][j]!=' ') cout<<s;
            else cout<<' ';
        }
        cout<<endl;
    }
    return 0;
}

5.L1-055 谁是赢家

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <string>
#include <utility>

using namespace std;

struct Actor {
	int audience_vote;
	int judge_vote;
}; 

Actor a,b;

int judge_1,judge_2,judge_3;

bool check_win(Actor a,Actor b) {
	if (a.judge_vote == 3) return true;
	if (b.judge_vote == 3) return false;
	return (a.audience_vote > b.audience_vote ? true : false);
}
int main() {
	cin>>a.audience_vote>>b.audience_vote;
	cin>>judge_1>>judge_2>>judge_3;
	!judge_1 ? a.judge_vote ++ : b.judge_vote ++ ; 
	!judge_2 ? a.judge_vote ++ : b.judge_vote ++ ; 
	!judge_3 ? a.judge_vote ++ : b.judge_vote ++ ; 
	
	bool flag = check_win(a,b);
	cout<<"The winner is "<<(flag ? "a":"b")<<": "<<(flag ? a.audience_vote: b.audience_vote)<<" + "<<(flag ? a.judge_vote : b.judge_vote); 
	return 0;
}

6.L1-056 猜数字

解析:

根据各自猜测的数字到平均数的距离进行排序即可得到想要的序列

#include<bits/stdc++.h>
#include<string.h>
#include<cstring>
using namespace std;
#define IOO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//const int maxLine=5000+10;
//#define ll long long int
#define int long long int
#define um unordered_map<int,int>
#define vec vector<int>
const int maxLine=1e4+10;
//#define DEBUG true
//int n,m,k;

//int arr[maxLine];

//调用可以进行重定向
void initRedict() {
#ifdef DEBUG
	cout<<"执行重定向"<<endl;
	//重定向输入
	freopen("../redict/demo/demo_in.txt","r",stdin);
#endif
}
string mystr;
int n;
int len,indexx;
int startx,starty;
char arr[maxLine][maxLine];

struct student {
	string name;
	double score;
	double rankNums;
};
struct student ttt[maxLine];

bool cmp(struct student a,struct student b){
	return a.rankNums<b.rankNums;
}
signed main() {
	int n;
	cin>>n;
	getchar();
	double sum=0;
	for(int i=0;i<n;i++){
		cin>>ttt[i].name>>ttt[i].score;
		sum+=ttt[i].score;
	}
	sum/=n;
	sum/=2;
	for(int i=0;i<n;i++) ttt[i].rankNums=fabs(sum-ttt[i].score);
	sort(ttt,ttt+n,cmp);
	cout<<(int)(sum)<<" "<<ttt[0].name<<endl;
	
	
	return 0;
}

7.L1-057 PTA使我精神焕发

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <string>
#include <utility>

using namespace std;


int main() {
	cout<<"PTA shi3 wo3 jing1 shen2 huan4 fa1 !";
	return 0;
}

8.L1-058 6翻了

解析:

注意是要求找到3个和9个以上的6,所以先找出9个以上的6进行替换,后找出3个以上的6进行替换,并将该行为封装为函数

#include<iostream>

using namespace std;

string s;

int getLength(string s,int indexs,char ch){
	int sum=0;
	for(int i=indexs;i<s.size();i++){
		if (s[i]!=ch) break;
		else sum++;
	}
	return sum;
}

bool flag=false;

string three_six = "6666"; // 超过3个连续的6 
string nine_six = "6666666666"; // 超过9个连续的6 

void del_str(string &init_s,string temp_str,string target) {
	while(init_s.find(temp_str)!=string::npos){
		int pos=init_s.find(temp_str);
		init_s.replace(pos,getLength(init_s,pos,temp_str[0]),target);
	}
}
signed main(){
	getline(cin,s);
	// 消除 6666666666
	del_str(s,nine_six,"27");
	// 消除6666.... 
	del_str(s,three_six,"9");
	cout<<s;
	return 0;
} 

9.L1-059 敲笨钟

解析:

一开始的思路根据空格分割出vector,然后利用诗句的对称性来读取特定位置的string并进行检测。但是测试样例的诗句其实包含了一些字数不对称的诗句,所以后面直接改成是否能找到符合要求的,以ong,和ong.结尾的字符串为条件。找到符合此要求的直接修改最后三个元素并输出该vector

#include<iostream>
#include<vector>
#include<sstream>
#include<string>

using namespace std;

int n;
string poem;
string first_targrt_tail = "ong,";
string second_targrt_tail = "ong.";

const int SUFFIX_SIZE = 4;

vector<string> splits(string s,char ch) {
	vector<string> poem_list;
	string tmp;
	istringstream  ss(s);
	while(getline(ss,tmp,ch)) {
		poem_list.push_back(tmp);
	}
	return poem_list;
}

bool find_spicial_suffix_string(vector<string> &my_vec , string s) {
	for(auto it:my_vec) {
		if (it.size() >= SUFFIX_SIZE && it.substr(it.size()-SUFFIX_SIZE) == s) return true;
	}
	return false;
}
signed main(){
	cin>>n;
	getchar();
	for(int i=0;i<n;i++) {
		getline(cin,poem);
		vector<string> my_vec = splits(poem,' ');
//		for(auto it:my_vec) cout<<it<<" "; 

		int size_v = my_vec.size();
		string first_tail = my_vec[(size_v - 1)/2];
		string second_tail = my_vec[size_v-1];
		
//		cout<<first_tail.size()<<" "<<first_tail.substr(first_tail.size()-SUFFIX_SIZE)<<" "<<second_tail.substr(second_tail.size()-SUFFIX_SIZE)<<endl;
		// 愚蠢的测试样例,诗句都不是对称的 
		if (
//			first_tail.size() >= SUFFIX_SIZE 
//			&& first_tail.substr(first_tail.size()-SUFFIX_SIZE) == first_targrt_tail 
//			&& second_tail.substr(second_tail.size()-SUFFIX_SIZE) == second_targrt_tail) 
			find_spicial_suffix_string(my_vec, first_targrt_tail)
			&& find_spicial_suffix_string(my_vec, second_targrt_tail) )
			{
			my_vec[size_v-3] = "qiao";my_vec[size_v-2] = "ben";my_vec[size_v-1] = "zhong.";
			bool flag = false;
			for(auto it:my_vec) {
				if (flag) cout<<" ";
				cout<<it;
				flag = true;
			}	
		}else {
			cout<<"Skipped";
		} 
		cout<<endl;
	}
	return 0;
} 

10.L1-060 心理阴影面积

#include<iostream>
#include<iomanip>

using namespace std;


int x,y;
const int H = 100;

double calc_triangle_area(int h) {
	return 1.0 * H * h * 0.5;
} 
signed main(){
	cin>>x>>y;
	cout<<fixed<<setprecision(0)<<calc_triangle_area(100) - calc_triangle_area(100 - x) - calc_triangle_area(y) <<endl;
	return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

JSU_曾是此间年少

打赏CSDN不如给自己

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

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

打赏作者

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

抵扣说明:

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

余额充值