寄生虫做网站流量,网业大全,手机怎么做网站服务器,网站图片分辨率#x1f36d; 大家好这里是清隆学长 #xff0c;一枚热爱算法的程序员 ✨ 本系列打算持续跟新华为OD-C/D卷的三语言AC题解 #x1f4bb; ACM银牌#x1f948;| 多次AK大厂笔试 #xff5c; 编程一对一辅导 #x1f44f; 感谢大家的订阅➕ 和 喜欢#x1f497; #x1f… 大家好这里是清隆学长 一枚热爱算法的程序员 ✨ 本系列打算持续跟新华为OD-C/D卷的三语言AC题解 ACM银牌| 多次AK大厂笔试 编程一对一辅导 感谢大家的订阅➕ 和 喜欢 在线评测链接
https://app5938.acapp.acwing.com.cn/contest/2/problem/OD1087 评测功能需要 ⇒ 订阅专栏 ⇐ 后私信联系清隆解锁
OJ题目截图 文章目录 在线评测链接OJ题目截图 灰度图像恢复问题描述输入格式输出格式样例输入 1样例输出 1样例输入 2样例输出 2样例解释数据范围题解参考代码 灰度图像恢复
问题描述
在计算机中,黑白图像常采用灰度图的方式存储。每个像素填充一个灰阶值,范围为 0 − 255 0-255 0−255,其中 0 0 0 表示全黑, 255 255 255 表示全白,其他值表示不同的灰度。为了节省存储空间,图像会使用压缩算法进行存储。
一种压缩算法的格式如下:
行数 列数 灰阶值1 连续像素个数1 灰阶值2 连续像素个数2 ...其中,前两个数分别表示矩阵的行数和列数。从第三个数开始,每两个数一组,第一个数为灰阶值,第二个数表示该灰阶值从左到右、从上到下连续出现的像素个数。
给定压缩后的图像数据和一个像素位置,请恢复原始灰度图矩阵,并输出指定像素位置的灰阶值。
输入格式
第一行为压缩后的图像数据,格式如上所述。
第二行包含两个整数 r r r 和 c c c,用空格分隔,表示要查询的像素位置的行号和列号。行号和列号从 0 0 0 开始计数。
输出格式
输出一个整数,表示指定像素位置的灰阶值。
样例输入 1
10 10 255 34 0 1 255 8 0 3 255 6 0 5 255 4 0 7 255 2 0 9 255 21
3 4样例输出 1
0样例输入 2
10 10 255 34 0 1 255 8 0 3 255 6 0 5 255 4 0 7 255 2 0 9 255 21
3 5样例输出 2
255样例解释
根据压缩数据恢复后的灰度图矩阵,在第一个样例中,第 3 3 3 行第 4 4 4 列的像素灰阶值为 0 0 0;在第二个样例中,第 3 3 3 行第 5 5 5 列的像素灰阶值为 255 255 255。
数据范围
图像大小不超过 100 × 100 100 \times 100 100×100。压缩数据长度不超过 1 0 4 10^4 104。
题解
根据压缩数据的格式,逐步恢复出原始的灰度图矩阵。遍历压缩数据,对于每一组灰阶值和连续像素个数,将对应的像素在矩阵中填充相应的灰阶值。最后输出指定位置的像素灰阶值即可。
参考代码
Python
def soln(data, r, c):rows, cols, *pixels map(int, data.split())matrix [[0] * cols for _ in range(rows)]x, y 0, 0for i in range(0, len(pixels), 2):val, cnt pixels[i], pixels[i1]for _ in range(cnt):matrix[x][y] valy 1if y cols:y 0x 1return matrix[r][c]data input()
r, c map(int, input().split())
print(soln(data, r, c))Java
import java.util.Scanner;public class Main {public static int soln(String data, int r, int c) {Scanner scanner new Scanner(data);int rows scanner.nextInt();int cols scanner.nextInt();int[][] matrix new int[rows][cols];int x 0, y 0;while (scanner.hasNextInt()) {int val scanner.nextInt();int cnt scanner.nextInt();for (int i 0; i cnt; i) {matrix[x][y] val;if (y cols) {y 0;x;}}}return matrix[r][c];}public static void main(String[] args) {Scanner scanner new Scanner(System.in);String data scanner.nextLine();int r scanner.nextInt();int c scanner.nextInt();System.out.println(soln(data, r, c));}
} Cpp
#include iostream
#include vector
#include sstreamusing namespace std;int soln(const string data, int r, int c) {istringstream iss(data);int rows, cols;iss rows cols;vectorvectorint matrix(rows, vectorint(cols, 0));int x 0, y 0;int val, cnt;while (iss val cnt) {for (int i 0; i cnt; i) {matrix[x][y] val;if (y cols) {y 0;x;}}}return matrix[r][c];
}int main() {string data;getline(cin, data);int r, c;cin r c;cout soln(data, r, c) endl;return 0;
}