清华大学考研复试机试:特殊乘法

版权声明:本文为博主原创,未经博主允许不得转载。转载请附上原文链接。 https://blog.csdn.net/qq_38341682/article/details/88583856

题目描述

写个算法,对2个小于1000000000的输入,求结果。 特殊乘法举例:123 * 45 = 14 +15 +24 +25 +34+35

输入描述

两个小于1000000000的数

输出描述

输入可能有多组数据,对于每一组数据,输出Input中的两个数按照题目要求的方法进行运算后得到的结果。

示例:

输入

123 45

输出

54

分析

水题,直接循环计算即可。不过要注意使用string来存储输入,并且注意字符和数字之间的转换。

AC代码如下:

#include<iostream>
#include<string>

using namespace std;

int main(void)
{
	string num1, num2;
	while(cin >> num1 >> num2)
	{
		int result = 0;
		int len1 = num1.length();
		int len2 = num2.length();
		int temp1, temp2;
		for(int i = 0; i < len1; ++i)
		{
			for(int j = 0; j < len2; ++j)
			{
				temp1 = num1[i] - '0';//字符转数字
				temp2 = num2[j] - '0';
				result += temp1 * temp2;
			}
		}
		cout << result;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38341682/article/details/88583856