C++80分求助
查看原帖
C++80分求助
601527
zhuzl009楼主2023/4/7 18:54
#include <iostream>
#include <string>

using namespace std;

typedef unsigned long long ull;
ull a[1005], n = 0;
char table[20] = "0123456789ABCDEF";

string get(ull x) {
	string tmp;
	while (x) {
		tmp = table[x % 16] + tmp;
		x /= 16;
	}
	return tmp;
}

int len(ull x) {
	int len = 0;
	while (x) {
		len ++;
		x /= 10;
	}
	return len;
}

int main() {
	while (1) {
		char op;
		cin >> op;
		if (op == '}')
			break;
		ull t = -1;
		cin >> t;
		if (t == -1) {
			cout << "{}";
			return 0;
		}
		a[++ n] = t;
	}
	cout << "{";
	bool first = true;

	for (ull i = 1; i <= n; i ++) {
		if (first)
			first = false;
		else
			cout << ",";

		string s = get(a[i]);
		int len1 = len(a[i]);
		int len2 = s.length() + 2;
		if (len2 <= len1)
			cout << "0x" << s;
		else
			cout << a[i];
	}
	cout << "}";
	return 0;
}
2023/4/7 18:54
加载中...