首页 > 其他分享 >B. Quasi Binary

B. Quasi Binary

时间:2024-03-09 21:36:17浏览次数:26  
标签:Binary false string int stop back Quasi ans

It's a simple problem on codeforces.
we traverse the through the string n, if we encouter a '1', we add a new string to ans and set stop to false.Otherwise, we stop the loop.
Once we find 1, we then append either '1' or '0' to the newly added string in 'ans', based on the next value we traverse.

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

void solve(){
    int n;
    cin >> n;

    string s = std::to_string(n);

    int m = s.size();
    vector<string> ans;
    while (true){
        bool stop = true;
        for (int i = 0; i < m; ++i){
            if (s[i] >= '1' && stop == true){
                ans.push_back("1");
                s[i] --;
                stop = false;
            }
            else if (stop == false){
                if (s[i] >= '1'){
                    ans.back() += "1";
                    s[i] --;
                }
                else{
                    ans.back() += "0";
                }
            }
        }
        if (stop){
            break;
        }
    }

    cout << ans.size() << '\n';
    for (const string& x : ans){
        cout << x << " ";
    }
    cout << '\n';
}

标签:Binary,false,string,int,stop,back,Quasi,ans
From: https://www.cnblogs.com/yxcblogs/p/18063358

相关文章

  • Binary Tree Maximum Path Sum
    SourceGivenabinarytree,findthemaximumpathsum.Thepathmaystartandendatanynodeinthetree.ExampleGiventhebelowbinarytree,1/\23Return6.题解1-递归中仅返回子树路径长度题目很短,要求返回最大路径和。咋看一下......
  • CF1066E Binary Numbers AND Sum 题解
    分析因为\(a\)是一直没有改变的,移动的只有\(b\),所以从\(a\)的每一位的贡献入手。对于\(a\)中的从低到高第\(i\)位,其对应的十进制值是\(a_{n-i+1}\times2^{i-1}\)。注意到\(b\)是每次右移一位的,所以在\(b\)中能与\(a_{n-i+1}\)匹配的都是在下标区间\([1,m-i+1]......
  • [LeetCode] 2864. Maximum Odd Binary Number
    Youaregivenabinarystringsthatcontainsatleastone'1'.Youhavetorearrangethebitsinsuchawaythattheresultingbinarynumberisthemaximumoddbinarynumberthatcanbecreatedfromthiscombination.Returnastringrepresentin......
  • [LeetCode] 2583. Kth Largest Sum in a Binary Tree
    Youaregiventherootofabinarytreeandapositiveintegerk.Thelevelsuminthetreeisthesumofthevaluesofthenodesthatareonthesamelevel.Returnthekthlargestlevelsuminthetree(notnecessarilydistinct).Iftherearefewerthan......
  • [USACO11NOV]Binary Sudoku G 题解
    定义一个\(3\times3\)的表格\(a\),表示每个小九宫格内1的个数的奇偶状态。再定义两个长为\(9\)的数组\(c0,c1\),表示每行每列上1的个数的奇偶状态。当\(d_{i,j}\)取反时,会将\(a_{[\frac{i}{3}],[\frac{j}{3}]},c0_i,c1_j\)各取反一次。要将\(a_{i,j}\)全部变为0......
  • Nginx添加第三方模块,出现“is not binary compatible in”错误的解决方案
    动态编译好第三方模块:ngx_http_ts_module.so 检测nignx配置,异常sudo/usr/local/openresty/nginx/sbin/nginx-tnginx:[emerg]module"/usr/local/openresty/nginx/modules/ngx_http_ts_module.so"isnotbinarycompatiblein/usr/local/openresty/nginx/conf/nginx.conf......
  • LeetCode] 2476. Closest Nodes Queries in a Binary Search Tree
    Youaregiventherootofabinarysearchtreeandanarrayqueriesofsizenconsistingofpositiveintegers.Finda2Darrayanswerofsizenwhereanswer[i]=[mini,maxi]:miniisthelargestvalueinthetreethatissmallerthanorequaltoqueries[......
  • MySQL备份恢复数据--binary-mode is enabled and mysql is run in non-interactive...
    使用mysqldump;MySQL自带的逻辑备份工具。mysqldump[选项]数据库名[表名]>脚本名mysqldump[选项]--数据库名[选项表名]>脚本名mysqldump[选项]--all-databases[选项]>脚本名备份mysqldump-hlocalhost-uwordpress-pwordpress_20200104>c......
  • npm i canvas 报错 [email protected] run install node-pre-gyp install --fallback-to-b
    今天在写项目的时候安装npmicanvas的时候一直报错。具体错误如下npmERR!commandfailednpmERR!commandC:\Windows\system32\cmd.exe/d/s/cnode-pre-gypinstall--fallback-to-build--update-binarynpmERR!node-pre-gypinfoitworkedifitendswithoknpmE......
  • Check if a given binary tree is BST【2月14日学习笔记】
    点击查看代码//CheckifagivenbinarytreeisBST#include<iostream>#defineMIN-999999#defineMAX999999structnode{intdata;node*left;node*right;};node*getnewnode(intx){node*temp=newnode;temp->data=x;......