任意进制之间的转换(C++实现)

任意进制之间的转换(C++实现)

题目描述
输入格式
第一行输入两个整数 n 和 m (2<=n,m<=16),n 代表的是第二行输入的数的进制,m 代表的是输出的数字的进制。
第二行输入一个x(如果有字母,输入大写字母)。
输出格式:
输出一个 m 进制的数。
样例输入:
10 16
30
样例输出:
1E
接下来是C++实现的此程序:

#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;

int main()
{
	int n, m, count = 0, len, i, t = 0, k;
	char a[1000], b[1000];//因为输入的数可能包含字母,所以我们要开char类型的数组来存储
	cin >> n >> m;
	cin >> a;
	len = strlen(a);//获得输入的数的位数
	k = len - 1;
	for (i = 0; i < len; i++)
	{
		if (a[i] >= '0' && a[i] <= '9')
		{
			t = a[i] - '0';
		}
		else
		{
			t = a[i] - 'A' + 10;
		}
		count += t * pow(n, k);//先转为10进制的数,用count变量来存储
		k--;
	}
	len = 0;
	while (count > 0)//循环实现10进制向m进制的转换
	{
		t = count % m;
		count /= m;
		if (t >= 0 && t <= 9)
		{
			b[len] = '0' + t;
		}
		else
		{
			b[len] = 'A' + (t - 10);
		}
		len++;
	}
	for (i = len - 1; i >= 0; i--)//倒叙输出m进制的字符串
	{
		cout << b[i];
	}
	return 0;
}
  • 13
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个基于双向循环链实现长整求和的示例程序。程序的实现思路如下: 1. 定义一个双向循环链结构体,每个节点存储4位十进制,头结点的符号位示长整的符号,链长度存储在头结点的绝对值中。 2. 读入两个长整,并将它们存储到两个双向循环链中。 3. 将两个从低位到高位逐位相加,将结果存储到一个新的链中。 4. 对于新链中每个节点的值,如果大于等于10000,则需要向高位进位,并将当前节点的值减去10000;如果小于0,则需要向高位借位,并将当前节点的值加上10000。 5. 最后输出新链示的长整。 ```C++ #include <iostream> #include <string> using namespace std; const int BASE = 10000; // 每个节点存储的值的最大值 const int DIGITS = 4; // 每个节点存储的值的位 // 定义双向循环链节点结构体 struct ListNode { int val; // 当前节点存储的值 ListNode* prev; // 前驱指针 ListNode* next; // 后继指针 ListNode(int x = 0) : val(x), prev(nullptr), next(nullptr) {} }; // 定义双向循环链结构体 struct LinkedList { ListNode* head; // 头结点 LinkedList() : head(new ListNode()) { head->prev = head->next = head; } ~LinkedList() { // 销毁链 ListNode* cur = head->next; while (cur != head) { ListNode* next = cur->next; delete cur; cur = next; } delete head; } void insert(int x) { // 在链末尾插入一个节点 ListNode* node = new ListNode(x); node->prev = head->prev; node->next = head; head->prev->next = node; head->prev = node; ++(head->val); // 链长度加1 } void print() { // 输出示的长整 if (head->val == 0) { // 长整为0 cout << "0" << endl; return; } if (head->val < 0) { // 长整为负 cout << "-"; } ListNode* cur = head->prev; while (cur != head) { cout.width(DIGITS); cout.fill('0'); cout << abs(cur->val); cur = cur->prev; if (cur != head) { cout << ","; } } cout << endl; } }; // 将一个字符串转换为长整的双向循环链示 LinkedList stringToList(const string& s) { LinkedList list; int sign = 1; int start = 0; if (s[0] == '-') { // 字符串示的长整为负 sign = -1; start = 1; } int num = 0; int len = 0; for (int i = s.length() - 1; i >= start; --i) { num += (s[i] - '0') * pow(10, len++); if (len == DIGITS || i == start) { // 每4位一组,转换为一个节点存储 list.insert(sign * num); num = 0; len = 0; } } return list; } // 两个双向循环链相加 LinkedList add(const LinkedList& a, const LinkedList& b) { LinkedList sum; ListNode* curA = a.head->next; ListNode* curB = b.head->next; int carry = 0; // 进位 while (curA != a.head || curB != b.head) { int valA = curA != a.head ? curA->val : 0; int valB = curB != b.head ? curB->val : 0; int val = valA + valB + carry; sum.insert(val % BASE); carry = val / BASE; if (curA != a.head) { curA = curA->next; } if (curB != b.head) { curB = curB->next; } } if (carry != 0) { // 如果最高位有进位 sum.insert(carry); } return sum; } // 对链示的长整求相反 LinkedList negate(const LinkedList& list) { LinkedList neg; neg.head->val = -list.head->val; ListNode* cur = list.head->next; while (cur != list.head) { neg.insert(-cur->val); cur = cur->next; } return neg; } // 比较两个示的长整的大小(绝对值) int compare(const LinkedList& a, const LinkedList& b) { if (a.head->val != b.head->val) { return a.head->val > b.head->val ? 1 : -1; } ListNode* curA = a.head->prev; ListNode* curB = b.head->prev; while (curA != a.head && curB != b.head) { if (curA->val != curB->val) { return curA->val > curB->val ? 1 : -1; } curA = curA->prev; curB = curB->prev; } return 0; } // 计算两个示的长整的差(假设a>=b) LinkedList subtract(const LinkedList& a, const LinkedList& b) { LinkedList diff; ListNode* curA = a.head->next; ListNode* curB = b.head->next; int borrow = 0; // 借位 while (curA != a.head || curB != b.head) { int valA = curA != a.head ? curA->val : 0; int valB = curB != b.head ? curB->val : 0; int val = valA - valB - borrow; if (val < 0) { borrow = 1; val += BASE; } else { borrow = 0; } diff.insert(val); if (curA != a.head) { curA = curA->next; } if (curB != b.head) { curB = curB->next; } } while (diff.head->prev != diff.head && diff.head->prev->val == 0) { // 去掉高位的0 ListNode* node = diff.head->prev; node->prev->next = diff.head; diff.head->prev = node->prev; delete node; --(diff.head->val); } return diff; } int main() { string s1, s2; cin >> s1 >> s2; LinkedList a = stringToList(s1); LinkedList b = stringToList(s2); LinkedList sum; if (a.head->val >= 0 && b.head->val >= 0) { // 两个长整都为非负 sum = add(a, b); } else if (a.head->val < 0 && b.head->val < 0) { // 两个长整都为负 sum = add(negate(a), negate(b)); sum.head->val = -sum.head->val; } else { // 两个长整符号不同 if (compare(a, b) >= 0) { // |a| >= |b| sum = subtract(a, b); } else { sum = subtract(b, a); sum.head->val = -sum.head->val; } } sum.print(); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小张不胖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值