特殊乘法 %

题目:求两个小于十亿的数,按位相乘再相加如12*45=1*4+1*5+2*4+2*5

法1:

#include<iostream>
#include<string.h>
using namespace std;
char a[10], b[10];
int main(){
    while (cin >> a >> b)
    {
        int la = strlen(a);
        int lb = strlen(b);
        int res = 0;
        for (int i = 0; i < la; i++)
            for (int j = 0; j < lb; j++)
                res += (a[i] - '0')*(b[j] - '0');
        cout << res << endl;
    }

    return 0;
}

法2:

#include<iostream>
#include<string.h>
using namespace std;
int a[10], b[10];
int main(){
    __int64 x, y;
    while (cin >> x >> y)
    {
        int i = 0,j=0;
        while (x)
        {
            a[i++] = x % 10;
            x = x / 10;
        }
        while (y)
        {
            b[j++] = y % 10;
            y = y / 10;
        }
        int res=0;
        for (int c = 0; c < i; c++)
            for (int d = 0; d < j; d++)
            {
                res += a[c] * b[d];
            }
        cout << res << endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/KingsCC/article/details/81780622
今日推荐