diff --git a/GESP/灰阶图像/十进制与十六进制互转.cpp b/GESP/灰阶图像/十进制与十六进制互转.cpp new file mode 100644 index 0000000..d03405d --- /dev/null +++ b/GESP/灰阶图像/十进制与十六进制互转.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; + +//һ16ƵĻҽתΪһ±ֵ +int toDec(char a, char b) { + int res = 0; + if (a >= 'A' && a <= 'F') + res += (a - 'A' + 10) * 16; + else + res += (a - '0') * 16; + + if (b >= 'A' && b <= 'F') + res += b - 'A' + 10; + else + res += b - '0'; + + return res; +} + +//һʮƵĻҽֵ[0,255]תʮ[0,FF] +string toHex(int x) { + int a = x % 16; // 0 ~ 15 + int b = x / 16; // + char c1; + if (a >= 10) + c1 = 'A' + a - 10; + else + c1 = '0' + a; + char c2; + if (b >= 10) + c2 = 'A' + b - 10; + else + c2 = '0' + b; + string res; + res.push_back(c2); + res.push_back(c1); + return res; +} + +int main() { + cout << toDec('A', 'B') << endl; //171ֹ10*16+11=171 + cout << toDec('1', '2') << endl; //18 + cout << toDec('0', '2') << endl; //2 + + cout << toHex(171) << endl; + cout << toHex(18) << endl; + cout << toHex(2) << endl; + + return 0; +} diff --git a/GESP/灰阶图像.cpp b/GESP/灰阶图像/灰阶图像 - 第一个版本.cpp similarity index 100% rename from GESP/灰阶图像.cpp rename to GESP/灰阶图像/灰阶图像 - 第一个版本.cpp diff --git a/GESP/灰阶图像/灰阶图像.cpp b/GESP/灰阶图像/灰阶图像.cpp new file mode 100644 index 0000000..340ad98 --- /dev/null +++ b/GESP/灰阶图像/灰阶图像.cpp @@ -0,0 +1,104 @@ +#include +using namespace std; +const int N = 256; +//Ͱ +int b[N];//0 ~ 255 + +//һ16ƵĻҽתΪһ±ֵ +int toDec(char a, char b) { + int res = 0; + if (a >= 'A' && a <= 'F') + res += (a - 'A' + 10) * 16; + else + res += (a - '0') * 16; + + if (b >= 'A' && b <= 'F') + res += b - 'A' + 10; + else + res += b - '0'; + + return res; +} + +//һʮƵĻҽֵ[0,255]תʮ[0,FF] +string toHex(int x) { + int a = x % 16; // 0 ~ 15 + int b = x / 16; // + char c1; + if (a >= 10) + c1 = 'A' + a - 10; + else + c1 = '0' + a; + char c2; + if (b >= 10) + c2 = 'A' + b - 10; + else + c2 = '0' + b; + string res; + res.push_back(c2); + res.push_back(c1); + return res; +} + + +//ԭʼַ +vector q; + +//ṹ壬ͰеĻҽװõ +struct Node { + int id;//± + int cnt;// + const bool operator<(const Node &b) const { + if (cnt == b.cnt) + return id < b.id; + return cnt > b.cnt; + } +}; +vector vec;//ҪbucketװNodeŵpУʹýṹ + +int main() { + int n; + cin >> n;//nͼ + for (int i = 1; i <= n; i++) { + string s; + cin >> s; + q.push_back(s);//ԭʼ + + for (int j = 0; j < s.size(); j += 2) { + char c1 = s[j], c2 = s[j + 1]; + int k = toDec(c1, c2); //ҽ׶Ӧλ + b[k]++; + } + } + //ҳǰ16λʹƵʸߵĻҽ + //򣬰ʲôأ + //1ǰ + //2һ?ͰСǰ + for (int i = 0; i <= 255; i++) { + if (b[i]) //0IJ + vec.push_back({i, b[i]}); + } + //ʹԶ򷽷 + sort(vec.begin(), vec.end()); + + + for (int i = 0; i < n; i++) { //öÿԭʼַÿ2һ飬жСӦеһӽ + for (int j = 0; j < q[0].size(); j += 2) { + char c1 = q[i][j], c2 = q[i][j + 1]; + //abװĶ̴vecеǰ16ĸ + int x = toDec(c1, c2); + int mi = INT_MAX; + int mi_p = 0; + for (int k = 0; k < min(16, vec.size()); k++) { //16зյģΪû16ô + if (mi > abs(vec[k] - x)) { + mi = abs(vec[k] - x); + mi_p = k; + } + } + //mi_p [0,255] -> ʮ + //ǰa,bֵ޸Ϊmi_pҽֵ + cout << toHex(mi_p); + } + } + return 0; +}