b422. Colorful Life and Monochromatic Life

contents

  1. 1. Problem
  2. 2. Sample Input
  3. 3. Sample Output
  4. 4. Solution

Problem

將一張 rgb 表示的彩色影像變成灰階影像。

套用公式 round((r + g + b)/3) 得到新的像素色彩。

Sample Input

1
2
3
3 2
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18

Sample Output

1
2
3
3 2
2 2 2 5 5 5 8 8 8
11 11 11 14 14 14 17 17 17

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
while (scanf("%d %d", &n, &m) == 2) {
printf("%d %d\n", n, m);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int r, g, b, w;
scanf("%d %d %d", &r, &g, &b);
w = (int) round((r + g + b)/3.0);
printf("%d %d %d%c", w, w, w, j == n-1 ? '\n' : ' ');
}
}
}
return 0;
}