第一行包含两个整数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
#include <stdio.h> #include <stdlib.h> void init(int m, int n, int x[m][n]); int main(void) { int m, n; float percent; int i, j, count; scanf("%d %d", &m ,&n); int (*a)[n] = (int (*)[n])malloc(sizeof(int) * m * n); if (NULL == a) { fprintf(stderr, "Memory Allocation failed!\n"); exit(EXIT_FAILURE); } int (*b)[n] = (int (*)[n])malloc(sizeof(int) * m * n); if (NULL == b) { fprintf(stderr, "Memory Allocation failed!\n"); exit(EXIT_FAILURE); } init(m, n, a); init(m, n, b); for (i = 0, count = 0; i < m; i++) { for (j = 0; j < n; j++) { if (a[i][j] == b[i][j]) { count++; } } } percent = (float)count / (m * n); printf("%.2f\n", percent * 100); return 0; } void init(int m, int n, int x[m][n]) { int i, j; for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { scanf("%d", &x[i][j]); } } return; }
#include <stdio.h> int main(){ int n, m, arr[10][10]; int x, count = 0; scanf("%d %d", &n, &m); for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++) scanf("%d", &arr[i][j]); } for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ scanf("%d", &x); if(x == arr[i][j]) count++; } } printf("%.2f", (float)count / (n * m) * 100); return 0; }
#include <iostream> using namespace std; int main() { int n, m; scanf("%d%d", &m, &n); int a[m * n]; int b[m * n]; for (int i = 0; i < m * n; i ++ ) scanf("%d", &a[i]); for (int j = 0; j < m * n; j ++ ) scanf("%d", &b[j]); int sum = 0; for (int i = 0; i < m * n; i ++ ) { if (a[i] == b[i]) sum ++; } printf("%.2lf", sum * 100.0 / m / n); }
a = input().split() b = [] while True: try: total = 0 b.append(input().split()) except: y,x = map(int,a) al = x * y for i in range(y,2*y): for j in range(x): if b[i][j] == b[i-y][j]: total += 1 t = total/al print("{:.2f}".format(t*100)) if t: break
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int m = scanner.nextInt(); int n = scanner.nextInt(); int[] array = new int[m*n]; int similar = 0; int num = 0; for(int i=0;i<m*n;i++) { array[i] = scanner.nextInt(); } //不必创建两个数组,另一个直接比较即可 for(int i=0;i<m*n;i++) { num = scanner.nextInt(); if(num==array[i]) { similar++; } } System.out.printf("%.2f",similar*1.0/(m*n)*100); }
#include <iostream> #include <iomanip> using namespace std; int pic1[10001]; int pic2[10001]; int main(){ int a, b, c; double count = 0; cin >> a >> b; c = a * b; for(int t = 0; t < c; t++){ cin >> pic1[t]; } for(int t = 0; t < c; t++){ cin >> pic2[t]; count += pic2[t] == pic1[t] ? 1: 0; } cout << fixed << setprecision(2) << count * 100.00 / c; return 0; }题目要求是相似度,没必要开二维数组,直接初始化长度为长乘宽的一维数组即可,我们只需要把两个数组都比较一遍,然后统计出有多少相同的数字,最后计算出百分比,结束。
#include <cstdio> int main(){ int twoImage[210][110], m, n; float count = 0; scanf("%d %d", &m, &n); for(int i = 0; i < 2*m; i++) for(int j = 0; j < n; j++) scanf("%d", &twoImage[i][j]); for(int i = 0; i < m; i++) for(int j = 0; j < n; j++){ if(twoImage[i][j] == twoImage[i+m][j]) count++; } printf("%.2f", 100*count/(m*n)); return 0; }
#include <stdio.h> int main() { int m, n, i, j; scanf("%d %d", &m, &n); int arr1[100][100] = {0}; int arr2[100][100] = {0}; for(i=0; i<m; i++) for(j=0; j<n; j++) scanf("%d ", &arr1[i][j]); for(i=0; i<m; i++) for(j=0; j<n; j++) scanf("%d ", &arr2[i][j]); int count = 0; for(i=0; i<m; i++) for(j=0; j<n; j++) if(arr1[i][j] == arr2[i][j]) count++; float per = 100.0 * count / (m*n); printf("%.2f\n", per); return 0; }
int main() { int m,n,a[100],b[100],i,cnt=0; float per; while(~scanf("%d%d",&m,&n)){ for(i=0;i<m*n;i++){ scanf("%d",&a[i]); } for(i=0;i<m*n;i++){ scanf("%d",&b[i]); } for(i=0;i<m*n;i++) { if(a[i]==b[i]) cnt++; } printf("%.2f",per=cnt/(m*n*1.0)*100); } return 0; }
#include <stdio.h> int main() { int m,n=0; scanf("%d %d",&m,&n); int arr1[100][100]={0}; int arr2[100][100]={0}; int i,j,count=0; for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&arr1[i][j]); } } for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&arr2[i][j]); } } for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(arr1[i][j]==arr2[i][j]) { count++; } } } printf("%.2f\n",100.0*count/(m*n)); return 0; }
#include <stdio.h> int main() { int n = 0,m = 0,count = 0; double percent = 0.; int arr[106][106] = {0}; int arr2[106][106] = {0}; scanf("%d%d",&n,&m); for(int i = 0;i<n;i++) { for(int j = 0;j<m;j++) { scanf("%d",&arr[i][j]);//输入 } } for(int i = 0;i<n;i++) { for(int j = 0;j<m;j++) { scanf("%d",&arr2[i][j]);//输入 } } for(int i = 0;i<n;i++) { for(int j = 0;j<m;j++) { if(arr[i][j]==arr2[i][j])//记录相同 count++; } } percent = (double)count/(n*m)*100;//计算百分比 printf("%.2lf",percent); return 0; }
#include<stdio.h> int main() { int arr[100][100] = {0}; int input = 0, n, m, count = 0; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) scanf("%d", &arr[i][j]); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { scanf("%d", &input); if (input == arr[i][j]) count++; } } float ret = (1.0 * count / (n * m) * 1.0) * 100; printf("%.2f\n", ret); return 0; }
m,n=map(int,input().split()) a=[] b=[] for i in range(m): a.append(list(map(int,input().split()))) for i in range(m): b.append(list(map(int,input().split()))) x=0 for i in range(m): for j in range(n): if a[i][j]==b[i][j]: x=x+1 print('{:.2f}'.format((x*100)/(m*n)))
#include <stdio.h> int main() { int m,n; scanf("%d %d",&m,&n); int array_1[m][n]; int array_2[m][n]; int i,j,count=0; for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&array_1[i][j]); } } for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&array_2[i][j]); } } for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(array_1[i][j]==array_2[i][j]) { count++; } } } printf("%.2f",100.0*count/(m*n)); return 0; }
n,m = map(int,input().split()) # map函数用法 arr = [input().split() for _ in range(n)] # 列表推导式 arr1 = [input().split() for _ in range(n)] total = n*m # 总像素点数 count = 0 # 计数相同点 for i in range(n): # for循环 for j in range(m): if arr[i][j] == arr1[i][j]: count += 1 per = count/total*100 # 百分比 print(f"{per:.2f}") # 格式化输出
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), m = in.nextInt(); int[][] num_1 = new int[n][m]; int[][] num_2 = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { num_1[i][j] = in.nextInt(); } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { num_2[i][j] = in.nextInt(); } } int count = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (num_1[i][j] == num_2[i][j]) { count++; } } } double ret = ((count * 1.00) / (n * m)) * 100; System.out.printf("%.2f", ret); } }
#include <stdio.h> int main() { int line=0,row=0; scanf("%d%d", &line, &row); int arr[50][50] = { 0 }; int drr[50][50] = { 0 }; int i = 0; int j = 0; float sum = line*row;//总的像素点 //第一幅图像 for (i = 0; i < line; i++) { for (j = 0; j < row; j++) { scanf("%d", &arr[i][j]); } } //第二幅图像 for (i = 0; i < line; i++) { for (j = 0; j < row; j++) { scanf("%d", &drr[i][j]); } } int count = 0;//统计相同像素点颜色 for (i = 0; i < line; i++) { for (j = 0; j < row; j++) { if (arr[i][j] == drr[i][j]) { count++; } } } float aver = ((float)count / sum)*100; printf("%.2f", aver); }