版权声明:代码属于原创,转载请联系作者并注明出处。 https://blog.csdn.net/weixin_43379056/article/details/84971682
1053. 相似度
题目描述
给出两幅相同大小的黑白图像(用0-1矩阵)表示,求它们的相似度。
说明:若两幅图像在相同位置上的像素点颜色相同,则称它们在该位置具有相同的像素点。两幅图像的相似度定义为相同像素点数占总像素点数的百分比。
输入
第一行包含两个整数m和n,表示图像的行数和列数,中间用单个空格隔开。1 <= m <= 100, 1 <= n <= 100。
之后m行,每行n个整数0或1,表示第一幅黑白图像上各像素点的颜色。相邻两个数之间用单个空格隔开。
之后m行,每行n个整数0或1,表示第二幅黑白图像上各像素点的颜色。相邻两个数之间用单个空格隔开。
输出
一个实数,表示相似度(以百分比的形式给出),精确到小数点后两位。
样例输入
3 3
1 0 1
0 0 1
1 1 0
1 1 0
0 0 1
0 0 1
样例输出
44.44
数据范围限制
1 <= m <= 100, 1 <= n <= 100
C++代码
#include <iostream>
#include <cassert>
#include <iomanip>
using namespace std;
int main()
{
const int MAX = 100;
int Matrix1[MAX][MAX], Matrix2[MAX][MAX];
int m, n;
cin >> m >> n;
assert(m>=1 && m<=MAX);
assert(n>=1 && n<=MAX);
// input data for 1st picture
for(int i=1; i<=m; i++)
{
for(int j=1; j<=n; j++)
{
cin >> Matrix1[i-1][j-1];
}
}
// input data for 2nd picture
for(int i=1; i<=m; i++)
{
for(int j=1; j<=n; j++)
{
cin >> Matrix2[i-1][j-1];
}
}
// compare two pictures pixel by pixel
float numOfSamePixels = 0;
for(int i=1; i<=m; i++)
{
for(int j=1; j<=n; j++)
{
if (Matrix1[i-1][j-1] == Matrix2[i-1][j-1])
{
numOfSamePixels++;
}
}
}
// calculate similarity degree of the two pictures
float similarity_degree = 100*numOfSamePixels/(n*m);
cout << setiosflags(ios::fixed);
cout << setprecision(2) << similarity_degree << endl;
return 0;
}