1066. 图像过滤(15)--PAT乙级真题java实现

个人博客:小景哥哥

1066. 图像过滤(15)–PAT乙级真题java实现

图像过滤是把图像中不重要的像素都染成背景色,使得重要部分被凸显出来。现给定一幅黑白图像,要求你将灰度值位于某指定区间内的所有像素颜色都用一种指定的颜色替换。
输入格式:
输入在第一行给出一幅图像的分辨率,即两个正整数M和N(0 < M, N <= 500),另外是待过滤的灰度值区间端点A和B(0 <= A < B <= 255)、以及指定的替换灰度值。随后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


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) throws IOException{
        Scanner sc = new Scanner(System.in);
        int M = sc.nextInt(), N = sc.nextInt(), A = sc.nextInt(), B = sc.nextInt(), filter = sc.nextInt();
        for(int i = 0; i < M; i++) {
            for(int j = 0; j < N; j++) {
                int rgb = sc.nextInt();
                if(rgb >= A && rgb <= B) {
                    rgb = filter;
                }
                if(j != 0)
                    System.out.print(" ");
                System.out.printf("%03d",rgb);
            }
            System.out.println();
        }

    }
}
/**
 * solution tow
 * @param args
 * @throws IOException
 */
class solution{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] s = br.readLine().split(" ");
        int M = Integer.parseInt(s[0]), N = Integer.parseInt(s[1]),
                A = Integer.parseInt(s[2]), B = Integer.parseInt(s[3]),
                filter = Integer.parseInt(s[4]);
        for(int i = 0; i < M; i++) {
            String[] sp = br.readLine().split(" ");
            for(int j = 0; j < N; j++) {
                int rgb = Integer.parseInt(sp[j]);
                if(rgb >= A && rgb <= B) {
                    rgb = filter;
                }
                if(j != 0)
                    System.out.print(" ");
                System.out.printf("%03d",rgb);
            }
            System.out.println();
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

勤奋的凯尔森同学

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值