PTA天梯赛L1 031-040题目解析

发布于:2024-03-07 ⋅ 阅读:(66) ⋅ 点赞:(0)

目  录

1.L1-031 到底是不是太胖了

2.L1-032 Left-pad

3.L1-033 出生年

4.L1-034 点赞

5.L1-035 情人节

6.L1-036 A乘以B

7.L1-037 A除以B

8.L1-038 新世界

9.L1-039 古风排版

10.L1-040 最佳情侣身高差


1.L1-031 到底是不是太胖了

分析: 

注意转换成市斤,公斤可能会出错qwq

代码: 

#include<iostream>
#include<cmath>
using namespace std;

int n;
double h,w;
void check(double h,double w) {
	double standw=(h-100)*0.9*2;
	if (fabs(w-standw)<0.1*standw) cout<<"You are wan mei!"<<endl;
	else if (w>standw) cout<<"You are tai pang le!"<<endl;
	else  cout<<"You are tai shou le!"<<endl;
}
signed main() {
	cin>>n;
	for(int i=0; i<n; i++) {
		cin>>h>>w;
//		w/=2;
		check(h,w);

	}
	return 0;
}

2.L1-032 Left-pad

分析: 

首先比较给定长度和字符串长度,字符串长度长就截取,否则就是用特定符号补全字符串

代码: 

#include<iostream>
#include<string>
using namespace std;

int n;
char s;
string str;
signed main(){
	cin>>n>>s;
//	cin>>str;
	getchar();
	getline(cin,str);
	if (str.size()<n){
		for(int i=0;i<n-str.size();i++){
			cout<<s;
		}
		cout<<str;
	}else {
		for(int i=str.size()-n;i<str.size();i++){
			cout<<str[i];
		}
	}
	
	
	return 0;
}

3.L1-033 出生年

分析: 

注意小于1000的年份都需要添加一种数字0,我们可以使用set来记录数字的种类

代码: 

#include<iostream>
#include<set>

#include<cstdio> 

#define endl "\n"
#define debug(x) cout<<"变量"<<#x<<"的值是"<<x<<endl;
using namespace std;

int y,n;

bool check(int year,int n){
	set<int> myset;
	if (year<1000) myset.insert(0);
	while(year){
		myset.insert(year%10);
		year/=10;
	}
//	debug(myset.size())
//	debug(n)
	if (myset.size()==n) return true;
	return false;
}
signed main(){
//	debug(666)
	cin>>y>>n;
//	debug(777)
	int i;
	for( i=y;;i++){
//		debug(i)
		if (check(i,n)) break;
	}
//	cout<<i-y<<" "<<
		printf("%d %04d",i-y,i); 
	
	return 0;
}


//  输出x 和当时的年份 
//我出生于1988年,直到25岁才遇到4个数字都不相同的年份
//         y           x         n

4.L1-034 点赞

分析: 

标签编号为1000以内,我们使用结构体排序按照出现频次直接输出即可

代码: 

#include<iostream>
#include<algorithm>

using namespace std;

struct sss{
	int coun;
	int indexs;
};

struct sss ttt[1010];
int m,n;

int temp;

bool cmp(struct sss a,struct sss b){
	if (a.coun!=b.coun) return a.coun>b.coun;
	return a.indexs>b.indexs;
}
signed main(){
	cin>>n;
	for(int i=0;i<1010;i++){
		ttt[i].indexs=i;
	}
	for(int i=0;i<n;i++){
		cin>>m;
		for(int j=0;j<m;j++){
			cin>>temp;
			ttt[temp].coun++;
		}
	}
	sort(ttt,ttt+1010,cmp);
	cout<<ttt[0].indexs<<" "<<ttt[0].coun<<endl;
	return 0;
}

5.L1-035 情人节

分析: 

简单统计一下输出完之后有多少个人的计数,中途记录第二个和第十四个人的名字

代码: 

#include<iostream>

using namespace std;

