ZUFE OJ 2289 God Wang II

Description

这个世界太无聊了,于是God Wang想出了新的运算符号$,对于两个数x,y来说x$y的值等于x和y各个位置上的数字乘积之和,没有的位按0来算

比如说123$321=1*3+2*2+3*1=10,105$51=1*0+0*5+5*1=5。于是God Wang又有了新的问题,

他定义了函数F(L,R)=(((((L$(L+1))$(L+2))$(L+3)....)$R),他想要知道F(L,R)的值,现在请你来告诉他吧。

Input

输入第一行为一个正整数T(T<=1000)

接下来T行,每行两个整数L,R (0<=L<R<=2^31-1)

Output

输出T行,每行一个整数表示输出的答案

Sample Input

3
8 10
50 51
51 64

Sample Output

7
25
38

 

暴力跑,遇到是0了就退出循环,因为之后一定都是0。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
 
int L,R;
int X[100];
int Y[100];
int xx,yy;
 
int F(int x,int y)
{
    memset(X,0,sizeof(X));
    memset(Y,0,sizeof(Y));
    xx=0;
    yy=0;
    int ans=0;
    while(x)
    {
        X[xx]=x%10;
        x=x/10;
        xx++;
    }
    while(y)
    {
        Y[yy]=y%10;
        y=y/10;
        yy++;
    }
    for(int i=0; i<max(xx,yy); i++)
        ans=ans+X[i]*Y[i];
    return ans;
}
 
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&L,&R);
        int  A=L;
        for(int i=L+1; i<=R; i++)
        {
            A=F(A,i);
            if(A==0) break;
        }
        printf("%d
",A);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zufezzt/p/4732722.html