图像过滤是把图像中不重要的像素都染成背景色,使得重要部分被凸显出来。现给定一幅黑白图像,要求你将灰度值位于某指定区间内的所有像素颜色都用一种指定的颜色替换。
输入在第一行给出一幅图像的分辨率,即两个正整数 M 和 N(0),另外是待过滤的灰度值区间端点 A 和 B(0)、以及指定的替换灰度值。随后 M 行,每行给出 N 个像素点的灰度值,其间以空格分隔。所有灰度值都在 [0, 255] 区间内。
输出按要求过滤后的图像。即输出 M 行,每行 N 个像素灰度值,每个灰度值占 3 位(例如黑色要显示为 000
),其间以一个空格分隔。行首尾不得有多余空格。
3 5 100 150 0
3 189 254 101 119
150 233 151 99 100
88 123 149 0 255
003 189 254 000 000
000 233 151 099 000
088 000 000 000 255
1 #include <iostream> 2 #include <iomanip> 3 #include <vector> 4 using namespace std; 5 //1066:图像过滤 6 int main() { 7 int m, n, a, b, replace; 8 cin >> m >> n >> a >> b >> replace; 9 vector<vector<int> > pic(m, vector<int>(n)); 10 for (int i = 0; i < m; i++) { 11 for (int j = 0; j < n; j++) { 12 cin >> pic[i][j]; 13 if (pic[i][j] >= a && pic[i][j] <= b) { 14 pic[i][j] = replace; 15 } 16 } 17 } 18 for (int i = 0; i < m; i++) { 19 for (int j = 0; j < n; j++) { 20 if (j != 0) { 21 cout << ‘ ‘; 22 } 23 cout << setw(3) << setfill(‘0‘) << pic[i][j]; 24 } 25 cout << endl; 26 } 27 return 0; 28 }
最后一个测试点超时
1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStreamReader; 4 5 public class Main { 6 public static void main(String[] args) throws IOException { 7 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 8 String[] s = in.readLine().split("\\s+"); 9 int[] m = new int[5]; 10 for(int i = 0; i < 5; i++){ 11 m[i] = Integer.parseInt(s[i]); 12 } 13 for (int i = 0; i < m[0]; i++) { 14 String[] str = in.readLine().split("\\s+"); 15 for (int j = 0; j < m[1]; j++) { 16 int p = Integer.parseInt(str[j]); 17 if (p >= m[2] && p <= m[3]) { 18 p = m[4]; 19 } 20 if (j != 0) 21 System.out.print(" "); 22 System.out.printf("%03d", p); 23 } 24 System.out.println(); 25 } 26 } 27 }
原文:https://www.cnblogs.com/47Pineapple/p/14700274.html