沐里纷纷


私信TA

用户名:Epoch

访问量:62991

签 名:

我不会算法

等  级
排  名 37
经  验 12836
参赛次数 1
文章发表 172
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

不会算法

解题思路:

注意事项:

000 = 0

参考代码:

#include <iostream>
#include <string>

using namespace std;

int pow(int a, int b)
{
	int ans = 1;
	while (b)
	{
		if (b & 1) ans *= a;
		a *= a;
		b >>= 1;
	}
	return ans;
}

void toOther(int n, int type, string& s)
{
	if (n)
	{
		toOther(n/type, type, s);
		s += n%type > 9 ? n%type - 10 + 'A' : n%type + '0';
	}
}

int toDec(string s, int type)
{
	int len = s.length();
	int ans = 0;
	for (int i = 0; i < len; i++)
	{
		if (s[i] >= '0' && s[i] <= '9')
			ans += (s[i] - '0')*pow(type, len - i - 1);
		else if (s[i] >= 'A' && s[i] <= 'Z')
			ans += (s[i] - 'A' + 10)*pow(type, len - i - 1);
	}
	return ans;
}

int main()
{
	string s;
	cin >> s;
	
	if (s == "000")
		s = "0";
	
	int dec = toDec(s, 16);
	string oct;
	if (dec)
		toOther(dec, 8, oct);
	else
		oct = "0";
	cout << s << " " << dec << " " << oct << endl;
	return 0;
}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区