跳到正文

个人记录

9.21课程题目解析+总结

xiangrui0305约 11 分钟
原文

这是 2025年9月27日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年3月11日
篇幅
约 11 分钟

版本

  1. v2当前
  2. v1

9.21课程题目解析+总结

题目解析+总结

P1029

分析题干+步骤书写

看到这里,大家都知道两个数的乘积是两个数的最大公约数与两个数的最小公倍数的乘积,而这两个数是已知的,所以我们只要枚举其中一个数即可。然后我们可以在for循环中做一个优化,并考虑两个数相同的情况,就可以了。

AC代码
C++
#include<bits/stdc++.h>
using namespace std;
int main(){
	int x,y;
	cin>>x>>y;
	int ans = x * y,cnt = 0;
	for(int i=1;i<=sqrt(ans);i++){
		if(ans % i == 0 && __gcd(i,x*y/i) == x){
			cnt += 2;
			if(i == x*y/i) cnt--;
		}
	}
	cout<<cnt<<endl;
	return 0;
}
题目小结

数学 + 枚举

P1328

分析题干+步骤书写
出拳剪刀石头布蜥蜴人斯波克
剪刀平输赢赢输
石头平输赢输
布平输赢
蜥蜴人平输
斯波克平

但是表格中的数据是没有填完整的。以下是填满的表格:

出拳剪刀石头布蜥蜴人斯波克
剪刀平输赢赢输
石头赢平输赢输
布输赢平输赢
蜥蜴人输输赢平输
斯波克赢赢输赢平

因为每一次赢的人得11分,输的得00分;平局两人都得00分,所以可编造出二维数组:

C++
int f[5][5]{
{0,0,1,1,0},
{1,0,0,1,0},
{0,1,0,0,1},
{0,0,1,0,1},
{1,1,0,0,0}};

接下来,我们只需要模拟一下两人的出拳和两人赢的次数,就完成了。

AC代码
C++
#include<bits/stdc++.h>
using namespace std;
int a[205],b[205];
int f[5][5]{
{0,0,1,1,0},
{1,0,0,1,0},
{0,1,0,0,1},
{0,0,1,0,1},
{1,1,0,0,0}};
int main(){
	int n,x,y;
	cin>>n>>x>>y;
	for(int i=1;i<=x;++i) cin>>a[i];
	for(int i=1;i<=y;++i) cin>>b[i];
	int cnt1 = 0,cnt2 = 0,sum1 = 0,sum2 = 0;
	for(int i=1;i<=n;++i){
		cnt1++,cnt2++;
		sum1 += f[a[cnt1]][b[cnt2]];
		sum2 += f[b[cnt2]][a[cnt1]];
		if(cnt1 >= x) cnt1 = 0;
		if(cnt2 >= y) cnt2 = 0;
	}
	cout<<sum1<<' '<<sum2<<endl;
	return 0;
}
题目小结

模拟

P1786

分析题干+步骤书写

本体只需模拟出最终答案即可。但要注意: 这一题要要排两次序,并注意排序大小,还得排除一些特殊职位。

AC代码
C++
#include<bits/stdc++.h>
using namespace std;
int n;
struct node{
	string name,do1;
	int banggong,DJ,id,zwid;
}a[1115];
bool cmp1(node x,node y){
	if(x.banggong == y.banggong) return x.id < y.id;
	return x.banggong > y.banggong;
}
bool cmp2(node x,node y){
	if(x.zwid == y.zwid){
		if(x.DJ == y.DJ) return x.id < y.id;
		return x.DJ > y.DJ;
	}
	return x.zwid > y.zwid;
}
int main(){
	cin>>n;
	int i2 = 0;
	for(int i=1;i<=n;++i){
		string a1,a2;int a3,a4;
		cin>>a1>>a2>>a3>>a4;
		if(a2 == "BangZhu" || a2 == "FuBangZhu"){
			cout<<a1<<' '<<a2<<' '<<a4<<endl;
			continue;
		}
		a[++i2].name = a1;
		a[i2].banggong = a3;
		a[i2].DJ = a4;
		a[i2].id = i;
	}
	sort(a+1,a+n+1-3,cmp1);
	for(int i=1;i<=n-3;++i){
		if(i >= 1 && i <= 2) a[i].zwid = 4,a[i].do1 = "HuFa";
		else if(i >= 3 && i <= 6) a[i].zwid = 3,a[i].do1 = "ZhangLao";
		else if(i >= 7 && i <= 13) a[i].zwid = 2,a[i].do1 = "TangZhu";
		else if(i >= 14 && i <= 38) a[i].zwid = 1,a[i].do1 = "JingYing";
		else a[i].zwid = 0,a[i].do1 = "BangZhong";
	}
	sort(a+1,a+n+1-3,cmp2);
	for(int i=1;i<=n-3;++i) cout<<a[i].name<<' '<<a[i].do1<<' '<<a[i].DJ<<endl;
	return 0;
}
题目小结

