POJ-3673 cow multiplition

本文介绍了一种特殊的乘法运算——Bessie风格乘法,并提供了实现该算法的具体代码示例。此乘法方式将两个整数的每一位进行配对相乘,再求和得到最终结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Bessie is tired of multiplying pairs of numbers the usual way, so she invented her own style of multiplication. In her style, A*B is equal to the sum of all possible pairwise products between the digits of A and B. For example, the product 123*45 is equal to 1*4 + 1*5 + 2*4 + 2*5 + 3*4 + 3*5 = 54. Given two integers A and B (1 ≤ A, B ≤ 1,000,000,000), determine A*B in Bessie's style of multiplication.

Input

* Line 1: Two space-separated integers: A and B.

Output

* Line 1: A single line that is the A*B in Bessie's style of multiplication.

Sample Input
123 45

Sample Output

54

这道题还是非常简单的,也就是用%和/来处理数据就好,但是在提交的时候出现了一个编译问题,也就是pow函数里面,要用10.0而不是10,是因为重载方面的问题,如果是10的话,那么就不知道该匹配哪个重载函数的参数,这个直接在编译器上没有问题,但是在oj上又出现了编译error,所以证明程序出错,都是程序员的错

#include<iostream>
#include<math.h>
using namespace std;
#define N 15
int main()
{
    long long a,b;
    while(cin>>a>>b)
    {
        int sum = 0;
        int i = 0;
        int value[N];
        int valu2[N];
        while(a>0)
        {
            int hee = i+1;
            long help = pow(10.0,hee);
            long he = pow(10.0,i);
            value[i] =(a%help)/he;
            a -= value[i]*pow(10.0,i);
            i++;
            //cout<<"a"<<" "<<a;
        }
       // cout<<endl;
        int j = 0;
        while(b>0)
        {
            int he = j+1;
            long help2 = pow(10.0,he);
            long hee = pow(10.0,j);
            valu2[j] = (b%help2)/hee;
            b-= valu2[j]*pow(10.0,j);
            j++;
        }
       /* for(int k = 0;k<i;k++)
            cout<<"value a  "<<value[k]<<"  ";
            cout<<endl;
        for(int t = 0;t<j;t++)
            cout<<"value b  "<<valu2[t]<<"  ";
            cout<<endl;*/
        for(int k = 0;k<i;k++)
            for(int t = 0;t<j;t++)
        {
            sum += value[k] * valu2[t];
        }
        cout<<sum<<endl;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值