Codeforces #3 A Shortest path of the king

本文介绍了一个CodeForces上的题目3A的解决方案,该题要求找到从一点到另一点的路径,使得路径上各点值相乘后的结果的末尾零的数量最少。通过统计路径中数值包含因子2和5的次数来解决,并提供了一段C++代码实现。

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

题目链接 http://codeforces.com/problemset/problem/3/A
走过的格的值相乘后后缀的0最少,统计路径中中 2和5最少的数就行,有值0的话看另外判断,可能有路径后缀的0没有。
下面ac代码

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef long long lld;
/*方向 R U L D RU RD LU LD
       0 1 2 3 4  5  6  7
*/
int main()
{
    char x1,x2;
    int  y1,y2;
    int dir[8]={0};
    while(cin>>x1>>y1>>x2>>y2)
    {
        memset(dir,0,sizeof(0));
        int xcha=x2-x1;
        int ycha=y2-y1;
        if(xcha==0&&ycha==0)
           cout<<0;
        else if(xcha>=0&&ycha>=0)
        {
            int flag=0;
            while(xcha>0&&ycha>0)
            {
                ycha--;
                xcha--;
                dir[4]++;
                flag++;
            }
            while(xcha>0)
            {
                xcha--;
                dir[0]++;
                flag++;
            }
            while(ycha>0)
            {
                ycha--;
                dir[1]++;
                flag++;
            }
            cout<<flag<<endl;
        }
        else if(xcha<=0&&ycha<=0)
        {
            int flag=0;
            while(xcha<0&&ycha<0)
            {
                ycha++;
                xcha++;
                dir[7]++;
                flag++;
            }
            while(xcha<0)
            {
                xcha++;
                dir[2]++;
                flag++;
            }
            while(ycha<0)
            {
                ycha++;
                dir[3]++;
                flag++;
            }
            cout<<flag<<endl;
        }
        else if(xcha<=0&&ycha>=0)
        {
            int flag=0;
            while(xcha<0&&ycha>0)
            {
                ycha--;
                xcha++;
                dir[6]++;
                flag++;
            }
            while(xcha<0)
            {
                xcha++;
                dir[2]++;
                flag++;
            }
            while(ycha>0)
            {
                ycha--;
                dir[1]++;
                flag++;
            }
            cout<<flag<<endl;
        }
        else if(xcha>=0&&ycha<=0)
        {
            int flag=0;
            while(xcha>0&&ycha<0)
            {
                ycha++;
                xcha--;
                dir[5]++;
                flag++;
            }
            while(xcha>0)
            {
                xcha--;
                dir[0]++;
                flag++;
            }
            while(ycha<0)
            {
                ycha++;
                dir[3]++;
                flag++;
            }
            cout<<flag<<endl;
        }

        while(dir[0]--)
            cout<<"R"<<endl;
        while(dir[1]--)
            cout<<"U"<<endl;
        while(dir[2]--)
            cout<<"L"<<endl;;
        while(dir[3]--)
            cout<<"D"<<endl;;
        while(dir[4]--)
            cout<<"RU"<<endl;;
        while(dir[5]--)
            cout<<"RD"<<endl;;
        while(dir[6]--)
            cout<<"LU"<<endl;;
        while(dir[7]--)
            cout<<"LD"<<endl;;
        cout<<endl;
/*方向 R U L D RU RD LU LD
       0 1 2 3 4  5  6  7
*/

    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值