PAT B1066图像过滤
输入样例:
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、将像素点用二维数组存放起来
2、输入的过程中进行判断,如果在替换的范围内就存储替换的数
3、遍历二维数组,按照对应的格式输出
#include<iostream> using namespace std; int pp [501] [501];//图片像素点的值 int main () { int M, N, start_rep, end_rep, rep;//M行N列数据,替换开始点为start,结束点为end,替换数为rep cin >> M >> N >> start_rep >> end_rep >> rep; //输入数据并进行判断和替换 for ( int i = 0; i < M; i++ ) { for ( int j = 0; j < N; j++ ) { cin >> pp [i] [j]; if ( pp [i] [j] >= start_rep&& pp [i] [j] <= end_rep ) { //在替换的范围内 pp [i] [j] = rep; } } } //按要求输出 for ( int i = 0; i < M; i++ ) { for ( int j = 0; j < N; j++ ) { if ( j != 0 ) printf ( " " ); if ( pp [i] [j] < 10) { printf ( "00%d", pp [i] [j] ); } else if ( pp [i] [j] >= 10 && pp [i] [j] < 100 ) { printf ( "0%d", pp [i] [j] ); } else { printf ( "%d", pp [i] [j] ); } } printf ( "\n" ); } system ( "pause" ); return 0; }
唯有热爱方能抵御岁月漫长。