JPEG 解码
一道特别幸福简单的第三题…都不敢想象在考场上碰见这种题目会有多幸福。直接按照题目意思做就好了,感觉比第二题还简单…而且数组特别小完全没有超时压力
补充一个对小数处理的部分,包括本题涉及的四舍五入取整以及输出取整:C++ 实现四舍五入的几种方法
直接上满分代码
#include<bits/stdc++.h>
using namespace std;
const int N = 10;
const double pi = acos(-1);
int Q[N][N], M[N][N];
double M1[N][N];
vector<int> Data;//填充函数
void filling()
{int dir = 1; //1是右上 0是左下int i = 0, j = 0;for(int k = 0;k < Data.size(); k ++){M[i][j] = Data[k];if(dir == 1){ //如果当前方向是右上方if(j == 7) {i ++; dir = 0;} //触碰到右边界else if(i == 0) {j ++; dir = 0;} //触碰到上边界else {i --; j ++; }}else{ //向左下方运动if(i == 7) {j ++; dir = 1;} //触碰到下边界else if(j == 0) {i ++; dir = 1;} //触碰到左边界else {i ++; j --;}}}
}
//逐项相乘
void multiply()
{for(int i = 0;i < 8;i ++){for(int j = 0;j < 8;j ++){M[i][j] *= Q[i][j];}}
}
double alpha(int x)
{return x == 0 ? sqrt(0.5) : 1;
}
void change()
{for(int i = 0;i < 8;i ++){for(int j = 0;j < 8;j ++){double x = 0;double mul1 = (pi / 8) * (i + 0.5); //对于每个i,j mul1和mul2是确定的,在循环外就算了double mul2 = (pi / 8) * (j + 0.5);for(int u = 0;u < 8;u ++){for(int v = 0;v < 8;v ++){x += alpha(u)*alpha(v)*M[u][v]*cos(mul1*u)*cos(mul2*v);}}x /= double(4);x = round(x + 128);x = x > 255 ? 255 : x;x = x < 0 ? 0 : x;M1[i][j] = x;}}
}
int main()
{int n, T;ios::sync_with_stdio(false);cin.tie(0);for(int i = 0;i < 8;i ++)for(int j = 0;j < 8;j ++)cin >> Q[i][j];cin >> n >> T;for(int i = 0;i < n; i ++){int x; cin >> x;Data.push_back(x);}if(T == 0) {filling();for(int i = 0;i < 8;i ++){for(int j = 0;j < 8;j ++){cout << M[i][j] << ' ';}cout << "\n";}}else if(T == 1){filling();multiply();for(int i = 0;i < 8;i ++){for(int j = 0;j < 8;j ++){cout << M[i][j] << ' ';}cout << "\n";}}else {filling();multiply();change();for(int i = 0;i < 8;i ++){for(int j = 0;j < 8;j ++){cout << M1[i][j] << ' ';}cout << "\n";}}return 0;
}