保存第一幅图像后逐位置比较第二幅图像,统计相同像素比例。
OJ: noi_openjudge
题目 ID: ch0108-06
难度:入门
标签:矩阵模拟python
日期: 2026-07-30 23:01
题意
计算两幅相同大小黑白图像的相同像素比例并保留两位小数。
思路
先保存第一幅图像。读入第二幅图像的每一行时,用 zip 逐位置比较并累计相同数,最后除以总像素数并乘
代码
cpp
#include <cstdio>
int m,n;
int map[200][200];
int main(){
int i,j;
scanf("%d%d",&m,&n);
for (i=1;i<=m;i++){
for (j=1;j<=n;j++){
scanf("%d",&map[i][j]);
}
}
int t,cnt=0;
for (i=1;i<=m;i++){
for (j=1;j<=n;j++){
scanf("%d",&t);
if( map[i][j] == t)
cnt++;
}
}
double ans = cnt*100.0/(m*n);
printf("%.2lf\n",ans);
return 0;
}复杂度
总结
Python代码
python
row_count, column_count = map(int, input().split())
first_image = [input().split() for _ in range(row_count)]
same_count = 0
for row in range(row_count):
second_row = input().split()
same_count += sum(left == right for left, right in zip(first_image[row], second_row))
print(f"{same_count * 100 / (row_count * column_count):.2f}")C++代码
cpp
#include <cstdio>
int m,n;
int map[200][200];
int main(){
int i,j;
scanf("%d%d",&m,&n);
for (i=1;i<=m;i++){
for (j=1;j<=n;j++){
scanf("%d",&map[i][j]);
}
}
int t,cnt=0;
for (i=1;i<=m;i++){
for (j=1;j<=n;j++){
scanf("%d",&t);
if( map[i][j] == t)
cnt++;
}
}
double ans = cnt*100.0/(m*n);
printf("%.2lf\n",ans);
return 0;
}复杂度
时间复杂度为
总结
两组同形矩阵的逐位置比较可沿输入同步完成。