zoukankan      html  css  js  c++  java
  • B. Binary String Constructing

    #include<bits/stdc++.h>//构造,不断重复固定的结构缩小问题的范围,分类讨论(奇偶,01的多少) 
    using namespace std;//抓住主线,深入思考,不用怕wa,事实证明只要一直做下去总会是对的 
    int cnt[2];//分类构造题--通过举例子来简化思考,这样可以避免过度抽象引起思维混乱 
    string ans;
    int main()
    {
        int a,b,x;
        scanf("%d%d%d",&a,&b,&x);
        if(a>b)
        {
           if(x%2==1)
           {
                 for(int i=1;i<=x/2;i++)
                 {
                    ans+="01";
                 a--;
                 b--;    
              }
              while(a)
              {
                  ans+='0';
                  a--;
              }
              while(b)
              {
                  ans+='1';
                  b--;
              }
           }
           else
           {
                 for(int i=1;i<=x/2;i++)
                 {
                     ans+="01";
                  a--;
                  b--;    
              }
              while(b)
              {
                  ans+='1';
                  b--;
              }
              while(a)
              {
                  ans+='0';
                  a--;
              }
           }
           cout<<ans<<endl;
        }
        else
        {
            for(int i=1;i<=x/2;i++)
            {
                ans+="10";
                b--;
                a--;
            }
            if(x%2==1)
            {
                while(b)
                {
                    ans+='1';
                    b--;
                }
                while(a)
                {
                    ans+='0';
                    a--;
                }
            }
            else
            {
                while(a)
                {
                    ans+='0';
                    a--;
                }
                while(b)
                {
                    ans+='1';
                    b--;
                }
            }
            cout<<ans<<endl;
        }
    }

    如果用string,insert函数会简单很多

  • 相关阅读:
    第一次作业
    第0次作业
    第14、15周作业
    第七周作业
    第六周作业
    第四周作业
    第三周作业
    第4次作业
    第3次作业
    第二次作业
  • 原文地址:https://www.cnblogs.com/lishengkangshidatiancai/p/10343164.html
Copyright © 2011-2022 走看看