怎样计算十六进制的乘法

2024年8月9日 | 分类: 【编程】

【题目】

输入2个十六进制数,进行相乘,输出乘积,以十六进制数表示。

【样例】

输入:
350A
12

输出:
3BAB4

【思路】

十进制中转法:

1. 先将十六进制数转换为十进制数
2. 十进制数进行相乘
3. 将乘积转换为十六进制数

350A = 13578(Dec)
12 = 18(Dec)
13578 × 18 = 244404(Dec) = 3BAB4(Hex)

十六进制直接法:

十六进制的乘除法运算的规则是:逢16进1。

先用2*350A
2*A=20 进一位,余20-16=4
2*0=0 进零位,余0,加上面的进位等于1
2*5=10 进零位,余A
2*3=6 进零位,余6
所以2*350A=6A14

同理1*350A=350A

最后350A*12=6A14+350A0=3BAB4

 350A
   12  *
-------
 6A14
350A
------
3BAB4

中转法代码:


string Hex1, Hex2, strans;
int DecArr[MAX] = { 0 };


void Reverse(char* s ) {//新轮子,翻转字符串
	int n=numberaio(s);
	for (int i = 0, j = n - 1; i < j; i++, j--) {
		char c = s[i];
		s[i] = s[j];
		s[j] = c;
    }
}


long long HexToDec(string aHex)//转化十六进制
{
	long Dec = 0;
	int temp = 0;
	int count = 0;
	int DecCount = 0;
	int strlen= aHex.length();
	int StrLen = strlen;

	while (strlen--)
	{
		if (aHex[count] < '0' || (aHex[count]>'9' && aHex[count]<'A') || (aHex[count]>'F'&&aHex[count]<'a') || aHex[count]>'f')
		{
			cout << "输入的不是十六进制格式" << endl;
			return 0;
		}
		count++;
	}
	
	count = StrLen-1;
	while (StrLen--)
	{
		if (aHex[count] >= '0' && aHex[count] <= '9')
		{
			DecArr[DecCount++] = aHex[count] - '0';
		}
		else if (aHex[count] >= 'A' && aHex[count] <= 'F')
		{
			DecArr[DecCount++] = aHex[count] - 'A' + 10;
		}
		else
		{
			DecArr[DecCount++] = aHex[count] - 'a' + 10;
		}
		count--;
	}

	int j = 0;
	for (int i = 0; i < DecCount; i++)
	{
		if (i < 1)
		{
			Dec = Dec + DecArr[i];
		}
		else
		{
			Dec = (Dec + (DecArr[i] * (16 << j)));
			j += 4;
		}
	}

	return Dec;
}
void decToHex(long long n)
{
    char hexNum[MAX]="",strtemp[MAX]="";
  
    int i = 0;
    while (n != 0) 
    {
        int temp = 0;
        temp = n % 16;
  
        if (temp < 10) 
        {
            hexNum[i] = temp + 48;
            i++;
        }
        else 
        {
            hexNum[i] = temp + 87;
            i++;
        }
  
        n /= 16;
    }
	Reverse(hexNum);
	strans.assign(hexNum);
}
int main()
{
	
	long long Dec1, Dec2, ans;
	cin >> Hex1 >> Hex2;

	Dec1 = HexToDec(Hex1);//16进制转10进制   Conversion:改变
	Dec2 = HexToDec(Hex2);

	ans = Dec1 * Dec2;

	decToHex(ans);

	cout << strans << endl;

	return 0;
}




直接法代码(有bug):


void Reverse(char* s ) {//新轮子,翻转字符串
	int n=numberaio(s);
	for (int i = 0, j = n - 1; i < j; i++, j--) {
		char c = s[i];
		s[i] = s[j];
		s[j] = c;
    }
}


long long HexToDec(string aHex)//转化十六进制
{
	long Dec = 0;
	int temp = 0;
	int count = 0;
	int DecCount = 0;
	int strlen= aHex.length();
	int StrLen = strlen;

	while (strlen--)
	{
		if (aHex[count] < '0' || (aHex[count]>'9' && aHex[count]<'A') || (aHex[count]>'F'&&aHex[count]<'a') || aHex[count]>'f')
		{
			cout << "输入的不是十六进制格式" << endl;
			return 0;
		}
		count++;
	}
	
	count = StrLen-1;
	while (StrLen--)
	{
		if (aHex[count] >= '0' && aHex[count] <= '9')
		{
			DecArr[DecCount++] = aHex[count] - '0';
		}
		else if (aHex[count] >= 'A' && aHex[count] <= 'F')
		{
			DecArr[DecCount++] = aHex[count] - 'A' + 10;
		}
		else
		{
			DecArr[DecCount++] = aHex[count] - 'a' + 10;
		}
		count--;
	}

	int j = 0;
	for (int i = 0; i < DecCount; i++)
	{
		if (i < 1)
		{
			Dec = Dec + DecArr[i];
		}
		else
		{
			Dec = (Dec + (DecArr[i] * (16 << j)));
			j += 4;
		}
	}

	return Dec;
}
void decToHex(long long n)
{
    char hexNum[MAX]="",strtemp[MAX]="";
  
    int i = 0;
    while (n != 0) 
    {
        int temp = 0;
        temp = n % 16;
  
        if (temp < 10) 
        {
            hexNum[i] = temp + 48;
            i++;
        }
        else 
        {
            hexNum[i] = temp + 87;
            i++;
        }
  
        n /= 16;
    }
	Reverse(hexNum);
	strans.assign(hexNum);
}
int main()
{
	int jz = 16;
	string s1, s2;
    int res[MAX];

	cin >> s1 >> s2;

	int i, j, len1, len2;

    len1 = s1.size();
    len2 = s2.size();

    //初始化res为0 
	memset(res, 0, sizeof(res));


    //两数相乘的结果放在res中 
	for (i = 0; i < len1; i++) {
		for (j = 0; j < len2; j++) {
			if (s1[i] >= 97) {
				res[i + j + 1] += (s1[i] - 87) * (s2[j] - 87);
			}
			else {
				res[i + j + 1] += (s1[i] - 30) * (s2[j] - 30);
			}
        }
    }


    //处理进位,从数字低位开始
	for (i = len1 + len2 - 1; i > 0; i--) {
		
		if (res[i] >= jz) {
			res[i - 1] += res[i] / jz;
			res[i] = res[i] % jz;
		}

    }
	i = 0;
	while (res[i] == 0) {
		i++;
	}
	for (; i < len1 + len2; i++) {
		cout << res[i];
	}
	system("pause");
	bukaqingping();
	
	
	return 0;
}