2023天梯赛校内选拔赛

星号阵列-平行四边形

跳转链接:

4107:星号阵列-平行四边形

要点

比起之前写的二维打表会更好找规律一些,思维更加迅速也不容易写错,末尾空格的问题也可以解决

代码

#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
	int n;
	scanf("%d",&n);
	
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<n-i;j++) cout<<" ";
		if(i==1||i==n)
		{
			for(int i=0;i<n;i++) cout<<"*";
		}
		else
		{
			cout<<"*";
			for(int j=0;j<n-2;j++) cout<<" ";
			cout<<"*";
		}
		puts("");
	}
	return 0;
}

常规赛排名

跳转链接:

4108:常规赛排名

要点

  • 审题,场次分是根据一整场比赛三赢两胜得分,游戏分是根据一场比赛中赢的局数(一开始一直以为游戏分也是看一整场比赛😭😭)
  • map[]操作符
    我们都知道map由key映射value,key只能出现一次,根据key自动排序。
    map[key]=value,如果key不存在则插入,存在则更新
    map[key],如果key存在则返回value,不存在则返回0
  • int的输入
    int类型如果输入的是字符,int值不会是ASCALL码,如果是数组+字符将自动截取前部分数字(cin特性int输入时就会检测输入的是不是数字)
  • 自定义排序
    经常会想着vector容器类型得是pair相关的组合,但是直接使用一个类做vector类型然后自定义排序也是非常方便的

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<vector>

using namespace std;

class team
{
public:
	string name;
	int first,second; //场次分,游戏分	
}; 

bool cmp(team t1,team t2)
{
	if(t1.first!=t2.first)
	    return t1.first>t2.first;
	if(t1.second!=t2.second)
	    return t1.second>t2.second;
	return t1.name<t2.name;
}
map<string,int> play,game; //场次,游戏
vector<team> v;

int main()
{
	int n;
	scanf("%d",&n);
	
	while(n--)
	{
		string a,b;
		int a_score,b_score;
		char t;
		cin>>a>>b>>a_score>>t>>b_score;
		
		game[a]+=a_score-b_score; //插入!!
		game[b]+=b_score-a_score;
		
		if(a_score>b_score) play[a]++;
		else play[b]++;
	}
	//cout<<game.size();
	for(auto it:game)
	{
		v.push_back({it.first,play[it.first],it.second});
	}
	
	sort(v.begin(),v.end(),cmp);
	
	for(auto it:v)
	{
		cout<<it.name<<" "<<it.first<<" "<<it.second<<endl;
	}
	return 0;
}
 
posted @ 2023-03-29 00:15  咕噜噜冒泡  阅读(33)  评论(0)    收藏  举报