diff --git a/GESP/灰阶图像/十六进制转换测试.cpp b/GESP/灰阶图像/十六进制转换测试.cpp new file mode 100644 index 0000000..663db1d --- /dev/null +++ b/GESP/灰阶图像/十六进制转换测试.cpp @@ -0,0 +1,48 @@ +include +using namespace std; + +//һ16ƵĻҽתΪһ±ֵ +int toDec(char a, char b) { + int res = 0; + if (a >= '0' && a <= '9') + res += (a - '0') * 16; + else if (a >= 'A' && a <= 'F') + res += (a - 'A' + 10) * 16; + + if (b >= '0' && b <= '9') + res += b - '0'; + else if (b >= 'A' && b <= 'F') + res += b - 'A' + 10; + return res; +} + +//һʮƵĻҽֵ[0,255]תʮ[0,FF] +string toHex(int x) { + char c1, c2; + int a = x % 16; // 0 ~ 15 + int b = x / 16; // + if (a >= 10) + c1 = 'A' + a - 10; + else + c1 = '0' + a; + 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;// AB-->? 10*16+ 11 =171 + cout << toDec('1', '3') << endl;// 19 + cout << toDec('1', 'A') << endl;// 26 + + cout << toHex(171) << endl; //AB + cout << toHex(19) << endl; //13 + cout << toHex(26) << endl; //1A + return 0; +} diff --git a/GESP/灰阶图像/灰阶图像.cpp b/GESP/灰阶图像/灰阶图像.cpp index d97ca24..6975860 100644 --- a/GESP/灰阶图像/灰阶图像.cpp +++ b/GESP/灰阶图像/灰阶图像.cpp @@ -112,3 +112,10 @@ int main() { } return 0; } + + + + + + + diff --git a/GESP/灰阶图像/糖豆版本.cpp b/GESP/灰阶图像/糖豆版本.cpp new file mode 100644 index 0000000..680103a --- /dev/null +++ b/GESP/灰阶图像/糖豆版本.cpp @@ -0,0 +1,102 @@ +#include +using namespace std; + + +//һ16ƵĻҽתΪһ±ֵ +int toDec(char a, char b) { + int res = 0; + if (a >= '0' && a <= '9') + res += (a - '0') * 16; + else if (a >= 'A' && a <= 'F') + res += (a - 'A' + 10) * 16; + + if (b >= '0' && b <= '9') + res += b - '0'; + else if (b >= 'A' && b <= 'F') + res += b - 'A' + 10; + return res; +} + +//һʮƵĻҽֵ[0,255]תʮ[0,FF] +string toHex(int x) { + char c1, c2; + int a = x % 16; // 0 ~ 15 + int b = x / 16; // + if (a >= 10) + c1 = 'A' + a - 10; + else + c1 = '0' + a; + if (b >= 10) + c2 = 'A' + b - 10; + else + c2 = '0' + b; + + string res; + res.push_back(c2); + res.push_back(c1); + return res; +} + +const int N = 256; +//Ͱ +int bucket[N];//0 ~ 255 + +//ԭʼַ +string q[30]; + +//ṹ壬ͰеĻҽװõ +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() { + freopen("D://HuiJie.txt", "r", stdin); + + int n; + cin >> n;//nͼ + for (int i = 1; i <= n; i++) { + string s; + cin >> s; + q[i] = s;//ԭʼַ + for (int j = 0; j < s.size(); j += 2) { + char a = s[j], b = s[j + 1]; + int k = toDec(a, b); //ҽ׶Ӧλ + bucket[k]++; + } + } + + for (int i = 0; i <= 255; i++) + vec.push_back({i, bucket[i]}); + sort(vec.begin(), vec.end()); + + //ǰʮλַӡ + for (int i = 0; i < min(16, (int)vec.size()); i++) + cout << toHex(vec[i].id) ; + cout << endl; + + //ڶʵĻش + for (int i = 1; i <= n; i++) { //öÿԭʼַÿ2һ飬жСӦеһӽ + for (int j = 0; j < q[1].size(); j += 2) { + char a = q[i][j], b = q[i][j + 1]; //0 F ,0 F + int x = toDec(a, b); + int mi = INT_MAX; + int mi_p = 0; + for (int k = 0; k < min(16, (int)vec.size()); k++) { //16зյģΪû16ô + if (mi > abs(vec[k].id - x)) { + mi = abs(vec[k].id - x); + mi_p = k; + } + } + cout << toHex(mi_p)[1]; + } + cout << endl; + } + return 0; +}