跳到正文

个人记录

第13次课堂总结

jhy321约 3 分钟
原文
文章信息与版本

文章信息

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

版本

  1. v1当前

禽兽的传染病

错因

没初始化。

思路

首先分析题目,每次感染n个禽兽,那么肯定和n的次方有关系,m轮就是m次。至于+1的原因是要包括自己,所以要n+1,就可以得到结果。

C++
#include<bits/stdc++.h>
using namespace std;
unsigned long long n,m,f,a[10001],s[10001];
int main(){
	cin>>n>>m;
    f=1;
	for(int i=1;i<=m;i++){
		f*=n+1;
    }
    cout<<f;
	return 0;
}

滑雪课程设计Ski Course Design

错因

条件不对。

思路

遍历所有可能的高度,并且求出符合条件的最小和最大,并以界限为界判断高低,若高于则全部削去,低于则削去差大于17的部分。

C++
#include<bits/stdc++.h>
using namespace std;
long long n,ans,y=1000001,f,a[100001];
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
    }
    for(int i=0;i+17<=100;i++){
    	int f=0,j=i+17;
    	for(int k=1;k<=n;k++){
    		if(a[k]<i){
    			f+=(i-a[k])*(i-a[k]);
			}else if(a[k]>j){
				f+=(a[k]-j)*(a[k]-j);
			}
		}
		if(f<y)y=f;
	}
	cout<<y;
	return 0;
}

全排列问题

错因

全排列不够熟练。

思路

全排列模板。

C++
#include<bits/stdc++.h>
using namespace std;
long long n,m,f,a[10001],s[10001];
void c(long long st){
	if(st>n){
		for(int i=1;i<=n;i++)cout<<"    "<<a[i];
		cout<<endl;
		return ;
	}
	for(int i=1;i<=n;i++){
		if(s[i]==0){
			s[i]=1;
			a[st]=i;
			c(st+1);
			s[i]=0;
		}
	}
}
int main(){
	cin>>n;
	c(1);
	return 0;
}

Out of Place B

错因

更改的区间有误

思路

我们可以通过排序后的序列与原始的序列进行比较,判断是否进行过交换,若进行过交换则记录,最后计算交换次数。

C++
#include<bits/stdc++.h>
using namespace std;
long long n,m,f,a[10001],s[10001],x;
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		s[i]=a[i];
    }
    sort(s+1,s+1+n);
    for(int i=1;i<=n;i++){
		if(a[i]!=s[i]){
			x++;
		}
    }
    cout<<x-1;
	return 0;
}
原文