special multiplication. .

Title description:

Write an algorithm to find the result of special multiplication for two inputs less than 1000000000.

Example of special multiplication: 123*45=1*4+1*5+2*4+2*5+3*4+3*5.

enter:

Two numbers less than 1000000000.

output:

The input may have multiple sets of data. For each set of data, the two numbers in Input are calculated according to the method required by the question, and the result is output.

Sample input:

123 45

Sample output:

54

code:

#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

int main() {
	string str1, str2;
	while (cin >> str1 >> str2) {
		int answer = 0;
		for (int i = 0; i < str1.size(); i++) {
			for (int j = 0; j < str2.size(); j++) {
				answer += (str1[i] - '0') * (str2[j] - '0');
			}
		}
		printf("%d\n", answer);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/dlz_yhn/article/details/126392243