仅供参考
7-1 ISBN号任务识别工作计划 (10 分)
输入样例:
5 5
1 2
1 3
2 4
3 4
4 5
输出样哩:
在这里给出相应的输出。例如:
1 2 3 4 5
#include<algorithm>
#include<iostream>
#include<set>
using namespace std;
int main()
{
int a, b, n, m;
set<int> g;
cin >> n >> m;
for (int i = 0; i < m; i++)
{
a = 0; b = 0;
cin >> a >> b;
g.insert(a);
g.insert(b);
}
for (set<int>::iterator in=g.begin();in!=g.end();in++)
{
cout << *in;
if (in != g.end())
cout << " ";
}
return 0;
}
7-2 读取包含ISBN号的图像文件的内容 (10 分)
输入样例:
3 3
7 7 8
2 6 9
1 3 5
10 12 14
16 17 19
15 20 11
200 219 207
221 230 218
243 231 229
输出样例:
在这里给出相应的输出。例如:
3 3
7 7 8
2 6 9
1 3 5
5.33
10 12 14
16 17 19
15 20 11
14.89
200 219 207
221 230 218
243 231 229
222.00
#include <iostream>
using namespace std;
int main()
{
int m, n;
cin >> m >> n;
int **arry[3];
for (int i = 0; i < 3; i++)
arry[i] = new int* [m];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < m; j++)
arry[i][j] = new int[n];
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < m; j++)
for (int k = 0; k < n; k++)
cin >> arry[i][j][k];
getchar();
}
double sum[3] = { 0 };
double s;
for (int i = 0; i < 3; i++)
{
s = 0;
for (int j = 0; j < m; j++)
{
for (int k = 0; k < n; k++)
s += arry[i][j][k];
}
sum[i] = s / (m * n);
}
cout << m << " " << n << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < m; j++)
{
for (int k = 0; k < n; k++)
cout << arry[i][j][k] << " ";
cout << endl;
}
printf("%.2f", sum[i]);
if (i < 2)
{
cout << endl;
cout << endl;
}
}
}
7-3 将24真彩色图转换为灰度图像 (10 分)
输入样例:
3 3 0
3 6 8
4 8 6
3 9 5
1 4 8
5 2 8
2 5 9
2 5 7
7 5 2
3 9 6
输出样例:
1 4 7
4 4 6
2 6 7
#include<iostream>
#include<algorithm>
using namespace std;
//R0.299 + G0.587 + B0.114
void math_0(double*** &a,int n,int m)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < m; k++)
{
switch (i)
{
case 0:
a[i][j][k] *= 0.299;
break;
case 1:
a[i][j][k] *= 0.587;
break;
case 2:
a[i][j][k] *= 0.114;
break;
default:
break;
}
}
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < n; j++)
for (int k = 0; k < m; k++)
a[3][j][k] += a[i][j][k];
}
}
//(R + G + B) / 3
void math_1(double***& a, int n, int m)
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < m; k++)
a[3][j][k] += a[i][j][k];
for (int j = 0; j < n; j++)
for (int k = 0; k < m; k++)
a[3][j][k] =a[3][j][k]/ 3;
}
//max(R, G, B)
void math_2(double***& a, int n, int m)
{
double s[3];
for (int j = 0; j < n; j++)
{
for (int k = 0; k < m; k++)
{
s[0] = a[0][j][k];
s[1] = a[1][j][k];
s[2] = a[2][j][k];
sort(s, s + 3);
a[3][j][k] = s[2];
}
}
}
int main()
{
int n, m;
char type;
cin >> n >> m >> type;
double*** arry = new double** [4];
for (int i = 0; i < 4; i++)
arry[i] = new double* [n];
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < n; j++)
arry[i][j] = new double[m];
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < n; j++)
for (int k = 0; k < m; k++)
cin >> arry[i][j][k];
}
for (int j = 0; j < n; j++)
for (int k = 0; k < m; k++)
arry[3][j][k] = 0;
switch (type)
{
case '0':
math_0(arry,n,m);
for (int j = 0; j < n; j++)
{
for (int k = 0; k < m; k++)
{
int s = arry[3][j][k];
cout << s;
if (k < m - 1)
cout << " ";
}
if (j < n - 1)
cout << endl;
}
break;
case '1':
math_1(arry, n, m);
for (int j = 0; j < n; j++)
{
for (int k = 0; k < m; k++)
{
int s = arry[3][j][k];
cout << s;
if (k < m - 1)
cout << " ";
}
if (j < n - 1)
cout << endl;
}
break;
case '2':
math_2(arry, n, m);
for (int j = 0; j < n; j++)
{
for (int k = 0; k < m; k++)
{
int s = arry[3][j][k];
cout << s;
if (k < m - 1)
cout << " ";
}
if (j < n - 1)
cout << endl;
}
break;
default:
break;
}
return 0;
}
7-4 做出灰度图的灰度直方图 (10 分)
输入样例:
4 5
1 3 7 9 12
4 4 8 10 12
4 3 8 9 10
1 4 10 12 8
输出样例:
在这里给出相应的输出。例如:
1:2
3:2
4:4
7:1
8:3
9:2
10:3
12:3
#include<iostream>
using namespace std;
int main()
{
int n=0, m=0;
int pin[256] = { 0 };
cin >> n >> m;
int a;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> a;
pin[a] += 1;
}
}
for (int i = 0; i < 256; i++)
{
if (pin[i] == 0)
continue;
cout << i << ":" << pin[i]<<endl;
}
return 0;
}
7-5 找最小的值做图像分割 (10 分)
输入样例:
6
3 5 8 6 7 2
输出样例:
2 3 5 6 7 8
灰度级频数最小的值为:2
#include<iostream>
using namespace std;
void bubbleSort(int arr[], int n)
{
for (int i = 0; i < n; i++) {
//比较两个相邻的元素
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = t;
}
}
}
}
int main()
{
int n = 0;
cin >> n;
int* a = new int[n];
for (int i = 0; i < n; i++)
cin >> a[i];
bubbleSort(a, n);
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
cout <<"灰度级频数最小的值为:" <<a[0];
return 0;
}
7-6 标识每一个数字图元区域 (10 分)
输入样例:
4 6
0 1 1 0 0 1
1 1 1 0 1 1
0 0 0 0 1 1
1 1 1 0 0 1
输出样例:
0 1 1 0 0 2
1 1 1 0 2 2
0 0 0 0 2 2
3 3 3 0 0 2
无(代码有待完善)
7-7 提取每一个数字的最小平行外接矩阵 (10 分)
输入样例:
10 17
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0
0 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 0
0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0
0 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 0
0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0
0 0 0 1 1 1 0 0 0 0 0 1 1 1 0 0 0
0 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 0
0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
输出样例:
(1 1) (8 15)
#include<iostream>
using namespace std;
int main()
{
int m, n;
cin >> m >> n;
int** arry = new int* [m];
for (int i = 0; i < m; i++)
arry[i] = new int[n];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cin >> arry[i][j];
}
}
int a = 0, b = 0, c = 0, d = 0;
bool tep = true;
for (int j = 0; j < n; j++)
{
for (int i = 0; i < m; i++)
{
if (arry[i][j] == 1 && tep)
{
a = j;
tep = false;
}
if (arry[i][j] == 1)
b = j;
}
}
tep = true;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (arry[i][j] == 1 && tep)
{
c = i;
tep = false;
}
if (arry[i][j] == 1)
d = i;
}
}
printf("(%d %d) (%d %d)", c, a, d, b);
return 0;
}
7-8 数字图像尺度归一化 (10 分)
输入样例:
2 2 7 7
1 0
0 1
输出样例:
1 1 1 1 0 0 0
1 1 1 1 0 0 0
1 1 1 1 0 0 0
1 1 1 1 0 0 0
0 0 0 0 1 1 1
0 0 0 0 1 1 1
0 0 0 0 1 1 1
#include<iostream>
using namespace std;
int main()
{
int m, n, p, q;
cin >> m >> n >> p >> q;
int** arry = new int * [m];
int** arry1 = new int* [p];
for (int i = 0; i < m; i++)
arry[i] = new int[n];
for (int i = 0; i < p; i++)
arry1[i] = new int[q];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
cin >> arry[i][j];
int a, b;
for (int i = 0; i < p; i++)
{
for (int j = 0; j < q; j++)
{
a = i * m / p;
b = j * n / q;
arry1[i][j] = arry[a][b];
cout << arry1[i][j];
if (j < q - 1)
cout << " ";
}
if (i < p - 1)
cout << endl;
}
return 0;
}
7-9 中值滤波 (10 分)
输入样例:
4 4
1 5 6 7
3 4 2 8
9 6 3 2
1 5 8 6
输出样例:
1 5 6 7
3 4 5 8
9 4 5 2
1 5 8 6
#include<iostream>
#include<algorithm>
using namespace std;
int paixu(int** a)
{
int* b = new int[100];
int s = 0;
int index = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
b[index] = a[i][j];
index++;
}
}
sort(b, b + 9);
s = b[4];
return s;
}
int main()
{
int n, m;
cin >> n >> m;
int** arry = new int* [n];//用于存放输入的矩阵
int** midd = new int* [n];//用于存放矩阵(除边缘外)改变后的数值
for (int i = 0; i < n; i++)
{
arry[i] = new int[m];
midd[i] = new int[m];
}
for (int i = 0; i < n ; i++)
{
for (int j = 0; j < m ; j++)
{
cin >> arry[i][j];
}
}
int** tep = new int* [3];
for (int i = 0; i < 3; i++)
tep[i] = new int[3];
for (int i = 1; i < n - 1; i++)
{
for (int j = 1; j < m - 1; j++)
{
tep[1][1] = arry[i][j];
tep[0][1] = arry[i-1][j];
tep[1][0] = arry[i][j-1];
tep[0][0] = arry[i-1][j-1];
tep[0][2] = arry[i-1][j+1];
tep[1][2] = arry[i][j+1];
tep[2][0] = arry[i+1][j-1];
tep[2][1] = arry[i+1][j];
tep[2][2] = arry[i+1][j+1];
midd[i][j] = paixu(tep);
}
}
for (int i = 1; i < n - 1; i++)
{
for (int j = 1; j < m - 1; j++)
{
arry[i][j] = midd[i][j];
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << arry[i][j];
if (j < m - 1)
cout << " ";
}
if (i < n - 1)
cout << endl;
}
return 0;
}
7-10 计算正确率和准确率 (10 分)
输入样例:
3
9787111478188 9787111470108
927086257902X 927086257902X
8927518936148 093851895614X
输出样例:
0.33 0.82
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n;
cin >> n;
string** str = new string* [n];
for (int i = 0; i < n; i++)
str[i] = new string[2];
for (int i = 0; i < n; i++)
for (int j = 0; j < 2; j++)
cin >> str[i][j];
double flag = 0, s,t=0, num = 0;
for (int i = 0; i < n; i++)
{
s = 0;
t += str[i][0].size();
for (int j = 0; j < str[i][0].size(); j++)
{
if (str[i][1][j] == str[i][0][j])
s++;
}
num += s;
if (s == str[i][0].size())
flag++;
}
printf("%.2f %.2f", flag / n, num / t);
return 0;
}