第一题 求期望:
#include <bits/stdc++.h>
using namespace std;
double input[505][505];
double a[505];
int main()
{
int n, m;
cin>>n>>m;
double ans=0;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
scanf("%lf", &input[i][j]);
}
}
for(int j=0; j<m; j++){
for(int i=0; i<n; i++){
a[i] = input[i][j];
}
sort(a, a+n);
double sum = 0;
for(int i=0; i<n; i++){
sum += a[i];
ans += sum;
}
}
printf("%.7f\n", ans/n);
return 0;
}
第二题 求转移概率:
#include <bits/stdc++.h>
using namespace std;
double edg[4][4];
int main()
{
int t;
cin>>t;
while(t--){
int n;
cin>>n;
double p[4];
for(int i=0; i<3; i++)scanf("%lf", &p[i]);
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
scanf("%lf", &edg[i][j]);
}
}
for(int it=0; it<n; it++){
double new_p[4];
new_p[0]=new_p[1]=new_p[2]=0;
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
new_p[i] += p[j]*edg[i][j];
}
}
for(int i=0; i<3; i++)p[i]=new_p[i];
}
if(p[2]>0.5)printf("1\n");
else printf("0\n");
}
return 0;
}
第三题 求车队数量:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
int speed[maxn];
int main()
{
int n;
cin>>n;
for(int i=0; i<n; i++)scanf("%d", &speed[i]);
sort(speed, speed+n);
int l=0, r=0, ans=0;
for(r=0; r<n; r++){
while(speed[r]-speed[l] > 10){
l++;
}
ans = max(ans, r-l+1);
}
cout<<ans<<endl;
return 0;
}
第四题 求流满的水站数量:
#include <bits/stdc++.h>
using namespace std;
double f[11][11];
int main()
{
int n, t;
cin>>n>>t;
f[0][0]=t;
int ans = 0;
if(t>=1)ans++;
for(int i=1; i<n; i++){
for(int j=0; j<i+1; j++){
if(j==0){
f[i][j]=(f[i-1][j]-1)/2;
}
else if(j==i){
f[i][j]=(f[i-1][j-1]-1)/2;
}
else{
f[i][j]=(f[i-1][j]-1+f[i-1][j-1]-1)/2;
}
ans += (f[i][j]>=1 || abs(f[i][j]-1)<0.001);
// printf("%.3f ", f[i][j]);
}
// cout<<endl;
}
cout<<ans<<endl;
return 0;
}
第五题 求被围住的区域:
#include <bits/stdc++.h>
using namespace std;
int n;
int a[1005][1005];
int book[1005][1005];
int out;
int dx[5]={0, 1, 0, -1};
int dy[5]={1, 0, -1, 0};
void dfs(int x, int y){
// printf("%d %d\n", x, y);
book[x][y] = 1;
for(int i=0; i<4; i++){
int nowx = x + dx[i];
int nowy = y + dy[i];
if(nowx<0 || nowx>=n || nowy<0 || nowy>=n){
out = 1;
continue;
}
if(a[nowx][nowy]==0 && book[nowx][nowy]==0){
dfs(nowx, nowy);
}
}
// book[x][y] = out; //找到出口则无需再次搜索
}
void rs(int x, int y){
// printf("%d %d\n", x, y);
a[x][y] = 2;
for(int i=0; i<4; i++){
int nowx = x + dx[i];
int nowy = y + dy[i];
if(nowx<0 || nowx>= n || nowy<0 || nowy>=n){
continue;
}
// printf("%d %d\n", nowx, nowy);
if(a[nowx][nowy]==0) {
rs(nowx, nowy);
}
}
}
int main()
{
cin>>n;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
scanf("%d", &a[i][j]);
}
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
out = 0;
if(a[i][j]==0 && book[i][j]==0){
// book[i][j]=1;
dfs(i, j);
if(out==0){
rs(i, j);
}
}
}
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
printf(j==n-1?"%d\n":"%d ", a[i][j]);
}
}
return 0;
}