PTA乙级1066

1066

图像过滤是把图像中不重要的像素都染成背景色,使得重要部分被凸显出来。现给定一幅黑白图像,要求你将灰度值位于某指定区间内的所有像素颜色都用一种指定的颜色替换。

输入格式:

输入在第一行给出一幅图像的分辨率,即两个正整数 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

思路:这题我一开始完全没没看懂什么意思,后来结合别人的思路才明白,对于替换和补零,我看到一个很好的写法
写法博客来源
目前没发现其他更好的写法 String str= new DecimalFormat("000").format(i);用这个来使3变为003.但是他里面用了好多.length明显多此一举了,已经给了A,B就用A,B就行了.

代码:

package PTA2;

import java.text.DecimalFormat;
import java.util.Scanner;

public class PTA1066 {
    public static void main(String[] replacegs) {
        Scanner sc = new Scanner(System.in);
        int A=sc.nextInt();
        int B=sc.nextInt();
        int M=sc.nextInt();
        int N=sc.nextInt();
        int e=sc.nextInt();
        int[][] replace = new int[A][B];
        for(int i=0;i<A;i++) {
            for(int j=0;j<B;j++) {
                replace[i][j]=sc.nextInt();
            }
        }
        for(int i=0;i<A;i++) {
            for(int j=0;j<B-1;j++) {
                if(replace[i][j]>=M&&replace[i][j]<=N) {
                    String str =  new DecimalFormat("000").format(e);
                    System.out.print(str+" ");
                }
                else {
                    String str =  new DecimalFormat("000").format(replace[i][j]);
                    System.out.print(str+" ");
                }
            }
            if(replace[i][B-1]>=M&&replace[i][B-1]<=N) {
                String str =  new DecimalFormat("000").format(e);
                System.out.println(str);
            }else {
                String str =  new DecimalFormat("000").format(replace[i][B-1]);
                System.out.println(str);
            }

        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值