int coun=0;

string s;

string er="",shisi="";
signed main(){
	
	while(1){
		cin>>s;
		if (s==".") break;
		coun++;
		if (coun==2) er=s;
		if (coun==14) shisi=s;
	}
	if (coun<2) cout<<"Momo... No one is for you ...";
	else if (coun<14) cout<<er<<" is the only one for you...";
	else cout<<er<<" and "<<shisi<<" are inviting you to dinner...";
	
	return 0;
}

6.L1-036 A乘以B

代码: 

#include<iostream>

using namespace std;

signed main(){
	int a,b;
	cin>>a>>b;
	cout<<a*b;
	return 0;
}

7.L1-037 A除以B

分析: 

注意B为0的情况即可

代码: 

#include<iostream>
#include<iomanip>
using namespace std;

int a,b;

double s;

void print1(int a,int b){
	if (b==0) cout<<a<<"/"<<b<<"=Error"<<endl;
	else if (b<0) cout<<a<<"/"<<"("<<b<<")"<<"="<<fixed<<setprecision(2)<<1.0*a/b<<endl;
	else  cout<<a<<"/"<<b<<"="<<fixed<<setprecision(2)<<1.0*a/b<<endl;
}
signed main(){
	cin>>a>>b;
	print1(a,b);
	
	return 0;
}

8.L1-038 新世界

代码: 

#include<iostream>

using namespace std;

signed main(){
	cout<<"Hello World"<<endl<<"Hello New World"<<endl;
	return 0;
} 

9.L1-039 古风排版

分析: 

题目是要我们按照古代书写方式对现代书写方式的字符串进行输出,

我们观察可以发现,输出的每行字符在原始字符串的下标对n取余都是一样的数值,那么我们就可以按照这个特性将字符串从左到右,从上到下书写存储,

最后将这个二维数组从右往左,从上到下输出即可,注意使用memset可能会出现问题

代码: 

#include<iostream>
#include<string>
#include<string.h>

#define debug(x) cout<<"变量"<<#x<<"的值是"<<x<<endl;
 
using namespace std;
const int MAX=1010;

char s[200][200];
int n;
string str;

int colCoun=0;
// 计算需要多少列 
int getCol(int len,int n){
	return len/n+(bool)(len%n); 
}

void print(char arr[MAX][MAX],int line,int col){
	for(int i=0;i<line;i++) {
		for (int j=0;j<col;j++){
			cout<<arr[i][j]<<" ";
		}
		cout<<endl;
	}
}

void init(){
	for(int i=0;i<200;i++){
		for(int j=0;j<200;j++){
			s[i][j]=' ';
		}
	}
}
signed main(){
	cin>>n;
	getchar();
	getline(cin,str);
	init();
//	memset(s,sizeof(s),' ');
	int col=getCol(str.size(),n);
	for(int i=0;i<str.size();i++){
		s[colCoun%n][i/n]=str[i];
		colCoun++;
	}
//	debug(n)
	for (int i=0;i<n;i++){
		for(int j=col-1;j>=0;j--){
			cout<<s[i][j]; 
		}
//		cout<<"工作正常"<<endl;
		cout<<endl;
	}
	return 0;
} 

10.L1-040 最佳情侣身高差

分析: 


判断男女,套用不同的公式输出

代码: 

#include<iostream>
#include<iomanip>
using namespace std;

const double TIME=1.09;

double maleToFemale(double hight){
	return hight / TIME;
}
double feMaleToMale(double hight){
	return hight * TIME;
}
int n;
char s;
double nums;
signed main(){
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>s>>nums;
		if (s=='M') cout<<fixed<<setprecision(2)<<maleToFemale(nums)<<endl;
		else cout<<fixed<<setprecision(2)<<feMaleToMale(nums)<<endl;
	}
	
	return 0;
}

本文含有隐藏内容,请 开通VIP 后查看

网站公告


今日签到

点亮在社区的每一天
去签到