#202212-3 JPEG 解码题目问题
目前已经可以拿到40分,在离散余弦变化时按照题目条件进行计算,和网上满分代码对比也大致相同,但输出就是有问题(这里
我只输出了经过变化后没有+128和取整计算的部分)
代码如下
#include<bits/stdc++.h>
using namespace std;
int q[8][8],m[8][8];
int data[64];
double pi8 = acos(-1)/8.0,m2[8][8];
void read_scan_data_1(int n)
{
//填充图像矩阵方法一:总共只有64个元素,可以直接将走位表直接枚举出来
int idx[8][8] = {{0, 1, 5, 6, 14, 15, 27, 28},
{2, 4, 7, 13, 16, 26, 29, 42},
{3, 8, 12, 17, 25, 30, 41, 43},
{9, 11, 18, 24, 31, 40, 44, 53},
{10, 19, 23, 32, 39, 45, 52, 54},
{20, 22, 33, 38, 46, 51, 55, 60},
{21, 34, 37, 47, 50, 56, 59, 61},
{35, 36, 48, 49, 57, 58, 62, 63}};
for(int i = 0;i < 8;i ++){
for(int j = 0;j < 8;j ++){
m[i][j] = data[idx[i][j]];
}
}
}
void read_scan_data_2(int n)
{
//填充矩阵方法二:找规律版
int i = 0,j = 0,k = 1; //(i,j)为当前方位,k指示前进方向为右上方还是左下方
for(int x = 0;x < n;x ++){
m[i][j] = data[x];
if(k == 1){
//如果向右上方推进,斜对角(左下右上)线两侧的例外分别是i=0和j=7
if(i == 0) j++, k = -1; //注意更改方向
else if(j == 7) i ++,k = -1;
else i --,j ++;
}
else{
//如果向左下方推进,斜对角线两侧的例外分别是i=7和j=0
if(i == 7) j ++, k = 1;
else if(j == 0) i ++, k = 1;
else i ++,j --;
}
}
}
double alpha(int u)
{
if(u == 0) return sqrt(0.5);
else return 1;
}
int cast(int x)
{
if(x < 0) return 0;
if(x > 255) return 255;
return x;
}
int main()
{
for(int i = 0;i < 8;i ++){
for(int j = 0;j < 8;j ++){
cin>>q[i][j];
}
}
int n; cin>>n;
int T; cin>>T;
for(int i = 0;i < n;i ++) cin>>data[i];
//读入填充数据
read_scan_data_2(n);
//量化
for(int i = 0;i < 8;i ++){
for (int j = 0;j < 8;j ++){
m2[i][j] = m[i][j] * q[i][j];
}
}
if(T == 0){
for(int i = 0;i < 8;i ++){
for(int j = 0;j < 8;j ++)
cout<<m[i][j]<<" ";
cout<<endl;
}
}
else if(T == 1){
//输出量化后的矩阵
for(int i = 0;i < 8;i ++){
for (int j = 0;j < 8;j ++){
cout<<m2[i][j]<<" ";
}
cout<<endl;
}
}
else{
for(int i = 0;i < 8;i ++){
for (int j = 0;j < 8;j ++){
double cnt = 0;
for(int u = 0;u < 8;u ++){
for(int v = 0;v < 8;v ++){
double coefficient = alpha(u) * alpha(v);
cnt += coefficient * m2[u][v] * cos(pi8 *(i + 0.5)*u)*cos(pi8 *(j + 0.5)*v);
}
}
//m2[i][j] = cast(round(cnt/4.0 + 128));
m2[i][j] = cnt/4.0;
cout<<m2[i][j]<<" ";
}
cout<<endl;
}
}
return 0;
}
输出结果如下:
官网结果如下: