C/C++ Programming Learning-Week 6 ⑥ Image similarity

Topic link

Title description

Given two black and white images of the same size (using a 0-1 matrix), find their similarity.

Note: If two images have the same pixel color at the same position, it is said that they have the same pixel at that position. The similarity of two images is defined as the percentage of the same number of pixels in the total number of pixels.

Input format The
first line contains two integers m and n, which represent the number of rows and columns of the image, separated by a single space. 1 ≤ m ≤ 100, 1 ≤ n ≤ 100.

After m lines, each line has n integers 0 or 1, indicating the color of each pixel on the first black and white image. Use a single space to separate two adjacent numbers.

After m lines, each line has n integers 0 or 1, indicating the color of each pixel on the second black and white image. Use a single space to separate two adjacent numbers.

Output format
A real number, which represents the similarity (given as a percentage), accurate to two decimal places.

Sample Input

3 3
1 0 1
0 0 1
1 1 0
1 1 0
0 0 1
0 0 1

Sample Output

44.44

Ideas

The idea is to input two images first, save them in a two-dimensional array; then compare them one by one to see which ones are the same, and finally divide by (m * n). , Is the degree of similarity, and the title is required to be output as a percentage, so ×100.0 is required.

C++ code:

#include<bits/stdc++.h>
using namespace std;
int ma1[105][105], ma2[105][105], m, n;
int main()
{
    
    
	while(cin >> m >> n)
	{
    
    
		memset(ma1, 0, sizeof(ma1));
		memset(ma2, 0, sizeof(ma2));
		int cnt = 0;
		for(int i = 0; i < m; i++)
			for(int j = 0; j < n; j++)
				cin >> ma1[i][j];
		for(int i = 0; i < m; i++)
			for(int j = 0; j < n; j++)
				cin >> ma2[i][j];
		for(int i = 0; i < m; i++)
			for(int j = 0; j < n; j++)
				if(ma1[i][j] == ma2[i][j]) cnt++;
		printf("%.2lf\n", cnt * 100.0 / (m * n));
	}
	return 0;
}

For students who don’t have C language foundation, you can learn the C language grammar first. I will sort it out and send it out later.
I have already written it. You can go to the C language programming column to see the content of the first week .

Other exercises this week:

C language programming column

C/C++ Programming Learning-Week 6① Calculate A+B (Novice Course)

C/C++ programming learning-week 6② A*B problem

C/C++ Programming Learning-Week 6 ③ Class size

C/C++ Programming Learning-Week 6 ④ Sum of odd numbers

C/C++ Programming Learning-Week 6 ⑤ Calculation of the bounce height of the ball

C/C++ Programming Learning-Week 6 ⑥ Image similarity

C/C++ Programming Learning-Week 6 ⑦ Separate each digit of an integer

C/C++ Programming Learning-Week 6⑧ Simple Calculator

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/112911932