1488.Problem_F 八进制模拟

Description

There are two octal integers numbers a and b.You need to calculate the result of a minus b,then output in octal notation.

If the result is negative, you should use the negative sign instead of complement notation.

Input

The first line is an integer T(1 <= T <= 1000), indicating the number of test cases.

For each case, the first and only line contains two octal integers a, b. (0 <= a, b < 2^100).

Output

Print the result contains a list of the fully qualified octal digits,one per line.

Sample Input

176 7

Sample Output

67

Hint

        就是给你两个八进制数~让你进行减法,并以八进制的形式输出。思路不难~~但一定要小心各个地方的坑;

        顺便提供一组数据( 1   2000  1);

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int m[1005];
#define mm 1000
int main()
{
	int te;
	scanf("%d", &te);
	string j, k;
	char q[1005], p[1005];
	int spot;
	while (te--)
	{
		spot = 1;
		memset(m, 0, sizeof(m));
		memset(q, 0, sizeof(q));
		memset(p, 0, sizeof(p));
		cin >> j >> k;
		int len1 = j.length();
		int len2 = k.length();
		int cnt = len1 - 1;
		for (int s = mm; s > mm - len1; s--)
		{
			q[s] = j[cnt--];
		}
		cnt = len2 - 1;
		for (int s = mm; s > mm - len2; s--)
		{
			p[s] = k[cnt--];
		}
		if (len1 > len2)
		{
			for (int s = mm; s > mm - len2; s--)
			{
				int w = q[s] - p[s];
				if (w < 0)
				{
					w += 8;
					q[s - 1] = q[s - 1] - 1;
				}
				m[s] = w;
			}
			for (int s = mm - len2; s > mm - len1; s--)
			{
				int t = q[s] - '0';
				if (t < 0)
				{
					t += 8;
					q[s - 1]--;
				}
				m[s] = t;
			}
		}
		else if (len1 == len2)
		{
			int sp = 1;
			for (int s = mm - len1 + 1; s <= mm; s++)
			{
				if (q[s] > p[s])
				{
					break;
				}
				if (q[s] < p[s])
				{
					sp = 0;
					break;
				}
			}
			if (sp)
			{
				for (int s = mm; s > mm - len2; s--)
				{
					int w = q[s] - p[s];
					if (w < 0)
					{
						w += 8;
						q[s - 1] = q[s - 1] - 1;
					}
					m[s] = w;
				}
			}
			else
			{
				//	cout << "asdasd" << endl;
				spot = 0;
				for (int s = mm; s > mm - len2; s--)
				{
					int w = p[s] - q[s];
					if (w < 0)
					{
						w += 8;
						p[s - 1]--;
					}
					m[s] = w;
				}
			}
		}
		else
		{
			spot = 0;
			for (int s = mm; s > mm - len1; s--)
			{
				int w = p[s] - q[s];
				if (w < 0)
				{
					w += 8;
					p[s - 1]--;
				}
				m[s] = w;
			}
			for (int s = mm - len1; s > mm - len2; s--)
			{
				int t = p[s] - '0';
				if (t < 0)
				{
					t += 8;
					p[s - 1]--;
				}
				m[s] = t;
			}
		}
		if (!spot)
		{
			cout << "-";
		}
		int sp = 0;
		for (int s = 0; s < mm; s++)
		{
			if (m[s] != 0)
			{
				sp = 1;
			}
			if (sp)
				cout << m[s];
		}
		cout << m[mm] << endl;
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值