问题 C: 特殊乘法
时间限制: 1 Sec 内存限制: 32 MB
献花: 155 解决: 127
[献花][花圈][TK题库]
题目描述
写个算法,对2个小于1000000000的输入,求结果。特殊乘法举例:123 * 45 = 1*4 +1*5 +2*4 +2*5 +3*4+3*5
输入
两个小于1000000000的数
输出
输入可能有多组数据,对于每一组数据,输出Input中的两个数按照题目要求的方法进行运算后得到的结果。
样例输入
24 65
42 66666
3 67
样例输出
66
180
39
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int maxSize = 128;
int main()
{
#ifdef _DEBUG
ifstream cin("data.txt");
#endif // _DEBUG
char array[maxSize] = {'\0'};
bool fristP = true;
while (cin.getline(array, maxSize))
{
if (array[0] == '\0') { break; }
long long Sum = 0;
int Afrist = 0, Bfrist = 0,Aend;
while (array[Bfrist++] != ' '); Aend = Bfrist - 1;
while (array[Bfrist] == ' ') { ++Bfrist; };//空格折腾得我蛋疼
for (int i = Afrist; i < Aend; ++i)
{
for (int j = Bfrist; array[j] != '\0' && array[j] != ' '; ++j)
{
Sum += (array[i] - '0') * (array[j] - '0');
}
}
if (!fristP) { cout << endl; }
else { fristP = false; }
cout << Sum;
}
#ifdef _DEBUG
cin.close();
system("pause");
#endif // _DEBUG
return 0;
}
代码二:(别人写的)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a,b;
while (cin>>a>>b)
{
int n1=(int)a.length();
int n2=(int)b.length();
long long sum=0;
for(int i=0;i<n1;i++)
{
for(int j=0;j<n2;j++)
{
sum+=(a[i]-'0')*(b[j]-'0');
}
}
cout<<sum<<endl;
}
return 0;
}