image similarity

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

Explanation: If two images have the same pixel color at the same position, they are said to have the same pixel at that position. The similarity of two images is defined as the percentage of the same pixels in the total pixels.
Input
The first line contains two integers m and n, representing 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 contains n integers 0 or 1, indicating the color of each pixel on the first black and white image. Separate two adjacent numbers with a single space.
After m lines, each line contains n integers 0 or 1, indicating the color of each pixel on the second black and white image. Separate two adjacent numbers with a single space.
Output
A real number representing 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

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int[][] a= new int[100][100];
        int[][] b= new int[100][100]; 
        int m, n, i, j;  
        Scanner scanner = new Scanner(System.in);
        m = scanner.nextInt();
        n = scanner.nextInt();
        for(i=0; i<m; i++)  
               for(j=0; j<n; j++)  
                   a[i][j] = scanner.nextInt();
         for(i=0; i<m; i++)  
               for(j=0; j<n; j++)  
                   b[i][j] = scanner.nextInt();

          int count = 0;  
            for(i=0; i<m; i++)  
                for(j=0; j<n; j++)  
                    if(a[i][j] == b[i][j])  
                        count++;  
          System.out.println( String .format("%.2f", (float)100 * count / (m * n)));
          scanner.close();
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326598754&siteId=291194637