首页 > 其他分享 >B. Sum of Two Numbers - 贪心+思维+构造

B. Sum of Two Numbers - 贪心+思维+构造

时间:2023-04-26 18:47:53浏览次数:33  
标签:typedef int Sum Two long Numbers ll

题意:

  给定一个整数n,输出x,y满足以下要求:

  1. x+y=n

  2. x的每一位上的数加在一起的数位和和y的数位和相差不超过1.

分析:

  从高位开始依次遍历,将其平均分给x和y,奇数剩余的1由x和y轮流加上。

代码:

#include <bits/stdc++.h>

#define endl '\n'

using namespace std;

typedef pair<int,int> pii;
typedef long long ll;

void solve()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        ll x=0;
        ll y=0;
        int flag=0;
        for(int i=0;i<s.size();i++)
        {
            int res=s[i]-'0';
            if(res%2==0)
            {
                x+=res/2;
                y+=res/2;
            }
            else
            {
                int tt=res/2;
                x+=tt;
                y+=tt;
                if(flag==0)
                {
                    x++;
                    flag=1;
                }
                else
                {
                    y++;
                    flag=0;
                }
            }
            if(i!=s.size()-1)
            {
                x*=10;
                y*=10;
            }
        }
        cout<<x<<' '<<y<<'\n';
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    solve();
}

 

标签:typedef,int,Sum,Two,long,Numbers,ll
From: https://www.cnblogs.com/yaowww/p/17356955.html

相关文章

  • 勇士召唤Summon Quest一款mac冒险游戏 中文版
    SummonQuest是一款卡牌收集类的手机游戏,玩家需要在游戏中收集各种强力的卡牌,并组建自己的队伍来进行战斗。游戏采用了即时战斗的方式,玩家需要根据卡牌的属性和技能来制定最佳策略,战胜对手。游戏特色:卡牌收集:SummonQuest拥有数百张不同类型的卡牌,每个卡牌都有独特的属性和技能,玩家......
  • Cycle GAN:Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial
    paper:https://arxiv.org/pdf/1703.10593.pdf[2017]code参考:https://github.com/junyanz/pytorch-CycleGAN-and-pix2pixhttps://zhuanlan.zhihu.com/p/79221194https://blog.csdn.net/fangjin_kl/article/details/128117396https://www.bilibili.com/video/BV1kb4y197P......
  • sumOfNegatives
    Countofpositives/sumofnegativesGivenanarrayofintegers.Returnanarray,wherethefirstelementisthecountofpositivesnumbersandthesecondelementissumofnegativenumbers.0isneitherpositivenornegative.Iftheinputisanemptyar......
  • Spatiotemporal Remote Sensing Image Fusion Using Multiscale Two-Stream Convoluti
    SpatiotemporalRemoteSensingImageFusionUsingMultiscaleTwo-StreamConvolutionalNeuralNetworksabstract地表反射率图像的渐变和突变是现有STF方法的主要挑战。(Gradualandabruptchangesinlandsurfacereflectanceimagesarethemainchallengesinexisting......
  • ubuntu - k3s安装失败出现unable to find suitable network address.error
    系统时ubuntu18原因:离线安装k3s需要手动设定ip的网关使用iproute 查看是否出现default via  xxxx.xxxx.x.xdev ens33xxxxx内容。 如果没有出现说明没有默认网关。 方式1:临时生效使用命令:iprouteadddefaultvia192.168.1.1devens33  方式2......
  • codeforces 225B B. Well-known Numbers(数论+二分+贪心+构造)
    题目链接:codeforces225B题目大意:定义f(k,n)为类似菲波那契数推导,只不过变为前k项的和,然后给出一个数s,利用k-菲波那契数构造出一个不重复的集合的元素和为s,集合的规模大于1题目分析:首先因为菲波那契数的增长速度快的吓人,所以给的数据范围109很快就能达到,我们得到O(n)的构造出所有的......
  • Vulnhub之 BoredHackerBlog: Social Network 2.0靶机详细测试过程
    Socnet作者:jasonhuawen靶机信息名称:BoredHackerBlog:SocialNetwork2.0地址:https://www.vulnhub.com/entry/boredhackerblog-social-network-20,455/识别目标主机IP地址(kali㉿kali)-[~/Desktop/Vulnhub/Socnet]└─$sudonetdiscover-ieth1-r192.168.56.0/24Cu......
  • kubenetes pod networ flannel network
     kubectlapply-fhttps://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.ymlhttps://github.com/flannel-io/flannel/blob/master/Documentation/kube-flannel.ymlkubeadminit\--kubernetes-version=v1.23.5 \--image-repositoryre......
  • Unlock the Power of High-Performance Networking with the IPQ9554
    UnlockthePowerofHigh-PerformanceNetworkingwiththeIPQ9554Intoday'sworld,reliableandhigh-speedinternetconnectivityisessentialforeverythingfromonlinegamingandstreamingtoremoteworkandlearning.Whetheryou'reaconsumer......
  • Codeforces Round 689 (Div. 2, based on Zed Code Competition)D.Divide and Summari
    题意:我们给定包含n个正整数的数组,我们可以对这个数组执行一些操作之后,可以让数组内元素的和成为我们想要的数。我们对数组的执行操作一共分为三个步骤,第一个步骤是我们首先计算出数组的中间值mid。这里mid的定义不是中位数也不是均值,而是最大值和最小值的均值。也就是mid=(min......