首页 > 其他分享 >B. Sum of Two Numbers

B. Sum of Two Numbers

时间:2023-02-11 11:13:48浏览次数:41  
标签:digits 10 dlen int Sum Two num Numbers sum

B. Sum of Two Numbers

https://codeforces.com/problemset/problem/1788/B

 

思路

计算原数的所有位置上的digit和

得到digit和的一半

 

对于原数从左到右切分,前半部分digit和为half

例如:

1331

-->>

1300

0031

Code

https://codeforces.com/problemset/submission/1788/193134592

int t;
vector<int> digits;
vector<int> bdigits;
vector<int> sdigits;
 
void storedigits(int num){
    while(num){
        int r = num%10;
        
        digits.push_back(r);
 
        num /= 10;
    }
 
    reverse(digits.begin(), digits.end());
}
 
int getds(int num){
    int sum = 0;
 
    while(num){
        int r = num%10;
        sum += r;
 
        num /= 10;
    }
 
    return sum;
}
 
void split(int half){
    int dlen = digits.size();
    int sum = 0;
    REP(i, dlen){
        int one = digits[i];
 
        if (sum < half){
            if(sum + one <= half){
                bdigits.push_back(one);
                sdigits.push_back(0);
            } else {
                int bpart = half - sum;
                int spart = one - bpart;
 
                bdigits.push_back(bpart);
                sdigits.push_back(spart);
            }
        } else{
            bdigits.push_back(0);
            sdigits.push_back(one);
        }
        
        sum += one;
    }
}
 
int tonum(vector<int>& digits){
    int dlen = digits.size();
    
    int pow = 1;
    int sum = 0;
    REP(i, dlen){
        int one = digits[dlen-i-1];
        
        sum += one*pow;
        
        pow *= 10;
    }
    
    return sum;
}
 
int printdigits(vector<int>& digits){
    int dlen = digits.size();
 
    REP(i, dlen){
        int one = digits[i];
        
        cout << one << " ";
    }
    
    cout << endl;
}
 
int main()
{
    cin >> t;
 
    REP(i, t){
        int n;
 
        cin >> n;
 
        digits.clear();
        
        storedigits(n);
//        printdigits(digits);
 
        int ds = getds(n);
        int half = ds/2;
        
        bdigits.clear();
        sdigits.clear();
        
        split(half);
//        printdigits(bdigits);
//        printdigits(sdigits);
        
        int bnum = tonum(bdigits);
        int snum = tonum(sdigits);
        
        cout << bnum << " " << snum << endl;
    }
 
    return 0;
}
 

 

标签:digits,10,dlen,int,Sum,Two,num,Numbers,sum
From: https://www.cnblogs.com/lightsong/p/17111052.html

相关文章

  • A. One and Two
    A.OneandTwohttps://codeforces.com/problemset/problem/1788/A思路统计2的个数,如果是奇数个,不满足如果是偶数个,2队列中,前半部分最后一个2则为目标位置如果个数......
  • E. Sum Over Zero
    E.SumOverZeroYouaregivenanarray$a_1,a_2,\ldots,a_n$of$n$integers.Consider$S$asasetofsegmentssatisfyingthefollowingconditions.Eache......
  • Session-based Recommendation with Graph Neural Networks
    目录概符号说明基本框架gatedGRU技术细节代码WuS.,TangY.,ZhuY.,WangL.,XieX.andTanT.Session-basedrecommendationwithgraphneuralnetworks.InAAA......
  • 合理安排kafka的broker、partition、consumer数量
    broker的数量最好大于等于partition数量一个partition最好对应一个硬盘,这样能最大限度发挥顺序写的优势。一个broker如果对应多个partition,需要随机分发,顺序IO会退化成随......
  • 联邦学习论文阅读笔记03 Incentive Design for Efficient Federated Learning in Mobi
    精翻:https://blog.csdn.net/weixin_43978453/article/details/104947600 挑战:客户端自身存在计算与通信成本,不给足够的好处不愿意参与训练。方法:基于合约理论设计了一......
  • pytest---多重断言(pytest-assume)
    前言在编写自动化测试用例的时候,可能一条用例存在这多条断言,那么在自动化中如何编写多条断言且断言失败后还能继续往下执行?这里引入新的插件pytest-assumepytest-assume......
  • 124. Binary Tree Maximum Path Sum[Hard]
    124.BinaryTreeMaximumPathSumApathinabinarytreeisasequenceofnodeswhereeachpairofadjacentnodesinthesequencehasanedgeconnectingthem.......
  • 什么是Azure Network Watcher
    现如今,越来越多的企业开始在云中部署他们的数据资产。一般情况下,当企业在云中部署某个应用时,基本上都是首先构建一个虚拟网络,然后进行一些基本的安全和连接设置。然后使其通......
  • IOS开发者自带弱网测试工具界面说明NETWORK LINK CONDITIONER
    IOS手机的开发者自带了弱网模拟工具,以下是界面说明,便于大家使用时自行配置使用。测试工具NETWORKLINKCONDITIONER。1、准备环境,设置中调出:开发者选项(如果没有,需要真机联......
  • Codeforces Round #716 (Div. 2), B. AND 0, Sum Big, 快速幂结论题
    problemB.AND0,SumBigtimelimitpertest2secondsmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputBabyBadawy’sfirstwordswe......