结构体 + 大模拟

homework~

P1067代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n; cin>>n;
    for(int i=1;i<=n+1;++i){
    	int x; cin>>x;
    	if(x > 0){
    		if(i != 1) cout<<'+';
    		if((x == 1 && n-i+1 == 0) || (x != 1)) cout<<x;
			if(n-i+1 == 1) cout<<'x';
			else if(n-i+1 != 0) cout<<"x^"<<n-i+1;
		}
		else if(x < 0){
			cout<<'-';
    		if((x == -1 && n-i+1 == 0) || (x != -1)) cout<<abs(x);
			if(n-i+1 == 1) cout<<'x';
			else if(n-i+1 != 0) cout<<"x^"<<n-i+1;
		}
	}
    
	return 0;
}

P1190代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,a[10005];
priority_queue<int,vector<int>,greater<int> >op;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;++i){
    	cin>>a[i];
    	if(i <= min(n,m)) op.push(a[i]);
	}
    for(int i=m+1;i<=n;++i){
    	int top = op.top(); op.pop();
    	op.push(a[i]+top);
	}
	int maxx = 0;
	while(op.size()) maxx = max(maxx,op.top()),op.pop();
	cout<<maxx<<endl;
	return 0;
}

P1093代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
struct node{
	int yu,shu,ying,sum,id;
}a[305];
bool cmp(node x,node y){
	if(x.sum == y.sum){
		if(x.yu == y.yu) return x.id < y.id;
		return x.yu > y.yu;
	}
	return x.sum > y.sum;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;++i){
    	cin>>a[i].yu>>a[i].shu>>a[i].ying;
    	a[i].sum = a[i].yu+a[i].shu+a[i].ying,a[i].id = i;
	}
	sort(a+1,a+1+n,cmp);
	for(int i=1;i<=5;++i) cout<<a[i].id<<' '<<a[i].sum<<endl;
	return 0;
}

P1563代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m;
struct node{
	bool d;
	string name;
}pp[100005];
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;++i) cin>>pp[i].d>>pp[i].name;
    int id = 1;
    for(int i=1;i<=m;++i){
    	int u,v;
    	cin>>u>>v;
    	if(u == 0){
    		if(pp[id].d == 0){
				if(id <= v) id = n - (v - id) % n;
				else id -= v;
			}
			else{
				if(id + v > n) id = (v - (n - id)) % n;
				else id += v;
			}
		}
		if(u == 1){
    		if(pp[id].d == 0){
				if(id + v > n) id = (v - (n - id)) % n;
				else id += v;
			}
			else{
    			if(id <= v) id = n - (v - id) % n;
    			else id -= v;
			}
		}
	}
	cout<<pp[id].name<<endl;
	return 0;
}

P2615代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[40][40],n;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin>>n;
    int x = 1,y = n/2+1;
    a[x][y] = 1;
    for(int k=2;k<=n*n;++k){
    	if(x == 1 && y == n) x = 2;
		else if(x == 1) x = n,y++;
		else if(y == n) y = 1,x--;
		else{
			if(a[x-1][y+1] == 0) x--,y++;
			else x++;
		}
		a[x][y] = k;
	}
	for(int i=1;i<=n;++i){
		for(int j=1;j<=n;++j) cout<<a[i][j]<<' ';
		cout<<endl;
	}
	return 0;
}